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?
Join the DZone community and get the full member experience.
Join For FreeIn 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.
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.
Published at DZone with permission of Ezequiel Reyno. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Microservices With Apache Camel and Quarkus
-
Understanding Dependencies...Visually!
-
Competing Consumers With Spring Boot and Hazelcast
-
Microservices With Apache Camel and Quarkus (Part 3)
Comments