Build your very own Twitter QnA Bot
Table of Contents
The idea of this solution has come to my mind a couple of months ago when doing a couple of conference sessions. During the sessions, I had some questions coming from the audience, which is a good thing for me, but somehow I noticed, that those a more or less general questions rather than deep technical ones.
Therefore, I thought it would be helpful for me as well as for the audience to have a much simpler way of asking me questions during speaking engagements, but also whenever someone has a question which might be answered by me. And which social media platform would be suited better than Twitter for exchanging short messages which might be of interest to a broader audience? So the idea of my #AskCloudguy Twitter Bot was born. Feel free to implement your very own Twitter Bot based on a specific hashtag in order to build an automated Twitter QnA service for your followers based on the following solution.
The main architecture of this solution is basically reflected by the following illustration:
The main idea is that there is an Azure Logic App which is checking for new tweets based on a given hashtag. Whenever the Azure Logic App gets triggered, it receives the tweet text as well as the tweet ID, by the Twitter connector. The tweet text is forwarded to the QnAMaker.ai service in order to generate an answer based on the tweet text. After the answer is received by the QnAMaker the Azure Logic App makes an HTTP POST request to the Azure Function and forwards the tweet ID as well as the answer in the HTTP body in order to reference the original tweet and being able to post a reply to the tweet rather than posting a new tweet. The Azure Function itself connects to the Twitter API via the Twit module and updates the original tweet by referencing the tweet ID and replies with the answer generated by the QnAMaker service.
QnA Maker Service
The only thing you have to do is to create a new knowledgebase within the QnAMaker service and populate it with QnA pairs:
The Azure Function is the service where the actual Twitter reply action happens. In order to connect to the Twitter API I decided to use the Node module Twit which needs to be installed by running the following command in the function’s console first:
After the module and it’s dependencies have been installed, it’s time to update the application settings of our Azure Function, but first we need to gather the keys and secrets by Twitter. So head over to the Twitter app portal and create a new app in order to gather the following keys and secrets:
After you have copied them somewhere, we need to add those four variables to our application settings of the Azure Function in order to keep these secrets and keys out of our code:
Now that we have that in place, we can update/replace the code of the function by the following code:
Azure Logic App Design
The design of the Azure Logic App looks as follows:
Feel free to implement your very own hashtag. Please make sure to replace the Uri of the Azure Function with the correct Uri of your Azure Function.
Now we have everything in place we can test the solution by posting a new tweet with a question which is part of a QnA pair in our QnA knowledge base and check if the reply is being posted to that tweet accordingly:
Now that’s it. You build your very own QnA Bot for your very own Twitter hashtag. If you have any questions please feel free to contact me - try it out by posting a new question on Twitter with the hashtag #AskCloudguy and my Bot will reach out to you ;)