I am using the tweetstream gem in order to communicate with the Twitter API. I am able to stream tweets, and won't mind switching to the regular twitter gem if I need to. I just want to get live updates of when a specific account posts. What is the best way of doing this in ruby?
Asked
Active
Viewed 377 times
1
-
Give a look to https://github.com/sferik/twitter#streaming – Aguardientico May 04 '18 at 04:04
-
@Aguardientico I have already checked that. Right now the best way I have figured out how to do it is to do `client.search("from:target_user", result_type: "recent").take(1).collect do |tweet| `which I am not sure will work. My idea is it takes any tweet from target_user and will then compare it from the last time it checks. Essentially an infinite loop until there is a difference then it breaks out. – evan May 04 '18 at 04:33
-
Are you creating the client with `Twitter::Streaming::Client` or with `Twitter::REST::Client` the former keeps an open connection to receive new data from twitter – Aguardientico May 04 '18 at 04:39
-
@Aguardientico using [tweetstream](https://github.com/tweetstream/tweetstream) which is just an easier way to consume the streaming API from twitter – evan May 04 '18 at 19:04
1 Answers
1
If you want live updates of when a specific account posts then take a look at webhooks. Webhooks allow you to build or set up Twitter App which subscribes to certain events on twitter.com. The advantage of using webhooks is that Twitter would send you a payload as a POST request to your app and then your job would to handle this response in your app accordingly. This assumes that you have an app server running locally, like rails server. The advantage is also that it happens asynchronoysly and you do not need to manually hit Twitter API every now and then to know if there were any updates

jedi
- 2,003
- 5
- 28
- 66
-
Would it be suitable (or in accordance with Twitter API TOS) to run the script, then have it hit a while loop, that just constantly sits there and waits to see if something comes in? I want to avoid rails (just cause). – evan May 06 '18 at 20:39
-
It's hard for me to say becasue I have never used it although by reading the gem's documentation I understand that it does what you want. Try to post some code examples here so that others can help you. Without code it's hard for anyone to say anything. – jedi May 06 '18 at 21:45
-
This URL might give you a hint: https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/connecting It looks like you need to connect the Twitter API using Oauth and hit proper endpoint. – jedi May 06 '18 at 21:55
-
@jedi with the webhook api how would you listen to a user's tweets for a user that cant and won't OAuth into your app. – CQM Jan 28 '19 at 20:12
-
@CQM From Twitter API Developer page 'As you add accounts for which to receive events, you will subscribe them using that account's access tokens. (...) Once your web app is ready, the next step is adding accounts to receive activities for.' That means that Twitter allows you to subscribe to listen for events within specific accounts. Does that answer your question? – jedi Jan 28 '19 at 21:29