Build a Twitter Bot with Python, Tweepy and the Twitter API
- Description
- Curriculum
- FAQ
- Reviews
This is the perfect weekend project for Python beginners looking to create their first Twitter bot!
What is this course?
This course is for python beginners and twitter lovers who are interested in learning how to build a Twitter bot.
While I have found it helpful to read textbooks and watch online tutorials to get a better understanding of the basics for any subject, nothing beats project-based learning. Actually getting your hands dirty and running into real problems that require specific solutions has been my ideal way to learn something new.
With that being said, the hardest question typically is, what project should I focus on? From my personal experience, I’ve found it beneficial to focus on something you are passionate about. To find that something, just think of what you frequently pay attention to in your spare time, when no one is paying you…to pay attention to it. For me that is Twitter.
We are going with a bot because it will be a fun project to group together various ideas we want to learn, such as APIs, python, and Twitter.
Power of python
I’ve been hooked on it ever since I took the class Automate the Boring Stuff with Python. It is undoubtedly a popular programming language so I think it will be beneficial for many years to come.
This class is not meant to be an introduction to programming or python, so my assumption is that you understand some basics. This class is geared more towards helping you apply Python programming to an actual project to help you better retain information while having fun within the process.
Power of Twitter
I must admit. I am addicted to Twitter. Now before you scorn me, understand that I think it is truly a magical place. One of my favorite Twitter follows, Nikhil Krishnan, has a great presentation about Why Twitter is Dope. In it he basically highlights these reasons which I think get at the root of why I love Twitter so much.
- You get to discover people with mutual interests. This can include any age!
- Making friends is geo agnostic. You can connect with people all over the world at anytime.
- You can also access the smartest people in the world. I get to follow Elon Musk!
- Twitter is the idea marketplace at it’s finest. If you are wrong, you will quickly learn why that is.
- You get to follow people and not brands. People are brands essentially but atleast its more authentic.
- It’s quality over quantity. A few good tweets is infinitely better than a million double taps.
- It’s a conversation with an incredibly tight feedback loop.
Power of APIs
APIs are how software talks with other software. We can use APIs to build apps that add new services on top of the platforms or data from the platforms. This essentially provides a new experience for users via a layer of abstraction.
Kate Bae says APIs are “how to unleash data in new ways.” For our purposes, that means creating a bot that retweets an accounts likes. Most platforms, like Slack and Shopify, have public APIs with the hopes that devs build on them to build more value and create lock in.
Basically all of the major social media networks have APIs. They control them in different ways and prevent you from doing certain things while at the same time encouraging you to do other things to make a good user experience with the already public data. We will be using Twitters API for this project. You may not realize it but every time you use your phone, you’re using an API.
What You’ll Learn
After you make it through the entirety of the course, you will walk away knowing how to do the following:
- Setting up a virtual environment for your code
- Signing up as a Twitter developer
- How to Use Tweepy to call the Twitter API
- Building a Bot that Retweets our Likes
- Deploying a Bot using either AWS or Render
Please note this course requires MacOS. I do not have in-depth installation and setup instructions for Windows users.
-
1Course OverviewVideo lesson
Get a brief overview of the course to see if it's right for your next weekend project
-
2What You'll LearnVideo lesson
After you make it through the entirety of the course, you will walk away knowing how to do the following:
Setting up a virtual environment for your code
Signing up as a Twitter developer
How to Use Tweepy to call the Twitter API
Building a Bot that Retweets our Likes
Deploying a Bot using either AWS or Render
-
3PrerequisitesVideo lesson
Apologies in advance to Windows users but this course only runs through Mac examples. You don’t need amazing python skills but I do expect some basic knowledge. This means knowing how to write a for loop or function. If you don’t feel comfortable, spend some time learning the basics for free.
-
4ResourcesText lesson
-
5Setting Up Your EnvironmentVideo lesson
Situating your terminal window
Getting started with xcode
How to install homebrew
-
6Installing PythonVideo lesson
Downloading Python
Installing python with pyenv
-
7Creating A Virtual EnvironmentVideo lesson
Using virtual environments
Creating your own virtual environment
Activating your environment
-
8Modules & PackagesVideo lesson
Installing modules & packages through the terminal
Downloading a package
Installing via requirements.txt
-
9Visual Studio CodeVideo lesson
How to download Visual Studio Code
Opening project folder in VS Code
-
10Creating a Bot HandleVideo lesson
Signing up for twitter and create your bot account
Applying for a developer account
-
11Creating a New Twitter ApplicationVideo lesson
Filling out your app details
Creating authentication credentials
-
12Keys and Access TokensVideo lesson
3.3.1 What are they?
If you haven’t already learned, your API keys and access tokens should be guarded extra carefully. Keys and Access tokens are the credentials that tie developer apps to a user. These tokens allow apps we have authorized to make requests on our behalf.
API keys generally help with restriction, so you can only do certain things with certain keys, as well as traffic, so the platform knows how much you are using it. Sometimes there will be rate limits associated with a key so you can’t make a million calls an hour.
API keys are widespread and often used for their simplicity. You can generate API keys for specific services and then use it in applications or for requests you make through scripts. Since we are only accessing a single users data, API keys are going to be great for us.
3.3.2 Storing app keys and access tokens
There are some things we don’t want to share with others in our code. This usually includes usernames and passwords but also access tokens for APIs, like the credentials we just got from Twitter in our previous section.
We have the ability to define certain environment variables and store them in a separate file that we will make sure we don’t accidentally push to a place such as GitHub. This is where a .gitignore files comes into play.
You can generally create a gitignore with any github repo but let’s create ours manually. We are going to create the file by adding a new one and type `.gitignore` then we will fill it with the python default code from github
-
13Tweepy OverviewVideo lesson
Tweepy is an open source Python package that helps us access the Twitter API through Python. Tweepy abstracts away Twitter’s API endpoints through the use of classes and methods. Tweepy is built on top of the Twitter API so we get to use basically all of the API functionality but through an easier Python interface.
-
14Using TweepyVideo lesson
Authenticating with Tweepy
Testing credentials by printing user handle
Printing our first tweet using Tweepy
-
15Checking for LikesVideo lesson
Now that we are able to access Tweepy, we can go through and use some of the built in methods. Since our bot is going to check for likes and retweet them, we need to be able to do two things.
Check for Liked Tweets
Retweet the Liked Tweets
Over time various Twitter concepts have evolved so different, older names are used in Tweepy. Two to call out are a status is a tweet and a favorite is a like.
For learning purposes we are going to create a new files to test in and then we will convert it into one master file to make deploying easier.
-
16Retweeting LikesVideo lesson
How to retweet likes
-
17Converting to FunctionsVideo lesson
Converting authentication to a function
Converting retweet favorites to function
-
18Finalizing Our ScriptVideo lesson
How to bring it all together
-
19Adding to GithubVideo lesson
Finally we will want to add our code to github. I have found the best way to do this is following Githubs documentation
Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.
Open Terminal.
Change the current working directory to your local project.
Initialize the local directory as a Git repository.
$ git init
Add the files in your new local repository. This stages them for the first commit.
$ git add .
# Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.Commit the files that you've staged in your local repository
$ git commit -m "First commit"
# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.At the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL.
In Terminal, add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin remote repository URL
# Sets the new remote
$ git remote -v
# Verifies the new remote URLPush the changes in your local repository to GitHub.
$ git push -u origin master
# Pushes the changes in your local repository up to the remote repository you specified as the originRefresh github and take a look. We will need this if we decide to deploy with Render.

External Links May Contain Affiliate Links read more