-1

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

A Neto
  • 233
  • 2
  • 10
  • 1
    This doesn't make much sense. You haven't defined `tweet.id` or `user.id`, and even if you did, they wouldn't work. Use underscores `_` instead. It's not replying to the person because your script isn't identifying anyone. You have to fetch `tweet_id` and `user_id` from the tweet JSON. I suggest you read the Twitter documentation, rather than just Tweepy's: https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/overview/tweet-object – pigeonburger Sep 29 '20 at 05:40

1 Answers1

0

I've found solution:

the line status = api.get_status(tweet.id) shouldn't be there. So every status.text.lower() should be tweet.text.lower()

But the most important, the code must work with strings instead of intergers on Id's, so every tweet.id stay

tweet.id_str

that consequently changes salva_id(max(ids_pegos)) to salva_id(ids_pegos[0])

A Neto
  • 233
  • 2
  • 10