Python 使用Tweepy的自动直接消息响应
我目前正在为DM侦听器使用python中的tweepy包。我希望在收到发件人的信息后给他们回复。我有以下资料:Python 使用Tweepy的自动直接消息响应,python,twitter,wrapper,tweepy,Python,Twitter,Wrapper,Tweepy,我目前正在为DM侦听器使用python中的tweepy包。我希望在收到发件人的信息后给他们回复。我有以下资料: class StdOutListener( StreamListener ): def __init__( self ): self.tweetCount = 0 def on_connect( self ): print("Connection established!!") def on_disconnect( self,
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方法时,它会挂起。
奇怪的是,如果我自己发消息,当它循环时,我会收到大量消息。这是因为它在_data()上吗?我如何才能使此功能适用于其他发件人
更新:已解析-重新生成令牌并添加条件以检查发件人,本质上是将我自己列入黑名单。更新:已解析-重新生成令牌并添加条件以检查发件人,本质上是将我自己列入黑名单。谢谢。对于将来的读者,请记住在“权限”选项卡下启用对应用程序的直接消息的访问。之后,重新生成使用者和访问令牌,以使更改生效。旧令牌将导致401错误,仅更改它们而不更改权限是不起作用的。