4 # :title: Twitter Status Update for rbot
6 # Author:: Carter Parks (carterparks) <carter@carterparks.com>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2007 Carter Parks
11 # Users can setup their twitter username and password and then begin updating
17 class TwitterPlugin < Plugin
31 'X-Twitter-Client' => 'rbot twitter plugin'
35 # return a help string when the bot is asked for help on this plugin
36 def help(plugin, topic="")
37 return "twitter status [status] => updates your status on twitter | twitter identify [username] [password] => ties your nick to your twitter username and password"
40 # update the status on twitter
41 def get_status(m, params)
43 nick = params[:nick] || @registry[m.sourcenick + "_username"]
46 m.reply "you should specify the username of the twitter touse, or identify using 'twitter identify [username] [password]'"
50 # TODO configurable count
51 uri = "http://twitter.com/statuses/user_timeline/#{URI.escape(nick)}.xml?count=3"
53 response = @bot.httputil.get(uri, :headers => @header, :cache => false)
60 rex = REXML::Document.new(response)
61 rex.root.elements.each("status") { |st|
62 month, day, hour, min, sec, year = st.elements['created_at'].text.match(/\w+ (\w+) (\d+) (\d+):(\d+):(\d+) \S+ (\d+)/)[1..6]
63 debug [year, month, day, hour, min, sec].inspect
64 time = Time.local(year.to_i, month, day.to_i, hour.to_i, min.to_i, sec.to_i)
67 msg = st.elements['text'].to_s + " (#{Utils.secs_to_string(delta.to_i)} ago via #{st.elements['source'].to_s})"
68 texts << Utils.decode_html_entities(msg).ircify_html
72 m.reply "could not parse status for #{nick}"
75 m.reply texts.reverse.join("\n")
78 m.reply "could not get status for #{nick}"
83 # update the status on twitter
84 def update_status(m, params)
87 unless @registry.has_key?(m.sourcenick + "_password") && @registry.has_key?(m.sourcenick + "_username")
88 m.reply "you must identify using 'twitter identify [username] [password]'"
92 uri = "http://#{URI.escape(@registry[m.sourcenick + "_username"])}:#{URI.escape(@registry[m.sourcenick + "_password"])}@twitter.com/statuses/update.xml"
94 response = @bot.httputil.post(uri, "status=#{CGI.escape(params[:status].to_s)}", :headers => @header)
97 if response.class == Net::HTTPOK
98 m.reply "status updated"
100 m.reply "could not update status"
104 # ties a nickname to a twitter username and password
105 def identify(m, params)
106 @registry[m.sourcenick + "_username"] = params[:username].to_s
107 @registry[m.sourcenick + "_password"] = params[:password].to_s
108 m.reply "you're all setup!"
112 # create an instance of our plugin class and register for the "length" command
113 plugin = TwitterPlugin.new
114 plugin.map 'twitter identify :username :password', :action => "identify", :public => false
115 plugin.map 'twitter update *status', :action => "update_status", :threaded => true
116 plugin.map 'twitter status [:nick]', :action => "get_status", :threaded => true