How to Use the Twitter API to Create a Hashtag Search Bot

When Postman recently hosted the Postman Galaxy virtual conference with attendees from around the world, I needed to find out what people were saying about the event. And more importantly, who was saying these things. As a new Postman developer advocate, I was assigned to “figure out a way to have all the tweets with the #PostmanGalaxy hashtag automatically sent into a Slack channel.”

After brainstorming some different approaches, I was able to use a combination of the Twitter API, the Postman API, and Slack Incoming Webhooks to achieve exactly that. Below is a result of the final integration in action. Look at all those kind tweets about Postman Galaxy!

Twitter hashtag search bot for Postman Galaxy in action showing results in Slack
Twitter hashtag search bot for Postman Galaxy in action showing results in Slack

The start of a new search bot

Twitter has a vast, well-documented, and (dare I say) fun-to-use API. Despite that, I found that I couldn’t do exactly what I wanted right out of the gate—which was to use the API to find all tweets containing a specific hashtag and get the user information for those tweets. It was that second part that was the challenge.

While the Twitter API can obtain a large amount of user data for a specific tweet, depending on which product track you are using, you may have some limited access to certain endpoints. Since I used the basic “Standard” track, I was restricted from getting everything I needed in one shot.

Instead of upgrading my product track, I thought to myself: “I’m a developer for the people! I’m going to find a way for even Standard track devs to do this.” I was also on a deadline and didn’t have time to apply and wait for my access to be upgraded.

This limited access meant I had to get creative with gathering all the necessary data. Instead of doing a single search and getting everything I desired (username, user handle, date, source, body of tweet, etc.), I had to chain requests together using data from my initial search. Because of the limited access inherent to the Standard product track, when I sent a search query for the string %23PostmanGalaxy to the Twitter API, it would return only the author_id, but not the user’s Twitter handle or Twitter name.

Now I don’t know about you, but I don’t have other peoples’ Twitter ID numbers memorized. In fact, I couldn’t even tell you what mine is. And since we wanted to know who was using the #PostmanGalaxy hashtag, I needed to find a way to tie these author_ids to an actual Twitter handle that humans would understand. So I captured the IDs of the users in a comma separated string and saved it as an environment variable called allUserIdsString.

Thankfully, the Twitter API has a “Users by ID” request that takes the string of IDs as a parameter. I copied this request from the Twitter API v2 Postman Collection into my own folder and entered the environment variable as the query parameter value as shown below. Upon a successful request, I used some JavaScript code in the Tests tab to match the newly acquired usernames with the corresponding author_ids to our saved data.

Sending a second request to Twitter to obtain more user information
Sending a second request to Twitter to obtain more user information

With all of our tweets and necessary information in place, it’s time to send these tweets somewhere the rest of our team can see them: a Slack channel.

Setting up Slack webhooks for notifications

Slack makes it pretty easy to send things to a certain Slack channel using webhooks. After generating a webhook URL, I set up a POST request in Postman to send all tweets with the #PostmanGalaxy hashtag to a designated Slack channel that my team members were following. In the Body tab of the request, I formatted the content (the tweets) that I wanted sent over to Slack as JSON.

Using Slack’s nifty Block Kit Builder tool, you can get quite a bit of flexibility and customization on how you want the message to go through. Here’s a picture of that JSON body below. You can see that it sends only one tweet at time, called current_tweet, which is in the double curly braces to reference the variable named “current_tweet.”

This is what the JSON body looks like for a POST request to a Slack webhook URL
This is what the JSON body looks like for a POST request to a Slack webhook URL

Solving a character-limit conundrum

I learned the hard way that Slack has a character limit on the body of a request. When I tried to send the content for 20 tweets to post to Slack in one shot, I continuously got an error message. After banging my head against a wall for a while, an experienced co-worker suggested that the character limit may be the reason (thank you, Arlemi Turpault, for that key insight!).

Because of the character limit, I reused this request multiple times. If there are 20 tweets we want to send to Slack, we end up looping through the array of 20 tweets and calling this request 20 times. Thankfully, Postman makes it really easy to specify the order of your workflow based on certain conditions.

Automating the process with Postman monitors

If you planned on running this collection manually in Postman every time you wanted to see the new Tweets, we’d be done. But then you’d have to come into Postman every so often and use the Collection Runner to run through the entire collection. Not too bad if you only need to do it once or twice, but for a multi-day event like Postman Galaxy you’ll want a more efficient solution.

Since I didn’t want to wake up every 10 minutes in the middle of the night to update my team with the new #PostmanGalaxy tweets, I found a way to automate this process. By taking advantage of Postman’s monitors, we can run this collection automatically at set intervals.

The last step of this collection involves a pair of requests that work together to track the most recent tweet’s ID number, which is saved as an environment variable highest_tweet_id. Because we’re automating this process with monitors, it’s important to note that global/environment variables are not persisted across collection runs using a monitor.

To get around this, we can actually use Postman to help us use Postman. While Postman is an API collaboration platform, we also have the Postman API, which you can use to ensure only the newest, unseen tweets get pushed to Slack each time the monitor is run. All we’re really doing in the final two requests is making sure that the environment variable for the highest tweet ID gets updated and safely tracked for future monitor runs.

Here’s a visual overview of everything in this workflow. You can see that POST request to Slack getting called again so long as there are tweets to send to Slack:

Visual overview of workflow using Postman, Twitter API, Slack, and Postman API

Create your own Twitter hashtag watch

Now that you’ve learned how I made an automated Twitter hashtag search bot that posts updates to Slack, you can try creating your own. Feel free to fork the collection and play around with it. With complete instructions in the collection documentation, here’s a quick summary of the steps:

  1. Fork the Twitter Hashtag Search collection into your own workspace.
  2. Fork the Twitter Hashtag environment into your own workspace.
  3. Enter the missing auth credentials in the environment (Twitter bearer token, Postman API key, and Slack webhook URL).
  4. Change the query param in the “Search for Hashtag” request.
  5. Create and run a monitor for the collection.
  6. Check out the Slack messages for search results.

Stay up to date with your favorite hashtagged tweets about anything you find interesting (weird animals, anyone?). Seriously, there are some adorable and strange hashtags worth following—the Twitter world of helpful and entertaining hashtags is endless.

Technical review by Meenakshi Dhanani.


What do you think about this topic? Tell us in a comment below.

Comments

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



This site uses Akismet to reduce spam. Learn how your comment data is processed.

3 thoughts on “How to Use the Twitter API to Create a Hashtag Search Bot

  • Rolo

    Great POST

    • Sean Keegan

      I see what you did there! Thanks for the kind words, Rolo!

You might also like: