Home > Archives > 2007-11
2007-11
TwitterBot by Ruby
- 2007-11-29 (Thu)
This tutorial describes how to write your own Twitter bot by Ruby.
Plan
- Recieves DirectMessages, parses them and sends back responses.
- Keeps on being connected in Twitter via Jabber protocol.
- Written in Ruby !
Preparation
Twitter account
You need a twitter account for your bot.
Jabber ID
You need a Jabber ID for your bot to sit in Twitter. I use Jabber.JP service (free!), but you can choice whatever Jabber service you like.
If you use a GMail address for your bot, you can skip above step. (I could not connect to GTalk by GMail sub account, so took a different way)
Requirements
- XMPP4R
- gem install xmppr4r
Code
# Tiny Twitter Bot. # require 'rubygems' require 'xmpp4r' require 'kconv' class Reciever attr_accessor :client # user : Jabber ID # pass : Password def initialize(user, pass) @end = false Jabber::debug = true # connect Jabber client to the Server @client = Jabber::Client.new(Jabber::JID.new(user), false) @client.connect @client.auth(pass) @client.send(Jabber::Presence::new) thread = Thread.current # a callback to parse recieved messages @client.add_message_callback do |message| unless message.type == :error if message.body =~ /Direct from (.*):/ # who sends DirectMessage ? sender = $1 msg = message.body.split(/\r?\n/) msg.shift # cut Direct from ... msg.pop # cut last line, too body = msg.join # # do whatever you like in following lines :) # if body =~ /!abort/ # finish if told 'abort' @client.send(Jabber::Message::new(message.from, "d #{sender} aborting...").set_type(:chat)) thread.wakeup else # it just echoes in this example xxx = Kconv.toutf8("d #{sender}\n#{message.body}") @client.send(Jabber::Message::new(message.from, xxx).set_type(:chat)) end # if body end # if direct end # unless error end # callback @watcher = Thread.new do while not @end # saying "I'm alive!" to the Jabber server @client.send(Jabber::Presence::new) sleep 30 end end end def close @client.close @end = true @watcher.join end end # Reciever
After that, the only step remains is to create this Reciever class instance from main routine. To kill the bot, send Ctrl-C from the command line.
Let’s do some neat hacks at the lines parsing body by regexp.
Auto Follow
You need to follow your bot.
You may want your bot to counter-follow you automatically. I wrote a simple Procmail recipe and a tiny Ruby script. (original idea is にぽたん研究所::Twitter でイチイチ follow するのが面倒くさい
Procmail recipe
.procmailrc:
:0
* ^To:.your@bot.address
| counter_follow.rb
Ruby script to follow
counter_follow.rb:
#!/opt/local/bin/ruby require 'open-uri' ID = 'jabber bot ID' PASSWORD = 'jabber bot password' msg = ARGF.read msg =~ /\s+http:\/\/twitter.com\/(\w+)$/ screen_name = $1 exit 1 unless screen_name auth = {'Authorization' => 'Basic '+[ID+':'+PASSWORD].pack('m')} open('http://twitter.com/friendships/create/'+screen_name+'.json', auth)
Usage
Follow your bot from you Twitter account.
follow bot_account
Send a DirectMessage to your bot.
d bot_account hello bot !
Conclusion
Wrapped up a recipe how to create a tiny Twitter bot.
There were some pitfalls (it can hang up if you don’t send messages in ‘chat type’, cannot get any responses if you send something periodically, or …) in creating Twitter bot. I hope this tutorial help someone creating their own Twitter bot.
Any feedbacks make me happy
- Comments: 0
- Trackbacks: 0
WordPress CalendarCloud plugin
- 2007-11-29 (Thu)
What ?
A WordPress TemplateTag plugin that displays yearly calendars containing 12 months weighed by their post counts, like Tag Cloud.
Example

and, live demo is placed in the sidebar of this blog.
Download
calendar_cloud.zip (20KB)
Install
- Upload and unzip
calendar_cloud.zipto the/wp-content/plugins/directory - Activate the plugin through the ‘Plugins’ menu in WordPress
Use css to customize look & feel. See an example in style.css.
Usage
put following line in your template:
<?php calendar_cloud(); ?>
ChangeLog
- 0.2 : accordance with WordPress Coding Standard.
- 0.1 : initial release.
Feel free to send any feedbacks.
- Comments: 1
- Trackbacks: 0
Home > Archives > 2007-11

