Building a Twitter mailbot
I recently built a very simple Twitter mailbot for use on charlottebserver.com. Check it out: http://twitter.com/obs_election
The requirements were simple: Use a designated email address to send messages directly to a Twitter account.
Why email?
- It’s safe to say that everyone in the newsroom knows how to use email
- No need to share/learn a username/password for the account
- The Twitter interface can be bypassed (although it’s SO easy to use)
- You can only tie one SMS enabled device to a Twitter account and many folks would be using this account.
So… exactly how do you do it? Well, you need to establish a few things first:
- Get a twitter account.
- Create an email address that allows you to filter your emails through a script. Observer Interactive uses shared hosting at pair Networks (I would certainly recommend this company) and they allow us to set this up very easily
- Write a script to process the mail.
You should take a look at the Twitter API documentation. In particular, check out the link titled ‘The Easiest Way to Play Around With the Twitter API’ — it basically gives the secret away (and damn, it is easy).
Using PHP and it’s built in cURL functions all you need to do is parse the email, check the address to see if it’s someone we allow (we check for @charlotteobserver) and then use cURL to post it to Twitter’s API.
I hate to re-invent the wheel so I found a PHP class that parses email (although I had to make a fix to it to allow for multipart/alternative content-types) and someone had already written a function called ‘postToTwitter’ (how perfect is that!). I pieced it all together in no time flat.
The script is available (as plain text) here.
Any questions? Please send them my way.