Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tweepy流式直接消息_Python_Python 2.7_Tweepy_Twitter Streaming Api - Fatal编程技术网

Python Tweepy流式直接消息

Python Tweepy流式直接消息,python,python-2.7,tweepy,twitter-streaming-api,Python,Python 2.7,Tweepy,Twitter Streaming Api,我一直在使用Tweepy和Python2.7来流式发送推文,一切都很好,除了在我向帐户发送直接消息时没有调用on\u direct\u message()方法。我已经更新了权限,甚至尝试使用on_data()方法,但它似乎无法检测发送到帐户的直接消息: import tweepy CONSUMER_KEY = '' CONSUMER_SECRET = '' ACCESS_KEY = '' ACCESS_SECRET = '' auth = tweepy.OAuthHandler(CONSUME

我一直在使用Tweepy和Python2.7来流式发送推文,一切都很好,除了在我向帐户发送直接消息时没有调用
on\u direct\u message()
方法。我已经更新了权限,甚至尝试使用
on_data()
方法,但它似乎无法检测发送到帐户的直接消息:

import tweepy

CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_KEY = ''
ACCESS_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True)

followed_accounts = ['account', 'account']
followed_ids = []

for account in followed_accounts:
    followed_ids.append(str(api.get_user(screen_name=account).id))


class StdOutListener(tweepy.StreamListener):

    def on_direct_message(self, status):

        author = status.author.screen_name
        api.send_direct_message(screen_name=author, text='response')

        return True

    def on_status(self, status):

        author = status.author.screen_name
        statusID = status.id

        print status.text + "\n"

        api.update_status('response')
        api.send_direct_message(screen_name='my username', text='Just sent a Tweet')

        return True

    def on_data(self, status):
        print 'Entered on_data()'
        print status
    
        return True
        
    def on_error(self, status_code):
        print "Error Code: " + str(status_code)
        if status_code == 420:
            return False
        else:
            return True

    def on_timeout(self):
        print('Timeout...')
        return True

if __name__ == '__main__':
    listener = StdOutListener()
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)

    stream = tweepy.Stream(auth, listener)
    stream.filter(follow=followed_ids)

向帐户发送直接消息不会出现错误,并且帐户在Twitter上正确接收消息。

您确定应用程序设置和访问令牌(在“密钥和访问令牌”选项卡中找到)的访问级别为“读、写和直接消息”吗?