我目前正在使用python中的tweepy包为DM监听器。我希望在接收到他们的消息时向发送者发送回复。我有以下内容:
class StdOutListener( StreamListener ):
def __init__( self ):
self.tweetCount = 0
def on_connect( self ):
print("Connection established!!")
def on_disconnect( self, notice ):
print("Connection lost!! : ", notice)
def on_data( self, status ):
status = str(status)
try:
json_acceptable_string = status.replace('\\','')
#string to dict
status=json.loads(json_acceptable_string)
if 'direct_message' in status.keys():
print '\n'
print status[u'direct_message'][u'sender_screen_name'] +' sent: '+ status[u'direct_message'][u'text']
message=str(status[u'direct_message'][u'text'])
api.send_direct_message(screen_name=str(status[u'direct_message'][u'sender_screen_name']),text='Out of office now - will respond to you asap')
print 'auto response submitted'
else:
#not direct message flow
pass
except:
#not important flows - couldn't convert to json/not correct flow in stream
pass
return True
def main():
global api
try:
auth = OAuthHandler(consumer_key, consumer_secret)
auth.secure = True
auth.set_access_token(access_token, access_token_secret)
api = API(auth)
print(api.me().name)
stream = Stream(auth, StdOutListener())
stream.userstream()
except BaseException as e:
print("Error in main()", e)
if __name__ == '__main__':
main()
出于某种原因,我可以看到用户的print语句及其发送的内容,但是当它到达send_direct_message方法时,它会挂起。 奇怪的是,如果我给自己发信息,我会收到一连串的消息。这是因为它的on_data()?我怎样才能让这个工作给其他发件人?
更新:已解决 - 已注册的令牌并添加条件以检查发件人,基本上将自己列入黑名单。
答案 0 :(得分:1)
更新:已解决 - 重新生成令牌并添加条件以检查发件人,基本上将自己列入黑名单。