I,ve made a bot to reply followers whenever they call me with key words. however the reply only appears on my account. The person who got replied doesnt see the reply or gets notified
import tweepy
import random
import time
CONSUMER_KEY = 'xxxxx'
CONSUMER_SECRET = 'xxxxx'
ACCESS_KEY = 'xx-xx'
ACCESS_SECRET = 'xxxx'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
max_tweets = 20
list_delirio = ['coisa escrita', 'yes']
list_isso = ['ok', 'nope']
list_acabou = ['bye', 'see you']
delirio = random.choice(list_delirio)
isso = random.choice(list_isso)
acabou = random.choice(list_acabou)
def get_id():
with open('ultimoid.txt', 'r') as f:
ultimoid = f.read()
return ultimoid
def salva_id(novo_ultimo_id):
with open('ultimoid.txt', 'w') as f:
f.write(str(novo_ultimo_id))
def responde():
ultimoid = get_id()
ids_pegos = []
time.sleep(20)
try:
for tweet in tweepy.Cursor(api.mentions_timeline, since_id=ultimoid).items(max_tweets):
ids_pegos.append(tweet.id)
user_name = tweet.user.screen_name
status = api.get_status(tweet.id)
if 'frota' in status.text.lower():
api.update_status('@' + user_name + '\n hello...', in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
elif 'hello' in status.text.lower():
api.update_status('@' + user_name + '\n wtf!', in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
elif 'delírio' in status.text.lower():
api.update_status('@' + user_name + '\n' + delirio, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
elif 'é isso?' in status.text.lower():
api.update_status('@' + user_name + '\n' + isso, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
time.sleep(6)
else:
api.update_status('@' + user_name + '\n' + acabou, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True)
time.sleep(6)
salva_id(max(ids_pegos))
except Exception:
time.sleep(30)
pass
if __name__ == '__main__':
while True:
responde()
time.sleep(30)
I'm trying to understand whats the problem. I've already put
in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=True
as documentation says, but still not working
I've found solution:
the line
status = api.get_status(tweet.id)
shouldn't be there. So everystatus.text.lower()
should betweet.text.lower()
But the most important, the code must work with strings instead of intergers on Id's, so every
tweet.id
staythat consequently changes
salva_id(max(ids_pegos))
tosalva_id(ids_pegos[0])