6 Sept 2008

Getmail for twitter

Getmail a nice little Perl script that when setup with a cron job automatically scans your inbox on behalf of you or one of your twitter apps for emails from twitter and responds accordingly.

There are two types of email messages it responds to the first, is the email you get from twitter when someone sends you a direct message, the second is the one that twitter sends you when someone follows you.


How it works

The Getmail script works as an imap client and accesses the inbox of whichever twitter bot it is monitoring. It uses the Mail::IMAPClient perl module. Once it has successfully connected to the mail sever it selects a mail box, ie your inbox, or a specific mailbox of your choosing.

Once inside it looks for any recent mail. To put it another way it checks for any mail that has the imap RECENT flag attached to it. There is a slight problem with this method in that a message is only flagged as RECENT if no other imap clients have seen it, bare that in mind if you want to use this on your personal mail box, it will stop the script from seeing any mail.

When the script finds a message that is RECENT it checks the headers for the twitter custom headers (for example "X-Twitteremailtype"), and checks their value.

If the X-Twitteremailtype value is "is_following" then it calls a subroutine called follow. This subroutine doesn't do anything yet, it's up to you to fill it with commands. The most obvious would be to auto-follow.

If the  X-Twitteremailtype is "direct_message" then it calls an empty subroutine called directMsg, once again it's up to you the user of the script to define what that subroutine does.

Any other emails it finds in the inbox it just ignores, which is probably a good thing ;)


Possible Applications of this groovy script


You are free to do with this script what ever you wish, of course. This script will obviously be useful if you want to auto-follow people who follow you, or your twitter app. Some people may find use for the directMsg subroutine.

My reason for writting this script was so I co redesign echobot to auto-follow new users and to check direct messages without having to query twitter.


The Source Code

#!/usr/bin/perl

# Getmail.pl (For Twitter)
# By James Januszka, @jamesjanuszka, http://blog.jamesjanuszka.com
# Do as you will with this script but don't blame me if it f**k's up

use strict; # Spank me!
use warnings; # Stop or I'll shoot!
use Mail::IMAPClient;
my $host = "imap.smurfmail.net"; # Contact your email admin for this address
my $username = "pappa\@smurf.com"; # Don't forget to escape characters. Example \@
my $pass = "I <3 Smurfette"; # If you don't know this then, er ... Good Luck!
my $folder ="INBOX";# Which folder you want to look in. Example INBOX 
my $imap;

sub login{
 # login
  $imap = Mail::IMAPClient->new(
            Server => $host,
            User    => $username,
            Password=> $pass,
            )or die "Cannot connect to $host as $username: $@";
}

sub checkMail{
 # Check your mail
 $imap->select($folder) or die "Could not select: $@\n";
 my @recent = $imap->recent;# This checks for recent mail, if you check this mailbox with another client then run this script, it won't have any recent mail. Sorry thats the IMAP specification for you.
 foreach (@recent){
  my $messageType = $imap->get_header($_, "X-Twitteremailtype");
  my $messageFrom = $imap->get_header($_, "X-Twittersenderscreenname");
  my $messageString = $imap->body_string($_);
  if ($messageType eq "direct_message"){
   directMsg("$messageString");
  }
  if ($messageType eq "is_following"){
   follow($messageFrom);
  } 
 }
 
}

sub directMsg{
 # Do what you do with your direct messages here. 
 my @string = split(/\r/, "@_");# Split message email by returns, I used /r because /n was giving me problems
 print "$string[0] \n";
}

sub follow{
 # Do what you do when someone follows you. Ooh could this be a way of Auto-following someone ? ;)
 print "##########################FOLLOW @_#########################\n";
}


sub logout {
 #logout
 $imap->logout or die "Could not logout: $@\n";
}

# It's all very well writing pretty subroutines, but you need to remember to call them 
login;
checkMail;
logout;


picture credit gaetanlee