Twitter

Send Direct Message – Twitter4r

Posted in Ruby, Twitter, Uncategorized on February 12th, 2009 by Alan Hietala – Be the first to comment

I’ve been getting a lot of incoming search traffic looking for how to send a direct message using Twitter4r. Firstly RTFM if you can’t do that…

require('rubygems')
gem('twitter4r', '0.3.0')
require('twitter')
require('time')
 
login = "xxxxxx";
password = "xxxxxx";
 
#make the connection
 
twitter = Twitter::Client.new(:login =>; login, :password =>; password);
 
status = Twitter::Message.create(
   :text => 'I can\'t read RDOC',
   :receipient => 'anotherlogin',
   :client => twitter)

Hope that helps.

Twitter Bot Proof of Concept – Twitter4r

Posted in Ruby, Twitter on January 13th, 2009 by Alan Hietala – Be the first to comment

While at #geeklunch today the subject came up about spreading events via twitter but without having to follow absolutely everyone that might want to post an event. I proposed a solution of creating a mini twitter bot that would retweet anything that was sent to it via direct message from anyone on its white list. I’ve thrown together a little proof of concept and it works nicely. Took some digging as the Twitter4R documentation leaves something to be desired but it works quite nicely.

require('rubygems')
gem('twitter4r', '0.3.0')
require('twitter')
require('time')
 
#array of authorized users
@authUsers = ["alanhietala"]
login = "xxxxxx";
password = "xxxxxx";
#method to check if a user is authorized to post
def isAuthorized?(user)
 return @authUsers.include?(user);
end
 
 
 
# start main script
 
#make the connection to the bots account
 
twitter = Twitter::Client.new(:login => login, :password => password);
 
 
# get the private messages of the bot 
messages = twitter.messages(:received);
 
messages.each {|message|
  #TODO: process the message for commands
 
  #check if the sender is authorized to send messages
  if(isAuthorized?(message.sender.screen_name))
    #retweet the senders message
    status = Twitter::Status.create(
       :text => "RT "+ message.text,
       :client => twitter);
 
 
  end
 
 
  #delete the message from the server because its been processed
  twitter.message(:delete, message.id);
 
}

I’ll hack away at this some more and throw it into cron on a server some time later this week. Feel free to take this code and hack it to death.