Hello friends, today we will talk about something different (something that has nothing to do with mobile or desktop apps). We will build a simple proactive message bot. If you have been using Microsoft’s Botframework, you must have noticed the shift from the V3 to the V4 version and all the changes that came along. I did a few months away from the Botframework and once I was back, I met the V4 version which is different in many aspects to the V3 but better. Things go so fast in the software world, and it is not easy to keep the pace.
When building your bots, sometimes you want them to talk first. You want them to really initiate a conversation with the user and reply to specific conversations especially if the bot is in a group chat. I was confronted with this problem recently. Where I wanted my bot (in Microsoft Teams) to create a new conversation on its own and reply to the conversations it wanted to at any time it needed to. But, most of the docs out there as of the time I’m writing this post, target the V3 version of the Botframework. There were a few examples of building a proactive bot with the V4 version but most required the bot to save the conversation state and reply later to this conversation. This was not the ideal solution for my use-case. After searching and looking at samples in a few github repos (Links are at the bottom of the post), I had an appropriate solution for this scenario. This post is about implementing it.
If you like this post, you can chose to follow me on Twitter or Github and subscribe to this page’s notifications to stay updated with new articles or like my page on Facebook.
Building the Proactive Message Bot
As I mentioned above, this bot runs in Microsoft Teams. What it does is simple. It receives messages and delays for a number of seconds then, create a new conversation and replies to the conversation it created. This bot is built with the intention to be primarily made available in a Team. The purpose of this exercise is to demonstrate how to create new Conversations and reply to the conversation created. What is shown in this demo could be modified to suit a variety of scenarios.
The source code for this demo proactive message bot is found HERE.
To create your bot, you need to install the Botframework v4 Template for Visualstudio or you can do it manually.
Since We are working with Temas will be using The Microsoft Teams Extension library to facilitate some actions specific to Teams. This library as of the time I’m writing this post is not yet released. But can be added as a nuget package (pre-release version) to your project with the following commands: Install-Package Microsoft.Bot.Builder.Teams -Version 4.3.0-beta1
Implementation
First, you need to add the Microsoft Teams Middleware to your bot.
1 2 3 4 5 6 7 8 9 10 | services.AddBot<ProactiveBot>(opt => { var credentials = new SimpleCredentialProvider(Configuration["MicrosoftAppId"], Configuration["MicrosoftAppPassword"]); opt.CredentialProvider = credentials; opt.Middleware.Add( new TeamsMiddleware( new ConfigurationCredentialProvider(this.Configuration))); }); |
To create a conversation, we use the connector client, and we get the channel data as TeamsChannelData
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public async Task<ConversationResourceResponse> CreateConversation(string message, ITurnContext turnContext ) { ConnectorClient _client = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), await GetMicrosoftAppCredentialsAsync(turnContext), new HttpClient()); var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>(); var conversationParameter = new ConversationParameters { Bot = turnContext.Activity.Recipient, IsGroup = true, ChannelData = channelData, TenantId = channelData.Tenant.Id, Activity = MessageFactory.Text(message) }; var response = await _client.Conversations.CreateConversationAsync(conversationParameter); return response; } |
The function above creates a conversation and returns a ConversationResourceResponse
object. To get the Id of the conversation created, use the Id
property on the object returned. We will get this Id and pass it to the following method, to create a reply to that specific conversation.
1 2 3 4 5 6 7 | public async Task ProactivelyReplyToConversation(string conversationId, string message, ITurnContext turnContext) { ConnectorClient _client = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), await GetMicrosoftAppCredentialsAsync(turnContext), new HttpClient()); var reply = MessageFactory.Text(message); reply.Conversation = new ConversationAccount(isGroup: true, id: conversationId); await _client.Conversations.SendToConversationAsync(reply); } |
Conclusion
After the steps above, you need to deploy your bot and add it to a team in Microsoft Teams you can find how to do this here. Here is the end result in picture.
The complete source code for this demo could be found here. With this, you can build a bot which creates conversations and replies proactively. It is left up to you to modify this so suit your purpose.
Learn how to build an Edit Lable control in Xamarin Forms Here.
References:
https://github.com/OfficeDev/BotBuilder-MicrosoftTeams-dotnet
https://github.com/OfficeDev/BotBuilder-MicrosoftTeams-dotnet/tree/master/CSharp/Samples/Microsoft.Bot.Builder.Teams.RemindMeBot
If you find this article useful, please follow me on Twitter, Github, Linkedin, or like my Facebook page to stay updated.Follow me on social media and stay updated Follow me on social media and stay updated
BornSpectator