Search code examples
pythontwittertweepyreply

How can i reply to a tweet using tweepy?


I'm trying to make a twitter bot using tweepy and python but I can't figure out how to reply to a tweet.

import tweepy
from Keys import keys
import time

CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
auth.secure = True
api = tweepy.API(auth)

message = " Test message"

for tweet in tweepy.Cursor(api.search, q='search_item', lang = 'en').items(1):
try:
    print ("Found tweet by:@" + tweet.user.screen_name)
    api.update_status('@' + tweet.user.screen_name + message)
    print 'responded to @' + tweet.user.screen_name

    if tweet.user.following == False:
        tweet.user.follow()
        print ("following @" + tweet.user.screen_name)

except tweepy.TweepError as e:
    print(e.reason)
    time.sleep(3)
    continue
except tweepy.RateLimitError:
    time.sleep(15*60)
except StopIteration:
    break

I've got it to post a tweet with @username and then say the message but can't get it to reply.


Solution

  • I figured it out eventually. The code for replying to a tweet is:

    api.update_status(status = "your message here", in_reply_to_status_id = tweet.id_str)