python - on_direct_message is not listening the direct message streaming using tweepy
问题描述
The on_direct_message never called when a message arrives. i have used python3.7 and latest tweepy library. i n twitter account it works fine but not working with the code snippet am using. But the code snippet is working well to listen tweets.
twitter_stream=Stream(auth,StdOutListener())
print('Stream created...')
twitter_stream.filter(follow=[user.id_str], is_async=True)
NB:Permission taken for read, write and direct message. And all access parameters are correct
The StdOutListener is:
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 ):
print("Entered on_data()")
print(status, flush = True)
return True
def on_direct_message( self, status ):
print("Entered on_direct_message()")
try:
print(status, flush = True)
return True
except BaseException as e:
print("Failed on_direct_message()", str(e))
def on_error( self, status ):
print(status)
解决方案
Direct Messages are not supported in the Twitter streaming API (they were a part of the User Streams API, which was removed in 2018 and replaced with the Account Activity API).
To receive Direct Messages in realtime, you will need to implement a webhook handler for the Account Activity API. You could try the twitivity library, or look at this Python sample app. Tweepy does not have built-in support for this API.
推荐阅读
- django - 删除模型实例和错误“NoReversematch”
- php - PHP服务器无法在Mac终端上运行,我该怎么办?
- android - 如何在android studio中将位图数组发送到api
- inheritance - Angelscript 从数组中的对象调用覆盖函数
- sql - 检查不同的列
- unix - 两个值之间的Grep字符串Unix
- typescript - 如何使用 Inno Setup 删除准备安装界面?
- javascript - 如何在 reactjs 切换按钮中的单个 onclick 上执行两个不同的功能
- spring-boot - 查询第二页10000 elasticsearchresttemplate
- ruby-on-rails - 如何安全地使用参数变量来执行枚举搜索