Twitter Bot

Austin | Jan 1, 2021

Build your own Twitter Bot to increase your accounts engagement within social platforms through the integration of automated Twitter actions. The Twitter Bots can run 24/7 and perform several tasks.

Disclaimer: This tutorial is leveraging V1 of the Twitter API, which is still usable, but requires additional steps to obtain access for newer developer accounts.

Why Create a Twitter Bot

My original purpose for creating a Twitter Bot was to gain knowledge and experience coding in Python. I also was able to gain experience interacting with several other tools along the way. These tools include Redis, Docker, REST API’s, and Cloud Deployment, to name a few.

However, there are several other reasons you may want to build a Twitter Bot. With little-to-none coding experience you could gain 10x or more engagement on your tweets and account. You can boost your followers and find more accounts you are interested in. You can promote your business or scan for potential clients of your business. There are countless ways you can leverage and automated account and this tutorial will walk you through a handful to get you started.


  • Reply to Mentions
  • Tweet
  • Follow Users
  • Search Keywords in Tweets
  • Favorite Tweets
  • Send Direct Messages
  • Reply to Direct Messages
  • Scrape Tweets for Sentiment Analysis


  1. Create Twitter Developer Account
  2. Setup an IDE or text editor
  3. Code Your Bot Using My Skeleton
  4. Run Your Bot
    1. Locally
    2. Google Cloud Instance
    3. AWS ec2 VM Instance


Twitter Developer Setup

  • Create your developer account
  • Start a new application and fill out your information
  • Save your needed keys
    • Consumer ID (API KEY V2)
    • Consumer Secret Key (API SECRET V2)
    • Key ID (ACCESS TOKEN V2)
    • Secret Key ID (SECRET V2)

IDE/Text Editor

I use Visual Studio Code and prefer that over the other editors I have used. But I will also link some other popular ones if you want to check them out.

Python Download

Make sure to download a version of Python that is at least 3.

Customize Your Bot

Below are some instruction you can copy and paste in your terminal to get started with creating your personal bot. First open terminal and then cd into wherever you want to store your code. In this example I will cd into Documents and assume you are using VSCode.

cd Documents/
git clone
cd twitter-bot/
# code . is a shortcut with Visual Studio Code.
# If you aren't using VSCode just open the twitter-bot folder
# from within your preferred editor.
code .

Now that you have the code on your local machine you can edit to make it your own. Also, if you need inspiration or a live example run the command below in your terminal.

# cd into twitter-bot folder first
git checkout coding-specific

Now you will be able to see the exact script that my twitter bot is running within

  • After you’ve personalized the script, you are ready to get it running

Choose where to run your script

Three guides here for deployment. Cloud is preferable to keep the bot running more conveniently.

  • locally
  • Google Cloud VM instance
    • When you open a new account on the Google Cloud console you will recieve $300 credit for you to spend however you’d like.
  • AWS ec2 VM .
    • AWS offers a credit for the first year of use for current students.

Deploying Locally

Redis Setup

  • Download redis and activate your redis server -> youtube example
  • Start running your redis-server
  • Next open your redis-cli
    • Be sure to change the requirepass within your config to secure your server
    • Within redis-cli// > config get requirepass
      1. “requirepass”
      2. “This Will Be Empty”
  • Set your password
    • Within redis-cli// > config set requirepass yourPasswordHere (recommended >= 32 characters)

Docker Setup

Docker will allow us to run the Python script within a Dockerized container. This allows the script to be run on any machine. You can skip to run without Docker .

Build and Run Docker Images

Build your image and then run the image as a Docker container with the commands below.

  • FIRST cd into your working directory with Dockerfile and
$ docker build twitter-bot

$ docker run -d \
  --name bot_name \
  --restart unless-stopped \
  -e CONSUMER_KEY="some consumer ID" \
  -e CONSUMER_SECRET="some consumer secret KEY" \
  -e KEY="some key ID" \
  -e SECRET="some secret key ID" \
  -e REDIS_PASS="some password" \
  -v $PWD:/work \

