AIAppsAzureBotsCloudCognitive Services

Build your very own Twitter QnA Bot

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.

Solution Overview

The main architecture of this solution is basically reflected by the following illustration:

#AskCloudguy Twitter Bot solution design
#AskCloudguy Twitter Bot solution design

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 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:

QnaMaker service knowledgebase
QnaMaker service knowledge base

Azure Function

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:

npm install twit

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:

  • consumer_key
  • consumer_secret
  • access_token
  • access_token_secret

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:

Azure Function app settings
Azure Function app settings

Now that we have that in place, we can update/replace the code of the function by the following code:

module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    var Twit = require('twit');
    var T = new Twit({
        consumer_key:         process.env['consumer_key'],
        consumer_secret:      process.env['consumer_secret'],
        access_token:         process.env['access_token'],
        access_token_secret:  process.env['access_token_secret'],
        timeout_ms:           60*1000,  // optional HTTP request timeout to apply to all requests.

    if ( || (req.body && req.body.tweetID)) {
        context.res = {
            status: 200, /* Defaults to 200 */
            body: "Status " + ( || req.body.tweetID)
        // create the reply object
        var new_tweet = req.body.answer;
        var tweet = {
            status: new_tweet,
            in_reply_to_status_id: req.body.tweetID
        // reply to the original tweet'statuses/update', tweet, tweeted);

        function tweeted(err, data, response) {
            if (err) {
            } else {
                context.log("It worked!");
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"

Azure Logic App Design

The design of the Azure Logic App looks as follows:

Cloudguy Twitterbot Azure Logic App Design
Cloudguy Twitterbot Azure Logic App Design

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:

Twitter Bot test
Twitter Bot test

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 😉

One thought on “Build your very own Twitter QnA Bot

Leave a Reply

Your email address will not be published. Required fields are marked *