Search code examples
pythontwittertweepy

Media Reply Tweet


So I'm trying to create a bot which replies every tweet, that mentions my bot, with media. The thing is, that whenever I execute this code:

FILE_NAME = "last_seen_reply.txt"

def read_last_seen(FILE_NAME):
    file_read = open(FILE_NAME, 'r')
    last_seen_reply_id = int(file_read.read().strip())
    file_read.close()
    return last_seen_reply_id

def store_last_seen(FILE_NAME, last_seen_reply_id):
    file_write = open(FILE_NAME, 'w')
    file_write.write(str(last_seen_reply_id))
    file_write.close()
    return

def reply_to_tweets_with_media():

    mentions = api.mentions_timeline(read_last_seen(FILE_NAME), tweet_mode = "extended")
    media = api.media_upload(filename = "pinto.png")
    mid = media.media_id

    for mention in reversed(mentions):
        if "@pintobot_" in mention.full_text.lower():
            print("Sending reply...")
            api.update_status(status = ('@' + mention.user.screen_name +' random text'),in_reply_to_status_id = mention.id)
            print("Reply was succesfully sent!")
            store_last_seen(FILE_NAME, mention.id)

while True:
    reply_to_tweets_with_media()
    time.sleep(5)

The reply is sent and you can actually see it in the tweet, but when I change api.update_status and include a media_id,

api.update.status(status = ("@" + mention.user.screen_name + " random text"), in_reply_to_status_id = mention.id, media_ids = [mid])

you can see the number of replies on the tweet, but you can't see the reply itself. Is it a twitter bug, or is my code wrong?


Solution

  • Your code is fine - This just seems to be a bug with Twitter as I have also experienced this long before I started using the Twitter API. Rest assured that if your code prints 'Reply was successfully sent!' and you can see the number of replies to the tweet, it will have sent. I think it is just a problem with Twitter itself so it's nothing to worry about.

    If you go to 'Tweets and Replies' under your profile in the Twitter Web App (or on iPhone, Android, whatever you want), you should be able to see that the reply is there if you want further confirmation.