Tuesday, July 10, 2007

Twitter4R v0.2.0: Messaging API

Probably one of the more recent additions to the Twitter REST API has been that of direct messaging. In the Twitter web interface we were introduced to the supported construct of direct messaging only a couple of months ago. The Messaging API in Twitter4R provides access to these new supported features via two instance methods on the Twitter::Client class: #message and #messages.

Below we can see an example of a common non-trivial usage of this API:


gem('twitter4r', '>=0.2.0')
require('twitter')

client = Twitter::Client.new(:login => 'mylogin', :password => 'mypassword')
received_messages = client.messages(:received)
# Now do whatever it is you wish to do with the Array returned and assigned to received_messages.
# It is an Array of Twitter::Message. Twitter::Message has the following attributes:
# * sender (Twitter::User)
# * recipient (Twitter::User)
# * text (String)
# * created_at (Time)

new_message = client.message(:post, 'I am addicted to Twitter', 'myfriendslogin')
# Note: if we had a handle to our friend's Twitter::User object we could substitute that object
# for the screen name screen given (i.e. 'myfriendslogin' in the above example). This would
# look like:
# new_message = client.message(:post, 'I am addicted to Twitter', user)

# Now we realized sending that message was a big mistake, so we try to delete is quickly:
deleted_message = client.message(:delete, new_message)

To find out more about the Messaging API of Twitter4R visit the v0.2.0 RDoc and select the examples/messaging.rb link from the top left frame.

Also note that you can create new direct messages via the Model API of Twitter4R as well.

Happy direct messaging on Twitter using Twitter4R.

11 comments:

Hendy said...

For some reason I haven't yet been able to do anything with twitter4r 0.2.2:

