Marry Jane
Marry Jane

Reputation: 71

Event loop for Twitter4r

Is there an event loop for Twitter4R where when a user mentions the bot's handle, it will notify the bot?

I have tried to make it work with checking client.mentions_timeline every 5 seconds to see if there are new tweets that are mentioning the bot's handle. It worked, however, the rate limit often time gets exceeded after checking client.mentions_timeline numerous times. Is there a way that I can optimize this or am I missing something?

         6: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/users.rb:237:in `user'
         5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/users.rb:53:in `verify_credentials'
         4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:57:in `perform_get_with_object'
         3: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:72:in `perform_request_with_object'
         2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/utils.rb:50:in `perform_request'
         1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/request.rb:39:in `perform'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/twitter-6.2.0/lib/twitter/rest/request.rb:81:in `fail_or_return_response_body': Rate limit exceeded (Twitter::Error::TooManyRequests)

Thanks!

Upvotes: 1

Views: 20

Answers (1)

lacostenycoder
lacostenycoder

Reputation: 11226

The gem is just a wrapper for the API so refer to the Rate Limits on Twitter where we find this:

GET endpoints The standard API rate limits described in this table refer to GET (read) endpoints. Note that endpoints not listed in the chart default to 15 requests per allotted user. All request windows are 15 minutes in length. These rate limits apply to the standard API endpoints only, does not apply to premium APIs.

GET statuses/mentions_timeline  statuses    75  0

So it looks like you can only hit that endpoint 75 times per 15 minutes and the simple math for that is

15 * 60 / 75 = 12

So you probably should not hit it more than once every 12-15 seconds to be safe.

You could also keep track of your calls over time, rescue the exception and retry the request in the event that you hit the rate limit.

Upvotes: 1

Related Questions