我如何使用tweepy回复推文?

时间:2017-07-31 04:07:53

标签: python twitter tweepy reply

我正在尝试使用tweepy和python创建一个Twitter机器人,但我无法弄清楚如何回复推文。

import tweepy
from Keys import keys
import time

CONSUMER_KEY = keys['consumer_key']
CONSUMER_SECRET = keys['consumer_secret']
ACCESS_TOKEN = keys['access_token']
ACCESS_TOKEN_SECRET = keys['access_token_secret']

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
auth.secure = True
api = tweepy.API(auth)

message = " Test message"

for tweet in tweepy.Cursor(api.search, q='search_item', lang = 'en').items(1):
try:
    print ("Found tweet by:@" + tweet.user.screen_name)
    api.update_status('@' + tweet.user.screen_name + message)
    print 'responded to @' + tweet.user.screen_name

    if tweet.user.following == False:
        tweet.user.follow()
        print ("following @" + tweet.user.screen_name)

except tweepy.TweepError as e:
    print(e.reason)
    time.sleep(3)
    continue
except tweepy.RateLimitError:
    time.sleep(15*60)
except StopIteration:
    break

我已经通过@username发布了一条推文,然后说出了这条消息但却无法回复。

2 个答案:

答案 0 :(得分:0)

那么,这很简单。我必须指明推文使用@符号的方式。

api.update_status('My status update @whoIReplyTo',tweetId) 

答案 1 :(得分:0)

我最终想通了。 回复推文的代码是:

api.update_status(status = "your message here", in_reply_to _status_id = tweet.id_str)