Search code examples
pythontwitterprintingbotsstatus

How to use the most recently printed line as an input?


I am trying to create a Twitter bot that posts a random line from a text file. I have gone as far as generating the random lines, which print one at a time, and giving the bot access to my Twitter app, but I can't for the life of me figure out how to use a printed line as a status.

I am using Tweepy. My understanding is that I need to use api.update_status(status=X), but I don't know what X needs to be for the status to match the most recently printed line.

This is the relevant section of what I have so far:

from random import choice
x = 1
while True:
  file = open('quotes.txt')
  content = file.read()
  lines = content.splitlines()
  print(choice(lines))
  api.update_status(status=(choice(lines)))
  time.sleep(3600)

The bot is accessing Twitter no problem. It is currently posting another random quote generated by (choice(lines)), but I'd like it to match what prints immediately before.


Solution

  • Instead of directly printing a choice:

    print(choice(lines))
    

    create a new variable and use it in your print() and your api.update_status():

    selected_quote = choice(lines)
    print(selected_quote)
    api.update_status(status=selected_quote)