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.
Features
- Reply to Mentions
- Tweet
- Follow Users
- Search Keywords in Tweets
- Favorite Tweets
- Send Direct Messages
- Reply to Direct Messages
- Scrape Tweets for Sentiment Analysis
Walkthroughs
- Create Twitter Developer Account
- Setup an IDE or text editor
- Code Your Bot Using My Skeleton
- Run Your Bot
- Locally
- Google Cloud Instance
- AWS ec2 VM Instance
Prerequisites
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 https://github.com/austinbspencer/twitter-bot.git
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 bot.py 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 bot.py.
- 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
- “requirepass”
- “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 .
- Mac instructions
- Windows instructions
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 bot.py
$ 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 \
twitter-bot
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" \
bot-name
Build & Push to remote portainer
Make sure you are in the directory that has your Dockerfile and bot script
docker build --no-cache -t 10.0.0.1:PORT/bot-name .
docker push 10.0.0.1:PORT/bot-name
Running locally without Docker
Dependencies
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 bot.py 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
python bot.py
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
# SCRIPT FOR CPUs ONLY
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 \
software-properties-common
add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(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 https://download.docker.com/linux/ubuntu/gpg | 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 https://github.com/abspen1/twitter-bot.git
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 \
bot
Check that the container is running
sudo docker container ls
sudo docker logs bot_name
Google Cloud Terminal Commands
Running on an AWS ec2 VM
Amazon Web Services offers reliable, scalable, and inexpensive cloud computing services. Free to join, pay only for what you use.
Initialize
- First you need to set up your AWS account
- If you are currently a student, sign up for Github Developer Pack .
- You will get several awesome offers, including a free AWS student account with $100 credit!
- Once you have an account you will need to go to your console through your aws account
- Next go to Launch a virtual machine
- Choose the Amazon Linux 2 AMI (HVM), SSD Volum Type
- This should be the first option available
- Be sure to download and save your key-pair.pem
- Need to protect this private key file with instructions here
ssh into your vm instance
- I prefer to do this on my local machine
- here are some steps to connecting via SSH provided by AWS
- TIP: Your username is likely ec2-user
- To check this, go to your EC@ instance and hit connect at the top
Download Docker
- Awesome instructions here
- Git is necessary for cloning your repositories over and running your personal twitter bot on this VM
Download Redis
- Awesome instructions here
- 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)
Create custom TCP (IF you want to use Redis outside of your VM)
- Also need to create a custom TCP internal security group
- To do this go to Security then click on the security group listed
- Next Edit Inbound rules
- Should look like this
Download Docker
- Awesome instructions
- 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
- $ git clone https://github.com/abspen1/twitter-bot.git
- $ ls (check that the repo cloned into your instance)
Build the docker image
- cd into twitter-bot directory
- $ docker build -t bot .
Check if image was created
- $ docker image ls
Run docker image in a container
$ 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 \
bot
Check that the container is running
- $ docker container ls
- $ docker logs bot_name