I know I've been harsh on Twitter lately, but I have to admit that little by little I'm coming around to some of the cool things that you can do with it. One of those 'cool' things is to build bots.
I spent a little time today really digging into the details of just what you really need to do to build a Twitter bot. As it turns out it's VERY simple in lots of different languages! In fact, I built two very simple test bots already - one in Ruby and one in Perl. Both took only about 5 lines of actual code to some useful stuff!
Having jumped over the 'getting started' hump, I figured it was time to start building out a real Twitter bot. And what would be a better idea than to make my first 'real' bot a BotFu Twitter bot? The idea being that you'll eventually be able to send your BotFu game commands to twitter...and of course get your BotFu reactions via Twitter (think BotFu fight and playing via Twitter -- see Twitter can be used for cool things after all; who knew?!).
Anyway, the first problem that needed to be tackled (after the problem of having the idea and mapping out the basics of said idea) is how to get my bot's Twitter account to automatically follow anyone who follows it. Twitter requires that you are followed by an account before you can send a private Tweet to that account - so if I want to be able to send something like "d BotFu rnc" to the BotFu Twitter bot, BotFu first has to be following me.
So real quick here's the steps I'm taking to make this auto-follow happen...
1. Set up a Twitter account.
2. Within the settings area of your new account on Twitter there is a section called 'notices', make sure that you have 'Email when someone starts following me' checked and click save.
3. Set your email account up so that new emails get passed to the following Perl script (you'll of course need to be able to do this with your email provider or email software -- mine luckily supports this type of thing):
# Perl program to make the BotFu Twitter account follow a user
use Net::Twitter;
# your Twitter account information
my $username = "YOURUSERNAME";
my $password = "YOURPASSWORD";
# get the user we are going to start following from the subject line of the email sent:
if ($ARGV[0] =~ /(.+) is now following you on Twitter!/) {
# the user we want to follow should be the first match!
$user = $1;
my $twit = Net::Twitter->new(username => $username, password => $password);
# just in case we were following them already, we'll stop (before we start)
$t1 = $twit->stop_following($user);
# now we can start following the user
$t2 = $twit->follow($user);
}
That's all there is to it...now when someone decides to "follow BotFu", the BotFu Twitter bot will return the favor and follow them as well. Which of course means that the user will then be able to send 'direct' messages to the BotFu Twitter bot...and that's the first key to the whole idea!
The next step is also key though and that's the part of having our Twitter bot listen for direct commands and pass them off to the BotFu games as needed, responding appropriately of course...but I'll save the gory details of that for another post or two...



