Watson Telegram Bot Re-visted
Hickmat 100000QA3T Comments (10) Visits (20768)
So having created my Telegram bot I took a look at the code and figured that with a little bit of refactoring I could have a generic engine to allow me to snap in other chat mechanisms like Facebook. With this in mind I decided that the best approach would be to decouple the chat element from the interaction with my Watson layer and use an asynchronous communications mechanism to communicate between the bot interface and my Waston interaction handler.
A few months back I'd created myself a Docker Image to run Mosquitto so I decided to use this coupled with MQTT as the way to decouple my layers. This meant that my architecture would look like:
Next I took the Telegram flow that I documented in my last blog post and set about breaking it down. In doing this I also moved from using a "Long Poll" for getting updated from my bot to using a "Webhook". How to use a Webhook is well documented in the Telegram Bot API docs here so I'm not going to cover that in any detail.
Looking at my code it became clear that I had a chunk of function that handled the heavy lifting re talking to Telegram and then another chunk which managed the interaction with my Watson API flow, and maintained the state of what chats were active. Based on this I created a new NodeRED flow, the chat handler shown above. This flow ended up looking as follows:
In this flow you can see that I've added in the concept of a "Command". These are cases where the Watson Dialog needs to get an external application to do something. In my case I could drive actions and enquiries agains a backend system but this would require the user to perform some level of authentication. How I went about doing the authentication was pretty easy but something for another blog post.
From the flow you can see that it listens on an MQTT topic for questions to be sent to it. These are then checked to see if they are part of an existing chat or a new one and are then sent to my Watson wrapper flow via one of the two API's (start chat or get answer). Finally the results from my Watson flow are sent back to the application that requested them. In the flow you can see that I have Telegram but I've also dropped in a Facebook one ready for me to create a Facebook chat bot To determine where to send the Watson response the inbound MQTT message contains a "Channel" identifier.
With this in place I then re structured my Telegram flow to use this new Chat Handler. The resultant flow looked like this:
At the top of the flow you can see where I added in nodes to set up the Webhook. Out side of that the flow simply listens for inbound messages via the Webhook and sends them on to the "Chat Handler" and listens for response messages from the "Chat Handler" and passes them on to the Telegram user.. Once the message has been sent to the "Chat Handler" I send a "Thinking...." message back to the Telegram user. The flow sends the message back to the right user because the "chat id" is passed to the "Chat Handler" and is returned from the handler along with the response message.
With this in place I re-tested chatting with my Telegram bot and it worked like a dream. Next stop... create a Facebook bot
**** UPDATED to adding sanitized Flows ****