Software Architect / Developer / Microsoft MVP (AI)

Bot Framework, C#, Chatbots, Twitter

Using Bot Framework Composer & the Twitter API to create a chatbot that can search for Tweets

Recently I had to consume a 3rd party API from Bot Framework Composer that required Bearer Authentication.

The Bearer Authentication process typically involves sending your client key and secret to a publicly available endpoint, the endpoint then returns a unique string that can be used in subsequent requests to access protected web applications or services.

Failure to include the Bearer Token when trying to access a protected resource will result in a 401 error being returned by the protected resource (unauthorized).

A lot of APIs behave this way and maybe it is something you also need to do.

In this blog post, I’ll show you how to:

  • set Bearer Authentication in Bot Framework Composer
  • connect to the Twitter API in Bot Framework Composer
  • create a simple chatbot that lets you search for Tweets

You’ll have this up and running in less than 60 minutes.

Twitter Developer Application and Generating a Bearer Token

First, to make this possible, you need a registered developer application setup on Twitter. Once you have that you can then generate your Bearer Token.

To generate your Bearer Token, you need to make a request to the following endpoint:

https://api.twitter.com/oauth2/token?grant_type=client_credentials

I used Postman to quickly generate the Bearer Token. To do this, you need to set the client key and secret and the Authentication Type to Basic. You can see this here:

Now that we have the Bearer Token, we can use this to make a request to the Twitter API.

Standard Search API

We’ll use the Twitter Standard Search API. It’s free and lets you surface Tweets that match your specific search criteria. You can find out more about it here.

The root URL for the Standard Search API is: https://api.twitter.com/1.1/search/tweets.json

For the purposes of this blog, we’ll extend this to include the following parameters:

  • q=iphone
  • lang=en
  • count=1

Meaning our final URL we’ll use to make requests will be: https://api.twitter.com/1.1/search/tweets.json?q=iphone&lang=en&count=1

In plain English, this tells the Standard Search API to “search for Tweets that contain the text iphone that have been writing in English, then to return only 1 Tweet.

We can test this request in Postman by supplying the query. Doing so returns the following JSON:

Microsoft Bot Framework Composer

Now that we’ve proven we can fetch Tweets; we can model a chatbot using Composer. You can find out more about Composer in my earlier blog post.

This chatbot will do the following:

  1. Ask you what you want to search for
  2. Ask how many tweets you want to fetch
  3. Display the Tweets in a series of messages

Here we are asking the user what they want to search for and storing the input in a variable called dialog.term:

Next, we ask them how many Tweets they want. We then store this in dialog.count:

Now that we have the users query and the number of Tweets they want; we can send the Http Request to the Twitter API:

We need to take a closer look here as we’re doing a few things with this logic:

Setting the HTTP Method

The Standard Search API requires a GET, so we set the HTTP method:

Setting the URL endpoint

We set this to https://api.twitter.com/1.1/search/tweets.json?q=${dialog.term}&count=${dialog.count}&lang=en

Notice in the above URL that we inject the variables from our dialog into the querystring, thereby sending our user input to the Twitter API.

Setting the Authorisation Header

We need to authenticate our request with our generated Bearer Token. Notice the syntax “bearer <space> <bearer token value>. You can see this here:

Storing the data

Finally, we take the response from the Twitter API and storing in a variable called dialog.api_response:

Displaying the Data

With everything in place, all we need to do now is render the data from the Twitter API. The JSON payload from Twitter Standard Search API contains an array of Statuses (the Tweets). Each Tweet will contain JSON like the following fragment:

 "statuses": [
        {
            "created_at": "Fri Apr 17 04:35:36 +0000 2020",
            "id": 1251006371316801536,
            "id_str": "1251006371316801536",
            "text": "RT @Mus6ey: Only and IGBO man can sell his Mother and give his ashawo girlfriend the Money to buy wigs, nails and iPhone. Then come to Twit…",
            "truncated": false
        }

Composer will read this JSON into  dialog.api_response.CONTENT and we need to be able to loop through this.

Therefore, to access the array of statuses/Tweets, the property we need to loop through is : dialog.api_response.CONTENT.STATUSES

And finally, we can display the loop index we’re processing (index+1) Tweet Text (value.text) and Username (value.user.screen_name) that sent the Tweet:

For reference the value component of each expression is the actual Tweet we’re currently processing.

Testing

With everything in place, we can now run the chatbot! Here you can see I greet the bot, it asks me what I want to search for and how many tweets to fetch. The bot then invokes the Twitter API Standard Search API and displays the Tweets.

Granted this is a basic example but we’ve introduced a few different concepts. In the real world, you most likely place your chatbot in front of an existing data store that has already processed and aggregated social data – rather than query in real-time.

Other integrations could include Facebook, Instagram, Reddit.

You could even augment the chatbots intelligence by introducing Azure Cognitive Services such as text analytics or computer vision to help surface additional insights.

Summary

In this blog post we’ve seen how you can quickly build a chatbot using Bot Framework Composer and integrate this with the Twitter API.

We’ve also learned how to generate a bearer token and how to set this in Composer to help create a chatbot that can make authenticated requests to the Twitter API which searches for and returns Tweets.

Full code for this blog can be found on my GitHub repo here.

JOIN MY EXCLUSIVE EMAIL LIST
Get the latest content and code from the blog posts!
I respect your privacy. No spam. Ever.

Leave a Reply