Twitter4R v0.2.0: Messaging API

Written on 9:18:00 AM by S. Potter

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')

client = => '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.

If you enjoyed this post Subscribe to our feed


  1. Hendy |

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

  2. S. Potter |

    It looks like the 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'."

  3. S. Potter |

    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:


    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 server as your script seemed to have created.

  4. S. Potter |

    Let me know if the problem persists though.

  5. Anonymous |

    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'

  6. S. Potter |

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

  7. Anonymous |

    i am using the following code

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

    client = => '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'

    can tou help me out

  8. S. Potter |

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

  9. Anonymous |

    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'

    Please help

  10. S. Potter |

    @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:(

  11. S. Potter |

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

  12. CoolBox |

    Hey. Just found your gem and i have been playing around with it in my Console checking the functionality and everything.

    I have come across something though which is really confusing me. The gem works fine and brilliantly on my iMac at work but for some reason i keep getting the following error on my MacBook:

    ArgumentError: wrong number of arguments (1 for 0)
    from (irb):3:in `initialize'
    from (irb):3:in `new'
    from (irb):3

    The code i am using is:

    require 'twitter'
    require 'twitter/console'
    client = => "mylogin", :password => "mypassword") is then at this point that i get the error. As i say though i have played around with this in my Console on my iMac and it works fine!

    The only think i can think is that their might be some other gem or something conflicting with it, im not sure. Do you have any clues what this might be?

    Thanks again. :-)

  13. S. Potter |

    @CoolBox: yes, I think you are right - it is another gem interfering. When I first started Twitter4R the twitter gem was just a class called Twitter or TwitterClient or something (I forget). To namespace my work in my new gem (since the twitter gem was not very reusable at the time and only useful for CLI purposes) I created a module called Twitter to put my crap inside:) Since then the twitter gem has followed suit.

    The way to fix this would be remove the twitter gem (sudo gem uninstall twitter) or your might be able to get around it if you specify the following at the top of your file:
    gem('twitter4r', '>=0.3.0') # or whatever version you want to mandate

    I am not sure about the latter workaround, but it might work.


  14. McSvenster |

    Thank You Susan, for this work! I'm just a Ruby beginner and alway searching for new ways to learn about it. That's when I found Your twitter gem.
    I have a problem I cannot solve: when I try to send or reveive messagesI get the following error:

    NoMethodError: undefined method `parse' for Time:Class

    Twitter seems to get the request and to answer.

    Do You hav a hint for me?

  15. S. Potter |

    @McSvenster This is a known issue with Twitter4R v0.3.0 from Rubyforge. There are two solutions (one is really just a workaround):
    1. (The Workaround) Before doing require("twitter"), please add require("time").
    2. (The Preferred Solution) Install the GitHub development 0.3.1 version which has many new features. Follow instructions below:

    PS Please post future questions to the Google Group: twitter4r-users. Thanks!


Post a Comment