Auto RT (retweet) bot using Python tweepy
Twitter provides REST API to interact with it, you can create your own automations using these APIs.
You can directly access these APIs using curl requests but when it comes to simplicity we need to use some wrapped package, tweepy is one of them.
For more information about tweepy you can visit official documentation.
Problem Statement
I wanted to create a twitter bot which will periodically retweet tweets from particular handles.
Solution
We can create automation for retweet using twitter API and tweepy package, you just need to keep an eye on twitter API usage limits.
If you want to skip step by step guide, you can directly visit git for the solution code.
- Create Python virtual environment using virtualenv and activate it
# install virtualenv package
pip install virtualenv
# create new virtualenv named autort
virtualenv autort
# activate environment
source autort/bin/activate
- Install tweepy package in autort env
pip install tweepy
- Goto twitter developer portal and get below details for your application. I will write a separate post on how to create twitter developer app.
- Create settings.py file with above values and put handles from which you want to retweet
Note: Above all variables should be secret and you should not check-in these in any public repositories.
- Now create autoRT.py file containing executable code with tweepy like below.
- Place both settings.py and autoRT.py file in same directory and run this code using below command
python autoRT.py
- Once you see successful retweets on your account you can go for scheduling this in a crontab for every hour
- You can host this code on any server if you already have a server for your website/application. I hosted this on one of my aws server which runs similar code every hour.
You can visit git for full source code.