Kickstart your Twitter bot with our Glitch example written in Python


Kickstart your Twitter bot with our Glitch example written in Python

By Jessica Garson

Creating a Twitter bot is a great way to engage with the public conversation. They’re also a great way to get started with the Twitter API. There are many great bots on Twitter, such as bots that Tweet a meadow of emojis to Thread Reader, which enhances the Twitter experience by making it easier to read Tweet threads.

To help you get started faster, you can use our remixable example to make calls to the Twitter API. This example is written using Python and uses an OAuth 2.0 Authorization Code Flow with PKCE. It also utilizes the dbm Python package as a lightweight, cost-effective way of storing access tokens for connecting to the Twitter API in a key-value store. 

This code powers the Twitter bot @noun__verb, which Tweets noun/verb pairings hourly. This application is boosted using Glitch's pro plan

You can check out the full code on our GitHub.

Step 1 - Setting up to use the Twitter API

You need to sign up for access to the Twitter API. You will also create a project in the developer portal and an App containing the credentials required to use the Twitter API. Additionally, you will need to have OAuth 2.0 turned on in your App’s authentication settings. The developer playground is a great place to start using the Twitter API. 


Step 2 - Set up a Twitter account for your bot

You will want to create a new Twitter account for your bot handle. Afterward, you will want to set up the profile for your bot, including adding a profile picture and banner image and setting up a label to indicate your bot is automated.


Step 3 - Remix the Glitch example

You can remix our code sample on Glitch. After, you will want to update the .env file inside of Glitch to include the following fields:


You can obtain your client ID and client secret from the developer portal. The Glitch project name will be generated when you remix the project. You will also need to add your redirect URI in your App’s authentication settings in the developer portal.

To download all the requirements you need, you can run the following command in the terminal inside of your Glitch project.

      pip3 install -r requirements.txt

Step 4 - Tweet your bot’s first Tweet

Now that you’ve set everything up, you can start Tweeting from your bot’s handle. To do so, you can visit the site you’ve created and click the green button that says “Let’s Begin.”

This should look like this:


Step 5 - Make it your own 

To customize this bot, you may want to change it, so it doesn’t just Tweet noun/verb pairings. You can update this function to call different APIs. To find inspiration, you can look at the APIs available in Postman or ProgrammableWeb’s API directory.


      def parse_noun_verb():
    noun_url = ""
    verb_url = ""
    noun_response = requests.request("GET", noun_url).json()
    verb_response = requests.request("GET", verb_url).json()
    noun = noun_response[0]["word"]
    verb = verb_response[0]["word"]
    return "{}/{}".format(noun, verb)


Another way you can personalize your bot is by adjusting the timing of the bot. This bot currently Tweets hourly, but you can change the variable to run at a different time interval. Check out the Flask APScheduler documentation for more information.

This is the line of code you will want to update to adjust the timing of your bot:

      @scheduler.task('cron', id='everyother', hour='*')

Next steps

To learn more about building a similar bot from scratch, check out our tutorial that walks you through the process. Be sure to inform us on the forums if you run into any troubles along the way, or Tweet us at @TwitterDev if this tutorial inspires you to create anything!