Twitter Bot

a tiny Twitter bot skelton written in Ruby, using Jabber API.

code

lang/ruby/twitter_bot

see also


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

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

lang/ruby/twitter_bot/counter_follow.rb

Usage

  1. Follow your bot from you Twitter account.

follow bot_account

  1. 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.