Building a Proactive Message Bot in Teams with The Botframework v4 in c#

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.

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.

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

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

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.