1

I have put together a very simple twitter bot using the "python-twitter" library. Basically, it waits for a user to tweet something to respond to it. To do this, I used a while loop which compares every minute the last tweet the user posted with the one stored when the program was launched. If the tweet is different, then we perform the action on it (a response in my case), otherwise we wait. And in both cases we update the tweet in memory with the most recent one to be ready to wait for the next tweet.

This method works but I was wondering if there isn't a more general way, which consists instead of doing loops, to wait for the reception of a single tweet with an event in order to solve problems like: The user tweets twice within a minute, in which case only the most recent tweet is considered. Or the maximum request amount for consulting a tweet is exeeded (which is 100 000 I think in the twitter API)

I am not trying to apply the solution particularly on this program but to learn about the subject (if it is possible only using Java Script or other for example) being unable to find more precise resources on the internet

Thanks

Glox
  • 11
  • 3
  • What do you mean by "If the tweet is different"? It sounds like you are comparing the latest tweet from one particular user with a tweet stored in memory. – Jonas Apr 26 '21 at 03:39
  • Yes that's it, to get the "A new tweet is posted" event. I don't see any other solution than to get (every minute say) the last tweet sent by the user. Except that the last tweet sent is not necessarily a new tweet all the time, that's why I compare it with the one from the minute before, if there is any difference, then the tweet has been posted at the last minute. I was just wondering if there is a way to directly receive a request from twitter whenever a tweet is posted, or something similar for any API. – Glox May 01 '21 at 16:31
  • User [/statuses/filter](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/filter-realtime/api-reference/post-statuses-filter) with the `follow` parameter set to the user. However, it looks like python-twitter does not support this endpoint. There are other python libraries that do. – Jonas May 01 '21 at 18:06
  • Yes that is what I meant, I will look into that, thanks ! – Glox May 02 '21 at 13:14

1 Answers1

0

An example using TwitterAPI.

from TwitterAPI import TwitterAPI
USER_IDS = ['204832963']
api = TwitterAPI(<consumer key>, <consumer secret>, 
                 <access token key>, <access token secret>)
r = api.request('statuses/filter', {'follow': USER_IDS})
for item in r:
    print(item['text'] if 'text' in item else item)
Jonas
  • 3,969
  • 2
  • 25
  • 30