Search code examples
pythonapitwittertweepyreply

Reply to tweets in tweepy


So I have searched all over the place and all the solutions that were presented to me all posted a new tweet and didnt actually reply to the tweets I have collected. My goal is for the script to reply to the 2 tweets I retrieve but for some reason nothing works, I would really appreciate if anyone could help solve this.

while True:
    for tweet in tweepy.Cursor(api.user_timeline,
                   since='2017-12-24',
                   screen_name='something'
                   ).items(2):
                   try:
                       if not tweet.retweeted:
                               tweet.retweet()
                               m = "Something"
                               t = api.update_status(status=m, in_reply_to_status_id=tweet.id)
                               print("Something, Working...")
                               sleep(10)
                   except tweepy.TweepError as e:
                       print(e.reason)
                       sleep(5)
                       break

                   except StopIteration:
                       break

I tried "in_reply_to_status_id" as it was stated in the tweepy documentation but it doesn't work either, it simply tweets it out instead of replying.


Solution

  • When replying, prefix the @UserName to the status:

    while True:
        for tweet in tweepy.Cursor(api.user_timeline,
                   since='2017-12-24',
                   screen_name='something'
                   ).items(2):
                   try:
                       if not tweet.retweeted:
                               tweet.retweet()
                               m = "@UserName Something" # <---
                               t = api.update_status(status=m, in_reply_to_status_id=tweet.id)
                               print("Something, Working...")
                               sleep(10)
                   except tweepy.TweepError as e:
                       print(e.reason)
                       sleep(5)
                       break
    
                   except StopIteration:
                       break
    

    Notice that I set:

    m = "@UserName Something"
    

    with the @UserName prefix.