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.
CONSUMER_KEY
CONSUMER_SECRET
ACCESS_TOKEN
ACCESS_SECRET
BEARER_TOKEN
Get these details from Twitter Developer's Dashboard
  • Create settings.py file with above values and put handles from which you want to retweet
CONSUMER_KEY = "<Enter your consumer Key>"

CONSUMER_SECRET = "<Consumer Secret>"
BEARER_TOKEN = "<Bearer Token>"
ACCESS_TOKEN = "<Access Token>"
ACCESS_SECRET = "<AccessSecret>"

# Handles for which you want to retweet
RT_HANDLES = ["AdhavSpeaks", "realpython"]
settings.py

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.
import tweepy
import settings
import random

# Authenticate to Twitter
auth = tweepy.OAuthHandler(settings.CONSUMER_KEY, settings.CONSUMER_SECRET)
auth.set_access_token(settings.ACCESS_TOKEN, settings.ACCESS_SECRET)
# Create API object
api = tweepy.API(auth)
TWEET_COUNT = 0

def rt(api, id):
    global TWEET_COUNT
    if TWEET_COUNT < settings.MAX_TWEETS:
        try:
            api.retweet(id)
            print("Retweeting: " + str(id))
            TWEET_COUNT = TWEET_COUNT + 1
        except:
            print("Already Retweeted: "+ str(id))
    else:
        print("Exiting Program Max Reached")
        exit()
    

for handle in settings.RT_HANDLES:
    tweets = api.user_timeline(id=handle, count=3)
    for tweet in tweets:
        if TWEET_COUNT < settings.MAX_TWEETS:
            rt(api, tweet.id)
        else:
            exit()
autoRT.py
  • 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
0 * * * * source ~/autort/bin/activate; /usr/bin/python autoRT.py
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.