Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replying to tweets on your timeline with twitter gem

Tags:

ruby

twitter

I am trying to create a simple reply from my twitter timeline but ran into some problems and as a ruby newbie I had problems in understanding the doc.

Twitter.mentions_timeline.each do |tweet|
    reply_to = tweet.from_user_name()
    #Twitter.update("@#{reply_to} Not today.")
end

This is what've written so far, which is not much. Unfortunately the tweet doesn't have any from_user_name when using the mentions_timeline it seems. I am using this gem and does not find any good examples of this: https://github.com/sferik/twitter

Would be very nice to have some nice example of how to grab ones tweets from the timeline and replying to those. Also does twitter have anything that distinguish "new tweets" or do I need to create some kind of storage myself to seperate new from old?

Thanks in advance :)

EDIT: to clarify, I want to get all tweets sent to me directly and reply to them. I don't really want to continue in any conversation.

like image 418
Ms01 Avatar asked Jul 11 '13 20:07

Ms01


People also ask

How does Twitter timeline algorithm work?

The algorithm considers several variables, such as the people you follow, your retweets and liked tweets, the popularity of the tweets, geography, timeframes, and tweet engagement. Consequently, when you log onto Twitter, you will see tweets that the algorithm thinks you'll be most interested in.


2 Answers

A little late, but in the lastest version of twitter gem you can reply to a tweet with:

@client.update("@#{reply_to.user.username} Not today.", in_reply_to_status_id: reply_to.id)

Hope this helps someone.

like image 199
joseramonc Avatar answered Dec 02 '22 14:12

joseramonc


Here are a few examples that might be helpful for anyone using sferik's twitter gem for something like this. Note, I'm using version 5.0.0 currently, so your mileage may vary.

Assuming you've configured the REST client and assigned it to a variable named client, you can get an array of your last 20 mentions (or however many you've received in the past 2 months, if fewer) with this line:

client.mentions_timeline.map(&:full_text)

The respective mentioners of those tweets can be acquired similarly with this:

client.mentions_timeline.map(&:user).map(&:username)

You could then use client.update as suggested to reply as you see fit.

One tool I find incredibly useful when I'm exploring API's in Ruby is pry. Just require it, drop a binding.pry, and run your code in Terminal. Then you can explore to your heart's — or API's limit's — extent.

like image 35
O-I Avatar answered Dec 02 '22 15:12

O-I