Intelligent Bots with Microsoft Bot Framework & Service Fabric

Intelligent Bots with Service Fabric

During the //build conference Microsoft announced the Microsoft Bot Framework. I find the concept of “Conversations as Platforms” extremely powerful, and yes I agree that this idea / concept is not new at all, but I think it is now, with the Cloud and with the industrialization of Machine Learning techniques, that any developer will be able to build powerful bots that will help organizations to have intelligent bots that will interact naturally with customers.

After researching a little bit about Microsoft Bot Framework, I thought that it has loads of potential, the Microsoft Bot Framework Connector is a communication service that helps you connect your Bot with many different communication channels (GroupMe, SMS, email, and others).

The Get Started guide shows how you can create a simple bot and deploy it to Microsoft Azure App Service.

While Azure App Service is a great service, I immediately started to think how we could effectively :
1.Develop large scale bots that respond to interactions as fast as possible.
2.Manage the state of the bots, so we can track what customers said, and potentially use those conversations to learn what customers like and don’t like.
3.Apply a granular programing model that will help me to continuously improve my bot without affecting availability.

I quickly came to the conclusion that Service Fabric is a great platform to develop and host bots using the Microsoft Bot Framework , especially because of the following reasons :
-Service Fabric actor programing model fits nicely in a bot scenario, as potentially each conversation could become an active conversation.
-We can use Stateful actors or Stateful services to store all the conversations in a reliable way.
-Service Fabric will handle availability for us, and it will also allow us to develop new bot versions without affecting availability…..

And it works just great!!

With this first post I will show you how you can develop a simple bot and host it in Service Fabric using a Stateless Service.
It is important to note that I am in the early stages of validating my theories, like for instance, how to use Stateful Actors to implement more complex bots, but hopefully during next posts I will cover more advanced topics, for now, this is just a first step that shows us how to host a simple bot in Service Fabric.
These are the steps that I followed :

Step 1:Create a Stateless Service Fabric Web API

Create a Service Fabric Stateless Web API

Step 2:Install Microsoft.Bot.Connector Nuget Package

If you use VS Package Manager Console : Install-Package Microsoft.Bot.Connector

Step 3:Modify your API Controller

In my case, I made very minor changes, and I just added a Post Method to return a polite echo Message.

public class EchoController : ApiController
    {
        // GET api/values 
        public IEnumerable<string> Get()
        {
            return new string[] { "You Know it", "Service Fabric Rocks!" };
        }        

        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task<Message> Post([FromBody]Message message)
        {
            if (message.Type == "Message")
            {

                // return our reply to the user
                return message.CreateReplyMessage($"Service Fabric knows you said : {message.Text}");
            }
            else
            {
                return HandleSystemMessage(message);
            }
        }
        private Message HandleSystemMessage(Message message)
        {
            if (message.Type == "Ping")
            {
                Message reply = message.CreateReplyMessage();
                reply.Type = "Ping";
                return reply;
            }
            else if (message.Type == "DeleteUserData")
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            else if (message.Type == "BotAddedToConversation")
            {
            }
            else if (message.Type == "BotRemovedFromConversation")
            {
            }
            else if (message.Type == "UserAddedToConversation")
            {
            }
            else if (message.Type == "UserRemovedFromConversation")
            {
            }
            else if (message.Type == "EndOfConversation")
            {
            }

            return null;
        }

    }

Step 4:Debug the bot with Bot Framework Emulator

You can get the emulator Bot Emulator
Then you just have to start the emulator and change the URL to http://localhost:8688/api/echo, which is where we are publishing the Stateless Web API.

Bot Framework emulator
Type your message and then the bot should politely repeat what you said!

Get the Code!

Simple Echo Bot using the Microsoft Bot Framework and a Stateless Service Fabric Web API