我有一个Twitter机器人正在追踪一个特定的帐户。当该帐户发送推文时,我想向我的机器人发送推文。
到目前为止,我有下面的代码:
import tweepy
import time
import sys
import inspect
consumer_key = 'xxxxxxx'
consumer_secret = 'xxxxxxxx'
access_token = 'xxxxxxx'
access_token_secret = 'xxxxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth.secure = True
api = tweepy.API(auth)
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.user.screen_name.encode('UTF-8').lower() == 'xxxxxx': #twitterID of account I am following, if it tweets, my bot should tweet
api.update_status("Test Tweet") # tweet that is sent from my bot
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=MyStreamListener())
myStream.filter(follow=['xxxxxx'])#twitterID of account I am following
但是,当我从命令行运行此代码时,它会运行一秒钟,然后不返回任何内容-我的机器人没有鸣叫。
似乎if
方法中的on_status
语句未正确缩进。
如果不是这种情况,要正确调试,您需要在on_error
类中添加MyStreamListener
方法,以便能够确定Twitter的返回的错误/状态代码(如果有) API。
请参见the Handling Errors section of the Streaming With Tweepy documentation。