Twitter Bot Using Python and Heroku

Dr. Jessica Rudd
6 min readDec 29, 2020

Learn how to scrape data for automated Twitter posts using Python and Heroku for free

Tl;dr

While waiting for work-related code to run I wanted to use the time to learn new coding skills, so I built a Twitter bot to share the weather and encouraging quotes each morning. Follow @RResurgens.

Why build a Twitter bot?

  1. It’s quick and easy
  2. Heroku provides a ready-to-use environment based on containers that makes it easy to deploy code — smaller learning curve than AWS, imho (although Heroku actually uses AWS to deploy the code itself).
  3. I wanted to learn how to pull data using an API, refactor code, and push code to a server (as opposed to just running code locally)

Thanks to staying home and COVID-safe for the holidays this year, and the fact that my husband is deep into a round with Red Dead Redemption 2, I decided to use the down time from the hustle of a new job to learn a few skills and have some fun. Twitter bots, while not having the best public reputation, have always fascinated me. When used for good in clever ways they can provide helpful information and/or entertainment. I love this weather bot, for example:

The Twitter developer user agreement makes it clear that a bot should provide useful information. As an avid runner, getting a morning weather forecast is extremely useful! What makes providing this information to runners a bit cheeky though? The truth is, no matter what the weather, you should ALWAYS go for a run! So, I decided to build a bot that tweets the Atlanta weather every day at 7:30am, and tells runners to “GO for a run!”, regardless of the weather. Since I already had a saved list of inspirational quotes (whenever I see a quote I like, I save it in my notes app), I decided to add that to the tweet to provide a little extra incentive, especially since running in ATL these days is often cold, dark, and wet.

Creating the Tweets

I first saved my quotes in a json file as a dictionary of quote and author key-value pairs. This makes it easier to parse the quotes in Python and add one to the twitter text each day.

