Twitter Like and Retweet Bot
A Twitter bot written in Python using Tweepy and hosted on a server. It will like and/or retweet tweets that contain single or multiple keywords and hashtags.
Apply for Twitter Developer Access with the account you want the bot to be used for.
Create a new Twitter Application to generate your private keys, secrets, and tokens.
- Make sure the app settings has Read and Write permissions.
- Create a file named
credentials.pyto hold the private information using the format below.
- See File Structure for where the file should be placed.
TWITTER_API_KEY="xxxx" TWITTER_API_KEY_SECRET="xxxx" TWITTER_ACCESS_TOKEN="xxxx" TWITTER_ACCESS_TOKEN_SECRET="xxxx"
Adjustments you can make in
config.pyto tweak the bot to your liking. (Keep in mind the TwitterAPI search index has a 7-day limit, no tweets will be found for a date older than one week.)
- search_keywords - Keyword(s) and/or hashtag(s) that you want to retweet
- delay - Time to wait between processing requests in seconds
- Please be aware of the TwitterAPI rate limits
- result_type - Specify what type of search results you want to get
- "recent", "popular", or "mixed"
- number_of_tweets - Specify the number of tweets you want the bot to iterate through
- run_continuously - Set True if you want the bot to run continuously
- Also set True if you will be deploying the script
- retweet_tweets, like_tweets - Adjust booleans for whether you want to only retweet, only like, or do both
Run the script. Enjoy your Twitter bot!
Twitter-Retweet-Bot |-- config.py |-- credentials.py |-- requirements.txt |-- twitter-bot.py
Here is a tutorial on how to deploy / host the bot on a server.
- Create a free Beginner PythonAnywhere account.
- Go to Files, create a new Directory, upload the three
- Create a new bash console on your Dashboard and run pip with your python version to install tweepy
pip3.8 install --user tweepy
- Copy the file path, go to Tasks, enter the UTC time you want the script to run at, and enter the python version and file path with
twitter-bot.pyat the end.
- After the task runs as the scheduled time, you can check the task log to see the bot running
Amazon Web Services
- Launch an EC2 instance on Amazon Web Services.
- See Additional Information for more details.
- Load the key-pair file (.pem) into PuTTYgen (which was downloaded when you installed PuTTY) and save the private key as a private key file (.ppk).
- Connect to your instance on WinSCP.
- The host name is [email protected][public DNS here].
- Click Advanced, go to Authentication under SSH, and load the previously generated private key file (.ppk).
- Login to the session.
- Use WinSCP to transfer the project's .py files to the server.
- Connect to your instance on a bash command line using one of the following ways.
- Install python and pip to the server on the bash command line.
sudo apt update sudo apt upgrade sudo apt install python3 sudo apt install python3-pip pip3 install update pip
- Check if python and pip have been installed correctly.
python3 --version pip3 --version
- Install tweepy to the server.
pip3 install tweepy
- Run the script. Enjoy!.
- See Additional Information for details on running the script continuously.
- I used the screen option.