ear-fung.us I'm a programmer. I'm also pro-grammar.

18Aug/09Off

Setting up Twitter Reply Push Notifications with Perl & Prowl

IMG_0099Here's a quick and dirty tutorial on how to set up push notifications from a bluehost server to get all of your @replies from twitter to be pushed to your iPhone using Prowl. Well, it's pseudo-push, seeing that we'll set up a cron job to run a script that pulls from Twitter and pushes to your iPhone. More on that later.

This tutorial will assume a few things:

  1. You have purchased the Prowl iPhone application and have it set up and working properly.
  2. You have a Bluehost account or other hosting environment that allows you to schedule cron jobs and install custom Perl modules. Installing Perl modules on Bluehost is done from the control panel, other servers are out of luck for the scope of this tutorial, as I have no way to know how their setup works.
  3. You are reasonably savvy at general computer and server things.

First task is to purchase Prowl for the iPhone and get it working. Do not proceed if you don't own this application. It is probably the best $3.99 I've spent on an iPhone app to date. Then generate an API key in your account settings on the prowl.weks.net site.

Next, you want to install some Perl modules in your environment. Again, these instructions are for Bluehost account holders.

  1. Log into your control panel (http://yourdomain/cpanel/) and click the camel icon that says "Perl Modules"
  2. Under "Install a Perl Module", type: "Net::Twitter" and click "Install."  This will take some time as the Net::Twitter has quite a few dependencies.
  3. Install the modules "WebService::Prowl" and "JSON::Any"

After all these modules are successfully installed, we need to write our script and upload it to the server. I believe in giving credit where credit is due, so thanks to ohnekontur.de for the code framework.

#!/home/earfungu/perl
BEGIN {
my $homedir = ( getpwuid($>) )[7];
my @user_include;
foreach my $path (@INC) {
if ( -d $homedir . '/perl' . $path ) {
push @user_include, $homedir . '/perl' . $path;
}
}
unshift @INC, @user_include;
}
use Net::Twitter;
use WebService::Prowl;
my $prowl = WebService::Prowl->new(apikey => "YOUR PROWL API KEY");
my $nt = Net::Twitter->new(
traits => [qw/API::REST/],
username => "YOUR TWITTER USERNAME",
password => "YOUR TWITTER PASSWORD"
);
open(LASTTWEET, "
my @IN = ;
my $lastid = $IN[0];
close(LASTTWEET);
eval
{
my $statuses = $nt->replies({ since_id => $lastid});
for my $status (@$statuses)
{
$prowl->add(application => "TwitterProwl",
event => "$status->{user}{screen_name}",
description => "$status->{time}\n$status->{text}");
print " $status->{time} <$status->{user}{screen_name}> $status->{text}\n";
$lastid = $status->{id};
}
open(LASTTWEET, ">lasttweet.id");
print LASTTWEET "$lastid"; #Write the data
close(LASTTWEET);
};
if (my $err = $@)
{
die $@ unless blessed $err && $err->isa('Net::Twitter::Error');
warn "HTTP Response Code: ", $err->code, "\n",
"HTTP Message......: ", $err->message, "\n",
"Twitter error.....: ", $err->error, "\n";
}

Save this script as "twitterprowl.pl" and upload it to your server.

Go to twitter and find your most recent tweet ID. You can do this by clicking the date and time of a tweet in your timeline. Look at the URL and the long number is the tweet id. Open a new text file and paste this tweet id. Save the file as "lasttweet.id", upload it to the same directory, and make sure that perl can write to the file. This most likely calls for CHMOD 766.

Run the command by SSHing into your serve,  traversing into the proper directory, and running: "perl twitterprowl.pl" to make sure there are no errors.

Set up a cron job to run every `n` minutes. How often is up to you, but make sure you are mindful of the Twitter API call limit of 150 calls per hour. My IP is whitelisted, so I can make 10,000 calls per hour, so I set up my cron job to run every minute.

You now have a pseudo-push notification service for replies to you on twitter delivered right to your iPhone!

One last thing: this script would be very easy to modify to include all of your timeline instead of just replies to you, but for those of you who are following hundreds of people, this might not be a great idea.

Let me know in the comments if this helped you, or how you modified the script to do something even cooler!


Update: After having this script running for about 15 hours now, I can say in the immortal words of Borat, "Great success!" Everything is doing what it should, and pushing only replies to Prowl on my iPhone.

Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.