{
"quotes": [
{
"quote":"Life isn’t about getting and having, it’s about giving and being.","author":"Kevin Kruse"},
{
"quote":"Whatever the mind of man can conceive and believe, it can achieve.","author":"Napoleon Hill"},
{
"quote":"Strive not to be a success, but rather to be of value.","author":"Albert Einstein"},
{
"quote":"Two roads diverged in a wood, and I—I took the one less traveled by, And that has made all the difference.","author":"Robert Frost"},
{
"quote":"I attribute my success to this: I never gave or took any excuse.","author":"Florence Nightingale"},
{
"quote":"You miss 100% of the shots you don’t take.","author":"Wayne Gretzky"},

Luckily, I found Michelle Aniuchi’s amazing blog post and GitHub repository for her Game of Thrones quote tweeting bot. Their post and code are really easy to follow, and provided the template repository and much of the starter code for my project. Using Michelle’s quotes code, I was easily able to pull random quotes from my list and format them for a tweet.

The create_tweet() code from Michelle’s GoT quote Twitter bot

I then registered at OpenWeather for an API key so I could grab the Atlanta weather each morning. OpenWeather allows for a diverse selection of API calls to current weather and forecasts, as well as extensive historical weather data. I used their Current Weather Data API to call Atlanta weather each morning. The website provides example API calls for the various datasets, and custom data calls can be made with minor adjustments. The API call below returns a json data file with the current Atlanta weather in dictionaries.

The weather data and random quote are then combined into a custom tweet:

Once the code was set up to create custom text using current weather and a random quote, it was time to send the tweet! Here again, Michelle’s very helpful code and instructions made it easy to figure out how to call the Twitter API (once registered as a Twitter developer — see below). The function authenticates to Twitter, creates the tweet using the create_tweet() function, and then updates the account status using the API authentication credentials.

Voila, it works!

Now it’s time to get those tweets out in the world, and help get those runners running. Next up, is the step-by-step nitty gritty for getting connected to the Twitter API, building up the code, testing on your local machine, and getting pushed to Heroku (because who wants to have code running in the background on their command line in perpetuity? ). All of the code and instructions are also outlined in my GitHub repository.

Pre-requisites

To build and use the bot, you’ll need to:

  1. Create a new Twitter account to act as the bot
  2. Register for a twitter developer account
  3. Create a twitter app. Make sure to give it Read and Write permissions.
  4. Set up a Heroku account
  5. Initialize git repository in project folder, since Heroku pulls entire projects directly from the working directory of your repository.

How to use

To make your own bot follow these steps:

  1. Clone this repository on your local machine

2. Create a virtual environment in your project’s root directory: python3 -m venv environment && source environment/bin/activate

3. Install the required libraries using pip: pip install -r requirements.txt

4. Create a file called credentials.py in the root directory of your project. Put your twitter App keys there (and any other keys required for scraping data if needed).

#credentials.py
ACCESS_TOKEN=<YOUR_ACCESS_TOKEN_HERE>
ACCESS_SECRET=<YOUR_ACCESS_TOKEN_SECRET_HERE>
CONSUMER_KEY=<YOUR_CONSUMER_KEY_HERE>
CONSUMER_SECRET=<YOUR_CONSUMER_SECRET_HERE>
#API key for pulling weather from OpenWeather
FORECAST_APIKEY = credentials.FORECAST_APIKEY
  • MAKE SURE TO INCLUDE ‘import credentials’ import statement in twitter_bot.py
  • THIS IS JUST FOR TESTING. Once everything is tested and ready to deploy, you’ll move these to environment variables.
  • ADD THIS FILE TO THE .gitignore so you’re not putting your api keys publicly on GitHub!

5. Make changes in the logic of the bot by modyifing twitter_bot.py

6. Test your changes locally by running python twitter_bot.py from the root directory of your project

How to deploy

Once you are happy with your bot:

  1. Add any additional packages you used to requirements.txt
  2. Set up Heroku account and install Heroku command line interface
  3. Create a basic web server script
#server.py
from os import environ
from flask import Flask
app = Flask(__name__)
app.run(host= '0.0.0.0', port=environ.get('PORT'))

4. Set up a Procfile to tell Heroku what to do with the script and server

#Procfile
web: python server.py
worker: python twitter_bot.py

5. Update twitter_bot.pyso that Heroku can find your API credentials. Make sure to include these imports:

import sys
from os import environ

… and update where app retrieves credentials (instead of retrieving from credentials.py file they’ll now be served as environment variables from within Heroku dashboard…

CONSUMER_KEY = environ['CONSUMER_KEY']
CONSUMER_SECRET = environ['CONSUMER_SECRET']
ACCESS_KEY = environ['ACCESS_KEY']
ACCESS_SECRET = environ['ACCESS_SECRET']
FORECAST_APIKEY = environ['FORECAST_APIKEY']

6. Commit and push updated files to local main branch of git repository

7. In command line, in project folder, login to Heroku

Heroku login

8. Create app in Heroku from within CLI

heroku create [your-app-name]

9. Set environmental variables in Heroku dashboard

10. Push local git repository to deploy app

git push heroku main

DONE! Check Twitter to see if a tweet was sent! You can use Heroku dashboard to check logs, troubleshoot, and add additional functionality like scheduling. HAVE FUN!

Limitations

Please read Twitter’s automation rules before using this to create your own bot. Be kind, be helpful…with great power comes great responsibility.

Heroku runs apps in ‘dynos’, i.e. virtual computers that are powered up or down based on your application requirements. Heroku allows two dynos for free, but if your project requires more data processing you’ll need to increase the size and/or quantity of dynos used, leading to monthly charges from Heroku based on usage. These charges are higher/may grow faster than if you deployed directly using AWS or another IaaS (Infrastructure as a Service) service. While the Heroku platform is an easier user experience, deploying to AWS/Azure/Google, etc. may be necessary if your project grows.

Future Work

  1. Refactoring into python package
  2. Add additional functionality to auto-follow followers, and reply to direct messages
  3. Deploy using Docker and AWS

References

*I started this project using this repository as a template from Dylan Castillo: https://github.com/dylanjcastillo/twitter-bot-python-aws-lambda before deciding to switch to the Heroku build based on tasks I needed to learn for work.

*Weather data is pulled from OpenWeather

*How to Set up a Twitter Bot with Python and Heroku

*How to Make a Twitter Bot in Python With Tweepy

*Build and Deploy Twitter Bots with Python, Tweepy and PythonAnywhere

*Making a Quote Tweeting Twitter Bot with Python, Tweepy, and Heroku.

--

--

Dr. Jessica Rudd
Dr. Jessica Rudd

Written by Dr. Jessica Rudd

Senior Data Engineer | MLOps | Endurance Athlete | Using computation to improve analytics pipelines. #ChangeTheRatio

No responses yet