RubyGem twitter direct message text - ruby

I start the Ruby and I want to do a bot who take the text of direct message received and tweet it. I use the twitter gem and I have no idea how to do it. Can someone help me ?
Tanks a lot
Aurélien

Good News! The twitter API allows for reading private messages.
The gem provides documentation on recieving a list of messages here.
Your call would look something like this:
direct_messages_received(options = {:count => 10})
This will return the 10 most recent DMs received.

Related

Replying to tweets on your timeline with twitter gem

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.
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.
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.

Ruby Gems Documentation

I'm just trying to understand how to use particular ruby gems. For example, take this reddit gem. It says to have this code to start:
require 'snoo'
# Create a new instance of the client
reddit = Snoo::Client.new
# Log into reddit
reddit.log_in 'Username', 'Password'
# Send a private message to me (Paradox!)
reddit.send_pm 'Paradox', 'Snoo rubygem rocks!', "Hey Paradox, I'm trying your Snoo rubygem out and it rocks. Thanks for providing such an awesome thing!"
# Log back out of reddit
reddit.log_out
Great but in the documentation you can see that the Client class doesn't have very many exciting functions. The exciting functions are in the Account class but there is no way to get to it...because if I try something like this
reddit = Snoo::Account.new
I get this error:
`initialize': undefined method `new' for Snoo::Account:Module (NoMethodError)
Okay so there's no new method but how do I make an Account object and use its functions like log_in?
Snoo::Account is a Ruby Module, and has been mixed in to Snoo::Client already by the gem. All the functions of Snoo::Account are already available to you on the reddit object.
The synopsis documentation in the readme doesn't make this very clear. But otherwise the documentation on the gem looks good to me.
Taking a short look at the source code on github makes me believe this is a fault in the documentation, as client clearly includes the functionality of many other modules, including the Account module you would like to access. In your example code, try the following methods to confirm it for yourself:
reddit.methods.sort
reddit.is_a? Snoo::Account
I assume the documentation software didn't catch the includes as they were executed using a block.

How do I get Twitter follow request with the Tweetstream Ruby gem?

According to this Ars Technica tutorial, "the streaming API makes it easy to detect when the user gets a new follower. To detect follow event objects, look for the "event" key and check if it has the string "follow" as the value."
http://arstechnica.com/information-technology/2010/04/tutorial-use-twitters-new-real-time-stream-api-in-python/2/
The tweetstream gem supposedly exposes all methods from the API. However I can't figure out, how to get to those follow requests!
Any ideas?
The tweetstream gem is fairly complete, but sometimes you need to delve into the source to find stuff:
client.on_event(:follow) do |event|
p event[:source][:screen_name]
end
https://github.com/intridea/tweetstream/blob/master/lib/tweetstream/client.rb#L375 :)

Api for google-api-client gem

How do I get the api of this gem? I could currently get the name and email of the user with
google_client.execute!(:api_method => GoogleLogic.get_google_oauth2.userinfo.get).data.name
google_client.execute!(:api_method => GoogleLogic.get_google_oauth2.userinfo.get).data.email
but no where are these methods written in the official page of this gem
http://code.google.com/p/google-api-ruby-client/
Since the ruby client is generated dynamically, it may not have API docs. You can see the API definition here:
https://www.googleapis.com/discovery/v1/apis/oauth2/v1/rest
It might be useful to you to see what methods/attributes are generated in the library.
Not sure if this is relevant anymore, but I found the documentation on the github page quite useful:
https://github.com/google/google-api-ruby-client
And also the samples:
https://github.com/google/google-api-ruby-client-samples
Lastly, the developers console in google is very useful.
http://console.developers.google.com/

Twitter/Facebook API for Ruby

I want to write a Ruby application through which:
I can submit tweets to twitter.
I can submit a post to facebook.
I can manage real-time stats of tweets
Is there any twitter/facebook api for Ruby?
I use the Twitter gem and am quite happy with it.
For Facebook, there is the Facebooker gem.
Streams of tweets:
Tweetmon is a great gem for keeping real-time track of tweets. Here's an example of using it to get a stream of tweets on a specific keyword
#!/usr/local/bin/ruby
if ARGV.size==1
keyword = ARGV.shift
else
puts 'tweetmon usage: tweetmon <keyword>'
exit 1
end
require 'yaml'
require 'rubygems'
require 'tweetstream'
config = YAML::load(File.open(File.expand_path('~/.twitter')))
user =config['username']
password =config['password']
TweetStream::Client.new(user,password).track(keyword) do |status|
puts "[#{status.created_at}-#{status.user.screen_name}] #{status.text}"
end
To use this gem you need:
gem sources -a http://gems.github.com
gem install intridea-tweetstream
To submit a tweet is just a HTTP POST - doesn't need any extra libraries to do this.
The Twitter API is a RESTful web service. It's completely language agnostic. Use whatever language you want.
Not sure about Facebook.
Two more libraries that didn't get mentioned yet:
twitter4r: http://twitter4r.rubyforge.org/
Twibot: http://cjohansen.no/en/ruby/twibot_a_microframework_for_twitter_bots_in_ruby
If you're inclined to retain more control over how you use the Twitter and Facebook APIs you can use the Wrest gem.
Take a look at the facebook (http://is.gd/bJspX) and twitter (http://is.gd/bJsqV) examples.
Also, while both the Twitter and Facebook APIs are HTTP APIs, they are not RESTful despite their claims to the contrary.
I would use this gem, it's really helpful https://github.com/moomerman/twitter_oauth
or just gem install twitter_oauth
MiniFB https://github.com/appoxy/mini_fb is excellent for Facebook API. But in both APIs you can do everything through HTTPParty and OAuth.

Resources