2014-06-05 14 views
0

我創建一個Twitter的機器人將要針對其相關的Twitter的手柄鳴叫回應。我見過有關如何通過hashtags過濾流的文檔,但我不知道如何通過提及過濾。例如,如果與機器人相關的Twitter的手柄是twitter_bot,我願做這樣的事情:如何過濾流中提到

listener = CustomListener() 
stream = tweepy.Stream(OAuth, listener) 

# Is there a parameter here that accomplishes this?? 
stream.filter(mentions=["twitter_bot"]) 

我想只能處理其中有人鳴叫在twitter_bot手柄的情況下。

即。 「@twitter_bot怎麼樣?」

謝謝!

+1

我找不到任何東西,但你可能在這裏是源代碼https://github.com/tweepy/tweepy/blob/master/tweepy/streaming.py –

回答

1

對於提及有一個REST API endpoint,你可以使用它與tweepy,documentation已過時,該方法被重命名爲mentions_timeline。在這裏,您有tweepy方法:https://github.com/tweepy/tweepy/blob/master/tweepy/api.py#L79

用下面的代碼,並更改按鍵/祕密,你將獲得提到您的身份驗證的用戶:

import tweepy 

auth = tweepy.OAuthHandler(consumer_key='AAA', consumer_secret='BBB') 
auth.set_access_token('CCC', 'DDD') 
api = tweepy.API(auth_handler=auth, secure=True, retry_count=5) 

mentions = api.mentions_timeline() 
for mention in mentions: 
    print mention.id, mention.author.screen_name, mention.text