Coding, Software Development

How to Create a Twitter + Slack Bot to Search and Post Hashtags in Less Than 10 minutes

In case the title wasn’t clear enough, I created an application to search twitter for tweets with specific hashtags and then automatically post them in a Slack channel.


Here is the github repo you can clone and test this application for yourself.

Minute 1: Setting up A Twitter Developer Account

Go to the Twitter developer link and sign in with your twitter account if you already have one. After answering a couple of questions around your intended use for the Twitter developer portal, setup your Twitter project. If all goes well, you should be able to see the developer portal… something like this,

Before you exit the developer portal make sure you copy the Bearer Token and save it for all future purposes.

Minute 2: Creating a Slack App and an Incoming Webhook URL

I would tell you what to do but I think the Slack API documentation does an amazing job at it. Follow this link

Minute 3-4: Checking for Python and associated installations

Check if you have Python 3.6 or higher installed. You can check this using python3 –version in the terminal. If you don’t, use to download and install Python on your system. Next we have to install Flask and requests. Before that, check if you have pip. Installations through pip become extremely easy. pip is a package management system used to install and manage software packages written in Python. Here is an easy way to get the pip

Once you have pip, just use pip install Flask and requests packages.

pip install Flask requests

Minute 5: Creating the config file

Now that all the setup has been done, let’s start writing the actual code. But before that, let’s create the config file. (Just this one more thing before we start doing all the fun stuff in Python)

Remember the Bearer Token you saved in step 1, you will need that now. Create a file named config.json and put your Bearer Token there.

    "BEARER_TOKEN" : "<your bearer token>"

Minute 6: Creating the Twitter Hashtag Search Class in Python

Finally, let’s start coding. We will create a class to search hashtags on twitter.

Let’s start by creating a constructor for your class so that you can create a separate instance of your bot for every request. Don’t worry about memory overhead here; the Python garbage collector will clean up these instances once they are no longer needed. This code sets the query string based on a parameter passed to the constructor.

#The TwitterHashtagSearch class takes the query to search through tweets
class TwitterHashtagSearch:

    # The constructor for the class. It takes the query as the a
    # parameter and then sets it as an instance variable
    def __init__(self, query):
        self.query = query

Next we create the function to search twitter for the query string passed in the parameter. You can find all the code in my GitHub repository.

#Get the bearer token from the config.json file
    def get_bearer_token(self):
            with open('config.json', 'r') as f:
                config = json.load(f)
            bearer_token = config["BEARER_TOKEN"]
            return bearer_token
            print("Problem loading config.json")
            return None

    #Search Twitter to find the query to get the latest 20 tweets matching the query
    def search_twitter(self):
        bearer_token = self.get_bearer_token()
        headers = {"Authorization": "Bearer {}".format(bearer_token)}
        tweet_fields = "tweet.fields=text,author_id,created_at"

        #The max_results parameter can take any value between 10 and 100
        url = "{}&{}".format(
            self.query, tweet_fields
        response = requests.request("GET", url, headers=headers)

        if response.status_code != 200:
            raise Exception(response.status_code, response.text)

        return response.json()

Minute 7: Creating a function to post the tweets in Slack

Next, we create a function to post the searched tweets to the Slack channel using the Incoming Webhook URL created in Step 2. You can find all the code in my GitHub repository.

def post_tweet_to_slack(current_tweet):
    """Post the tweet to the Slack channel using the webhook.
    headers = {"Content-Type": "application/json"}

    #URL of the incoming webhook
    url = "<your webhook url>"

    #Extracting the text field from the tweet
    text = current_tweet['text']
    payload = {
        "text" : text

    response = requests.request("POST",url, headers=headers, data=json.dumps(payload))
    if response.status_code != 200:
        raise Exception(response.status_code, response.text)

Minute 8: Creating a Flask App to Run your Bot

We are going to create a Flask Application to run our bot. You can find all the code in my GitHub repository.

if __name__ == "__main__":
	# Create the logging object
    logger = logging.getLogger()

    # Set the log level to DEBUG. This will increase verbosity of logging messages

    #Define the query you want to search twitter for. This can be a hashtag, terms, etc.
    query = "%23spidermannowayhome"

    #The twitter_bot object creates an instance of TwitterHashtagSearch 
    twitter_bot = TwitterHashtagSearch(query)

    json_response = twitter_bot.search_twitter()

    #pretty printing
    #Feel free to delete this line if you like 
    print(json.dumps(json_response, indent=4, sort_keys=True))

    tweets = json_response['data']

    #Slack has a character limit on the body of a request. So we can't dump the content of 20
    #tweets at once. Hence, we end up looping through the array of 20 tweets and calling 
    #this request 20 times. 
    for tweet in tweets:

Minute 9: Running your Flask App

Finally bring everything together and execute your app. Start your Flask app:

python3 <file-name>.py

And Voila! You should get the tweets in your Slack workspace.

I was curious about Spiderman: No Way Home so that’s the hashtag I searched for


Once you are done developing your application and you are ready to move it to production, you’ll need to deploy it to a server. This is necessary because the Flask development server is not a secure production environment. There are many options for deploying Flask applications. I use Docker to deploy my Flask applications but you can use Gunicorn, Ngnix, WSGI. If you are curious, you can explore these links –

There are many other ways in which you can use APIs to fit your needs. Keep exploring and Happy Coding! ?


Share on facebook
Share on twitter
Share on linkedin

Related Articles