Twitter Auto Reply To Tweets (Python)

Okay, so my last post dealt with searching with Twython and this lead to me to thinking; what about replying to tweets that mention the search terms. Which is why I’ve created a auto-tweet reply program!


Some things I considered when writing this program;

  • I wanted to reply to Tweets from a certain day range (in this case the last 24 hours) as I am thinking of running the script once a day (may need to change the formatted_date >= yesterday to get this working correctly.
  • I didn’t want to spam Twitter (same reason I added in a delay between sending tweets).

# geektechstuff
# Twitter Application

from twython import Twython
#Twython is a Twitter Python library

import datetime
import time

#imports the Twitter API keys

from auth import (

twitter = Twython(

# This posts Tweets
# message = " Test Tweet aka /'Hello World!/'"
# twitter.update_status(status=message)
# print("Tweeted: %s" % message)

# This searches Tweets

print('GeekTechStuff Twitter Search Python Program')
search_term = input('What word should I look for? ')

results = twitter.cursor(, q=search_term)

print('Searching Twitter...')

now =
now = str(now)
now = now.replace("-", "")
now = int(now)

for result in results:

    name = result['user']
    screen_name = name['screen_name']

    creation_date = result['created_at']

    tweet_txt = result['text']

    print('Twitter User:', screen_name)

    # Next bit is all about the date of creation
    # Bit long winded and there is probably an easier solution
    date_unformatted = creation_date
    month = date_unformatted[4:7]
    date = date_unformatted[8:10]
    year = date_unformatted[-4:]
    if month == 'Jan':
        month = '01'
    elif month == 'Feb':
        month = '02'
    elif month == 'Mar':
        month = '03'
    elif month == 'Apr':
        month = '04'
    elif month == 'May':
        month = '05'
    elif month == 'Jun':
        month = '06'
    elif month == 'Jul':
        month = '07'
    elif month == 'Aug':
        month = '08'
    elif month == 'Sep':
        month = '09'
    elif month == 'Oct':
        month = '10'
    elif month == 'Nov':
        month = '11'
    elif month == 'Dec':
        month = '12'

    formatted_date = year + month + date
    formatted_date = int(formatted_date)
    yesterday = now - 1
    if formatted_date >= yesterday:
        #This posts Tweets
        twitter_handle = '@' + screen_name
        message = twitter_handle+" I am an automated Python program from GeekTechStuff, thanks for the Twitter mention "
        twitter.update_status(status=message, in_reply_to_status_id=twitter_handle)
        print("Tweeted: %s" % message)
        #delay so that it doesn't look like the program is spamming Twitter



One thing I am going to need to do is create a several messages and then get the program to randomly chose which to send as otherwise Twitter ends the program (Twitter doesn’t allow the same tweet to be sent twice consecutively).



2 thoughts on “Twitter Auto Reply To Tweets (Python)

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.