Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Connecting the Dots Between Firebase and Microsoft Bot Framework: Part II

DZone's Guide to

Connecting the Dots Between Firebase and Microsoft Bot Framework: Part II

You can create a bot that puts a message into the Azure Service Bus that will trigger your NodeJS Azure function to store the data in Firebase. Pretty cool, isn't it?

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

In Part I of this post, I described a system with several parts, such as the connections between Firebase, AngularJS, and Azure functions. I also added new tools like Microsoft Bot Framework and Azure Service Bus to enhance our app in a decoupled way. As always, the working example is available here.

Last time, I put my focus on the NodeJS Azure function, but today, I'll describe the Microsoft Bot Framework. As I said in my last post, 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 parses the input and generates the proper response, it puts a message into the Azure Service Bus that will trigger our NodeJS Azure function 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 that is able to interact with our customers. The bot itself is a web API application that can run on any web server. Then we just register our bot on the Microsoft Bot framework website. 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 customers from Skype, Facebook chat, or Tulio.

bot

Creating the Bot

I already told you that the bot is just a web API that can run on any server. Microsoft Bot Framework has two different versions: 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 web API controller, which will be the SPOC of our bot. Every message is going to be routed to this action. In my case, based on the activity type, I let my bot respond. 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 does that mean? The idea is that after the bot processes 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 is really simple. We just need the Service Bus connection string and the queue name. When a new message arrives at this particular queue in the Azure Service Bus, it will trigger the NodeJS Azure Function we created last time. This mechanism is created and configured 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.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
microsoft bot framework ,firebase ,integration ,bot ,azure ,angularjs

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}