Adam
Adam

Reputation: 11

How do I get the tweet someone is mentioning my bot's username in reply to with Tweepy?

I am trying to create a Twitter bot using Tweepy that replies to mentions but needs to process the tweet the mention is replying to. I have everything else figured out except how to get the tweet the mention is replying to. I have looked through the documentation several times and have been unable to find a way to do this. Any help is much appreciated.

Upvotes: 1

Views: 591

Answers (1)

pythonista_ah
pythonista_ah

Reputation: 97

to have your bot reply to a user's screen_name is what I think is what you are asking here. You need to have:

  1. class MyStreamListener(tweepy.StreamListener)

  2. inside the class, function on_status(self, status) and on_error(self, status_code)

     class MyStreamListener(tweepy.StreamListener):
           def on_status(self, status):
               name = status.user.screen_name
               id_str = status.id_str
    
               message = " // < message > //"
               reply_tweet = "@" + str(name) + " " + message
    
               api.update_status(reply_tweet, in_reply_to_status_id= id_str)
               print(" <tweet received> " + status.text)
               print(" tweet sent back > " + reply_tweet)
    
          def on_error(self, status_code):
              if status_code == 420:
                 print("rate limit reached") 
                 return False
    

i would suggest having a timer in a while loop function as to avoid rate limits

Upvotes: 1

Related Questions