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.