I'm creating a Twitter bot that will respond to tweets directed at its associated Twitter handle. I've seen documentation on how to filter the stream by hashtags, but I do not know how to filter by mentions. For example, if the Twitter handle associated with the bot is twitter_bot, I would like to do something like this:
listener = CustomListener()
stream = tweepy.Stream(OAuth, listener)
# Is there a parameter here that accomplishes this??
stream.filter(mentions=["twitter_bot"])
I would like to only handle the cases where someone tweets at the twitter_bot handle.
ie. "@twitter_bot how is it going?"
Thanks!
There is a REST API endpoint for the mentions and you can use it with tweepy, the documentation is outdated, the method was renamed to mentions_timeline. Here you have the method in tweepy: https://github.com/tweepy/tweepy/blob/master/tweepy/api.py#L79
With the following code and changing the keys/secrets you will get the mentions of your authenticated user:
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