I used to have some good working python that did auto-replies on the Tweepy stream listener, but due to changes in the Twitter API in August it no longer works.
I am re-building it by getting my most recent mention every 10 seconds (ideally it'd be less as I want to do near-instant replies), and checking if it was in the last ten seconds... if it was then the script assumes it's a new tweet and replies.
from tweepy import OAuthHandler
from tweepy import API
from datetime import datetime, time, timedelta
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
account_screen_name = ''
account_user_id = '897579556009332736'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterApi = API(auth)
mentions = twitterApi.mentions_timeline(count=1)
now = datetime.now()
for mention in mentions:
if now < (mention.created_at + timedelta(hours=1) + timedelta(seconds=10)):
print "there's a mention in the last 10 seconds"
# do magic reply stuff here!
else:
print "do nothing, no recent tweets/the last mention was more than 10 seconds ago so it isn't new"
This could work on a loop every 12 seconds; but any less and it hits the rate limit (i.e. this method above at 10 secs will hit the rate limit eventually)... so, is there a better way of just retrieving the most recent mention in order to do a reply based on the mention? I feel like I am probably doing it in a very inefficient way (e.g. this method actually gets the last 20 mentions!!) and the API probably has a better method that I could do more often without hitting rate limits?