Pull from previous build (optional)

docker pull bot-name \
&& docker run -d \
  --name bot_name \
  --restart unless-stopped \
  -e CONSUMER_KEY="some consumer ID" \
  -e CONSUMER_SECRET="some consumer secret KEY" \
  -e KEY="some key ID" \
  -e SECRET="some secret key ID" \
  -e REDIS_PASS="some password" \

Build & Push to remote portainer

Make sure you are in the directory that has your Dockerfile and bot script

docker build --no-cache -t .

docker push

Running locally without Docker


You will need to pip install the following packages if you want to run this bot locally without Docker.

  • Using requirements.txt
pip install -r requirements.txt
  • pip install manually
pip install redis
pip install schedule
pip install tweepy

# If you don't plan on using sentiment analysis ignore these
pip install matplotlib
pip install numpy
pip install pandas
pip install seaborn
pip install textblob

Setup local Environment

  • Be sure to cd into your working directory that contains your script
  • Export your variables so you can access them with os.getenv() (Mac OS / Linux)
export CONSUMER_KEY="your key" \
&& export CONSUMER_SECRET="your secret" \
&& export SECRET="your secret"\
&& export HOST="external ip address" \
&& export REDIS_PASS="your redis password"\
&& export KEY="your key"
  • Now you are ready to run your script

Running on Google Cloud VM Instance

  • First you need to set up your Google Cloud Instance
  • When you set up a cloud account you will get $300 credit!
  • You will need to go to Google Cloud Platform
  • Then to console where you can set up your compute engine
  • Watch this video for help with inital setup video
  • You should only need machine-type f1-micro (1 vCPU, 0.6 GB memory)
    • This will help make your $300 credit last longer

ssh into your vm instance

  • I prefer to do this on my local machine but you can also do it in browser within a shell
  • Install gcloud sdk on your local machine
  • There will be a generated ssh that you can copy and paste into your terminal to ssh into your vm instance using gcloud
  • Once you’re inside your instance within your terminal it should look something like this

Download Redis

  • Instructions
  • Very easy and can be completed in < 10 minutes
  • Be sure to create a secure password if you choose to configure remote access (recommended >= 32 characters)
  • Find your external IP on your VM console (needed for the HOST env variable)

Download Docker

  • Instructions
  • My tl;dr instructions are below
  • Note that you will likely need to add sudo infront of each command
    • Quickest way to fix this when you get the permission denied error
    • $ sudo !!
    • This is a shortcut to run previous command but with super user permissions
apt-get -y update

# If you have issues with this command, omit python-software-properties
apt-get -y --no-install-recommends install \
  curl \
  apt-utils \
  python-software-properties \

add-apt-repository -y "deb [arch=amd64] $(lsb_release -cs) stable"

# If you need to add sudo as prefix to these commands be sure to add sudo in front of the "apt-key add -"
curl -fsSL | apt-key add -

apt-get update
apt-get install -y docker-ce

Check that install worked

docker --version
  • If this shows your docker version then you’ve successfully installed docker on your vm instance

Clone your git repository to run within docker

  • First check that git is installed
git --version
git clone
ls (check that the repo cloned into your instance)

Build the docker image

cd into twitter-bot directory
sudo docker build -t bot .

Check if image was created

sudo docker image ls

Run docker image in a container

sudo docker run -d \
  --name bot_name \
  --restart unless-stopped \
  -e CONSUMER_KEY="some consumer ID" \
  -e CONSUMER_SECRET="some consumer secret KEY" \
  -e KEY="some key ID" \
  -e SECRET="some secret key ID" \
  -e HOST="external ip address" \
  -e REDIS_PASS="some password" \
  -v $PWD:/work \

Check that the container is running

sudo docker container ls
sudo docker logs bot_name

Google Cloud Terminal Commands