1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Be Notified of New Twitter Updates by IM

Be Notified by IM


If you're anything like me, you check Twitter many times a day. However, this can be a problem because to do that, you have to stop what you're working on, switch to the Web browser, switch to the Twitter page and hit reload. Not that this is so much work, but it certainly breaks your train of thought. This script solves that by sending you an instant message using the Jabber protocol when there's something new to see on Twitter.

Jabber (also called XMPP, the XML Message Passing Protocol) is an open instant messaging protocol. Unlike protocols like AOL Instant Messenger or MSN Messenger, whose inner-workings are closed and secret, Jabber's inner workings are open. Also, no one entity runs the Jabber servers. In fact, you can run your own Jabber server and it will be part of the larger Jabber network! Google Talk is implemented using Jabber, so this script can talk to anyone using Google Talk. You can also get free Jabber accounts at places like jabber.org and a variety of IM clients support Jabber.

Section 1 has a few more variables than the normal set-up section. It has the Jabber ID your script will log in to, your Twitter username and password, and a list of people to notify with Jabber. Finally, it makes a Twitter::Base object to interface with Twiitter.

Section 2 is a method to send an update to everyone in your list. This is hidden away in a method for two reasons. Firstly, the Jabber API is somewhat wordy. Secondly, this code is called from two different places, and it's code that shouldn't be repeated. It's rather straightforward--set up a message with a recipient, subject, body, etc and send it too everyone on your list.

Section 3 connects to and authenticates with the Jabber server. The object for interfacing with Jabber is stored in the jab variable.

Section 4 gets the infinite loop started. It requests the first update on your friends timeline by using the count option to the timeline method so only one update is returned. This update is sent to the notify method, and the ID of the update is stored. This is important, since in the future, we'll request updates only posted later than the update with this ID.

Section 5 is similar to section 4, only put in an infinite loop. Instead of requesting just one update, it will request all updates since the last update it's seen. For each of these updates, notify people via Jabber and remember its ID. Sleep for a minute, then do the whole thing over again.

 #!/usr/bin/env ruby
 require 'rubygems'
 require 'xmpp4r/client'
 require 'twitter'
 include Jabber
 ### Section 1
 $jabber_jid = 'my-scripts-jid@jabber-server.com'
 $jabber_password = 'password'
 $twitter_username = 'your-username'
 $twitter_password = 'your-password'
 $notify_list = ['my-jid@jabber-server.com', 'my-friends-jid@jabber-server.com']
 twitter = Twitter::Base.new( $twitter_username, $twitter_password )
 ### Section 2
 # Notify all friends of an update
 def notify(jab, update)
   $notify_list.each do|n|
     to = n
     subject = "Twitter update"
     body = "#{update.user.screen_name}: #{update.text}"
     m = Message::new(to,body).
 ### Section 3
 # Connect to jabber server
 jid = JID::new($jabber_jid)
 jab = Client::new(jid)
 ### Section 4
 # Get the last update, notify it
 # and save its ID
 update = twitter.timeline( :friends, {:count =>1 } )[0]
 notify jab, update
 last_id = update.id
 ### Section 5
 # Loop forever, wait for updates, and
 # notify them
 loop do
   updates = twitter.timeline( :friends, {:since_id => last_id } )
   updates.each do|update|
     notify jab, update
     last_id = update.id
   sleep 60
  1. About.com
  2. Technology
  3. Ruby
  4. Networking
  5. Twitter--Ruby Script Be Notified by IM of new Updates

©2014 About.com. All rights reserved.