Firebase + Azure Functions + Azure Service Bus + Microsoft Bot Framework – Connecting the dots. Part II

What’s up guys, Eze is here. In my last post I’ve described a system which has several parts such as Firebase, AngularJs and Azure Functions interconnected. I also added new tools like Microsof Bot Framework and Azure Service Bus to enhance our app in a decouple way. As always the working example is available here.  Yesterday I put my focus on the NodeJs Azure Funtion, but today I’ll describe the Microsoft Bot Framework. As I said in my last post (in case you missed it you can check it here), the idea is to create a bot that receives a place name and response with the current date time at that location. After the bot parse the input and generate the proper response puts into the Azure Service Bus a message that will trigger our NodeJs Azure Funtion to store the data in Firebase, pretty cool, isn’t it? You can check the working version here.

Microsoft Bot Framework

Microsoft has created an awesome framework that allows us to create a bot, yes a conversational tool able to interact with our customers. The Bot itself is a web api application that can run in any web server. Then we just register our bot in the Microsoft Bot framework website and under the configuration page we define different channels where our bot is going to run. And that’s it, as simple as that our bot will be able to interact with customer from Skype, Facebook chat or Tulio.

bot

 

Creating the Bot

I already told you the bot is just a web api that can run in any server and the reason I said that is because Microsoft Bot Framework has two different versions, of course a Bot Builder for .Net and a Bot Builder for NodeJs.

 [ResponseType(typeof(void))]
public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
if (activity != null)
{
// one of these will have an interface and process it
switch (activity.GetActivityType())
{
case ActivityTypes.Message:
await Conversation.SendAsync(activity, () => EchoChainDialog.dialog);
break;

case ActivityTypes.ConversationUpdate:
case ActivityTypes.ContactRelationUpdate:
case ActivityTypes.Typing:
case ActivityTypes.DeleteUserData:
case ActivityTypes.Ping:
default:
Trace.TraceError($"Unknown activity type ignored: {activity.GetActivityType()}");
break;
}
}
return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
}

As you can see in the code above we only need to define a post action in our webapi controller which will be the SPOC of our bot. Every message is going to be route to this action. In my case based on the activity type I let my bot response. In this example we are creating a new instance of the class  EchoChainDialog. This dialog entity is going to handle the conversation base on what we define. In my case I just use a switch method from the IDialog object to parse the text and response to our customer.

Azure Service Bus

I’ve created as well an Azure Service Bus to use as a trigger for my NodeJs Azure Function. What that means? Ok the idea is like this after the Bot process the request and generates a response it puts a message into the Azure Service Bus. As you can see below the code to do that it’s really simple. We just need the Service Bus connection string and the queue name. When a new message arrives to this particular queue in the Azure Service Bus that will trigger our NodeJs Azure Function we created yesterday. This mechanism is created and configure in Azure.

 try
{
var client = QueueClient.CreateFromConnectionString("connectionString", "queueName");
var message = new BrokeredMessage(JsonConvert.SerializeObject(responseData));
client.Send(message);
}
catch
{
}

 

For more details of Azure Service Bus you can use this link.

For mode details of Microsoft Bot Framework you can use this link.

For more details of Azure Functions you can use this link.

 

If you found this post useful please don’t forget to press the like button and share it. If you are in doubt don’t hesitate to ask a question and as always thank you for reading.

Advertisements

One thought on “Firebase + Azure Functions + Azure Service Bus + Microsoft Bot Framework – Connecting the dots. Part II

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s