botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

Proactive bot in C#

Open gosha1 opened this issue 10 years ago • 2 comments

I want to know where to call the SendToConversationAsync() method. In the sample, reactive part is done within MessagesController.Post(). Similarly, I want to know where should the proactive be placed. Can someone post a sample proactive bot in c#?

gosha1 avatar Jul 14 '16 21:07 gosha1

Hi, you need to construct ConnectorClient, get new coversation ID, perpare ChannelAccounts, build a message and send it.

var connector = new ConnectorClient(new Uri([ServiceUrl]));

var botAccount = new ChannelAccount("[bot id on given channel]");
var userAccount = new ChannelAccount("[user id on given channel]");

var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId.Id);
message.Text = "Hello";

var reply = await connector.Conversations.SendToConversationAsync(message as Activity);

Service URL in the constructor needs to be cached from previous messages from the user or hardcoded (which is not recommended by the docs).

msimecek avatar Jul 20 '16 09:07 msimecek

Thanks for the help @msimecek

gosha1 avatar Jul 20 '16 19:07 gosha1