ceefour@ojalanow:/media/prestige/home/ceefour/bin$ pm ahhhhh......
/usr/lib/ruby/gems/1.8/gems/twitter4r-0.2.2/lib/twitter/client/base.rb:34:in `raise_rest_error': Not Found (Twitter::RESTError)
from /usr/lib/ruby/gems/1.8/gems/twitter4r-0.2.2/lib/twitter/client/base.rb:39:in `handle_rest_response'
from /usr/lib/ruby/gems/1.8/gems/twitter4r-0.2.2/lib/twitter/client/base.rb:13:in `http_connect'
from /usr/lib/ruby/1.8/net/http.rb:543:in `start'
from /usr/lib/ruby/gems/1.8/gems/twitter4r-0.2.2/lib/twitter/client/base.rb:9:in `http_connect'
from /usr/lib/ruby/gems/1.8/gems/twitter4r-0.2.2/lib/twitter/client/messaging.rb:67:in `message'
from /home/ceefour/bin/pm:7
ceefour@ojalanow:/media/prestige/home/ceefour/bin$ vim pm
ceefour@ojalanow:/media/prestige/home/ceefour/bin$ pm ahhhhh......
/usr/lib/ruby/gems/1.8/gems/twitter4r-0.2.2/lib/twitter/model.rb:86:in `to_s': undefined method `respond_to' for #< Twitter::Message:0xb78fde70 > (NoMethodError)
from /home/ceefour/bin/pm:8:in `puts'
from /home/ceefour/bin/pm:8

My code is simply the same as here but with my login (email) & password.

weird. :-(

S. Potter said...

It looks like the Twitter.com website is being upgraded right now. When surfing to the website it gives me the message:

"Hey! We're letting loose a new feature or tweaking the existing ones! One second and you'll be back to your regularly scheduled Twitterin'."

S. Potter said...

Having said that, I am now able to run the Twitter4R sanity test that I usually only run right before doing a release.

My direct messaging code looks like:

require('twitter')
require('twitter/console')

version = Twitter::Version.to_version
puts "Sanity testing #{version}"
config_file = File.join(File.dirname(__FILE__), '..', 'config', 'twitter.yml')
twitter = Twitter::Client.from_config(config_file)

text = 'This is a test direct message for sanity test script purposes'
message = twitter.message(:post, text, 'mbbx6spp')

There were no external API changes between 0.2.0 and 0.2.2 except adding attributes, which would not have caused a 404 HTTP error from twitter.com server as your script seemed to have created.

S. Potter said...

Let me know if the problem persists though.

palash basu said...

no npw the folowin error is coming

wrong number of arguments (1 for 0)

RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/twitter4r-0.2.5/lib/twitter/console.rb:24:in `initialize'
c:/ruby/lib/ruby/gems/1.8/gems/twitter4r-0.2.5/lib/twitter/console.rb:24:in `new'
c:/ruby/lib/ruby/gems/1.8/gems/twitter4r-0.2.5/lib/twitter/console.rb:24:in `from_config'
#{RAILS_ROOT}/app/controllers/home_controller.rb:13:in `send_pm'
-e:4:in `load'
-e:4

S. Potter said...

@palash: it would help if you showed the code that exists in the controller this is coming from. The first thing to check is that the configuration file you are loading in the *from_config* actually exists and the correct path has been specified for it.

Without the calling code in the controller, I am unable to say any more.

palash basu said...

i am using the following code

require 'rubygems'
gem 'twitter4r', '>=0.3.0'
require 'twitter'

client = Twitter::Client.new(:login => 'anupks', :password => 'anup1234')
@messages = client.messages(:received)

getting the follwing error
uninitialized constant Twitter::Client

RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:263:in `load_missing_constant'
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:452:in `const_missing'
E:/ruzuku_v1/app/controllers/home_controller.rb:200:in `show_twitter'
-e:4:in `load'
-e:4


can tou help me out

S. Potter said...

@palash basu: There are two possible issues going on:

1. You might have the older less functional Twitter Gem installed, so when you do write the following:
require 'twitter'

It will load the older Twitter Gem Ruby code instead of the Twitter4R code. The work around for this currently is to remove the older Twitter Gem from your gem repository:
$ sudo gem uninstall twitter

2. Another possibility is that you do not have the initialization code in the environment.rb or another Ruby file that gets loaded before the HomeController. If the following code isn't already *in* the config/environment.rb, please add it to the bottom:
gem 'twitter4r', '>=0.3.0'
require 'twitter'

You should not need to use the "require 'rubygems'" line in environment.rb as Rails already loads it.

palash basu said...

Hi Potter

Thanks for your suggestion, but boss there is some other error coming

I am uninstall the older gem, an installing the newer by running the following command

$ sudo gem uninstall twitter

Again wite thetwo line code in environment.rb

gem 'twitter4r', '>=0.3.0'
require 'twitter'

but still i am getting the following error
ArgumentError in HomeController#show_twitter

wrong number of arguments (1 for 0)

RAILS_ROOT: ./script/../config/..
Application Trace | Framework Trace | Full Trace

c:/ruby/lib/ruby/gems/1.8/gems/twitter4r-0.3.0/lib/twitter/console.rb:24:in `initialize'
c:/ruby/lib/ruby/gems/1.8/gems/twitter4r-0.3.0/lib/twitter/console.rb:24:in `new'
c:/ruby/lib/ruby/gems/1.8/gems/twitter4r-0.3.0/lib/twitter/console.rb:24:in `from_config'
E:/ruzuku_v1/app/controllers/home_controller.rb:209:in `show_twitter'
-e:4:in `load'
-e:4

Please help

S. Potter said...

@palash basu: actually this is a *different* error. Which is good!

So it looks like the original problem was resolved and it was the older gem causing the problem.

I'll be look into this when I have my development laptop on, as my parent's desktop doesn't have Ruby installed:(

S. Potter said...

I totally forgot to respond to this over my Christmas vacation.

# in environment.rb
gem 'twitter4r', '>=0.3.0'
require 'twitter'

# in your controller action method
client = Twitter::Client.new(:login => 'anupks', :password => 'anup1234')
@messages = client.messages(:received)

I couln't reproduce any error at all using the code above. Can you make sure you have the code as described above?

I am using Twitter4R version 0.3.0 installed as a RubyGem locally.