botframework-sdk
botframework-sdk copied to clipboard
Proactive bot in C#
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#?
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).
Thanks for the help @msimecek