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.

To create a conversation, we use the connector client, and we get the channel data as TeamsChannelData .

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.

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.

Proactive message bot with the Microsoft Bot framework v4 in c# demo
Proactive message bot with the Microsoft Bot framework v4 in c# demo

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

1 Comment

  1. BornSpectator

    Reply

    Will this work if the the bot has never got a message before (say after a restart)? For e.g CreateConversation function need ITurnContextto send a message to any teams channel. But assuming the bot has just started up (or after a restart) and have not got any message, it does not have a reference to ITurnContext and so will not be able to send a message to any channel proactively.

    Ofcourse this will work if the bot has received a message before.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.