-1

I am trying to make a Twitter bot that will reply to tweet automatically but I do not want it to reply to tweets it has already replied to so I am trying to have the tweet IDs saved to a text document and have the bot reference it for the last seen ID but it is not updating the text document.

FILE_NAME = 'last_seen.txt'

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

def store_last_seen(FILE_NAME, last_seen_id):
    file_write = open(FILE_NAME, 'w')
    file_write.write(str(last_seen_id))
    file_write.close()
    return
tweets = api.mentions_timeline(read_last_seen(FILE_NAME), tweet_mode='extended')


for tweet in reversed(tweets):

    if '#botting' in tweet.full_text.lower():
        print(str(tweet.id) + ' - ' + tweet.full_text)
        api.update_status('@' + tweet.user.screen_name + ' This is a bot.', tweet.id)
        store_last_seen(FILE_NAME, tweet.id)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Zatreo
  • 1
  • 1
  • Is there a specific issue? Have you done any debugging? I would recommend reading https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. Please see [ask], [help/on-topic]. – AMC Sep 02 '20 at 19:52

1 Answers1

0

When I simplified the tweets for testing purposes, the program just overwrrote the previous entries. Repl here

So your writing program is not advancing past previous entries. I think you want "a" mode and not "w." You also want to probably put new lines or a break character in between entries so when scanning for a match you don't get false matches.

Python write to file without overwriting current txt

themagicbean
  • 156
  • 14
  • ... so if that helped please mark as answered. I know the community as a whole probably sees it as a basic, already answered question, but as one of few I could answer I'd appreciate the mark. – themagicbean Sep 03 '20 at 22:51