Twitter direct message notifications with Prowl

If you want instant notifications of a direct message on Twitter on your iPhone, you have a few options. You could set up your mobile device to receive SMS messages and get them quickly, but you’re stuck paying for all of those incoming SMS messages. However, it’s annoying when you follow an account, and then they follow and DM you at 4AM. Twitter allows you to set quiet hours, but then you don’t see any notifications on your phone when you wake up.

You could always pick up Boxcar [iTunes Link] from iTunes and use it’s plug-and-play setup routine to begin receiving notifications of direct messages. The downside of the app is that the notifications are fairly delayed (sometimes 5-10 minutes late in my experience) and you’re paying $2.99 for an app that can only handle one Twitter account. Also, the app only handles Twitter direct messages and replies – nothing else.

Here’s where Prowl [iTunes Link] comes in. You can toss anything at the Prowl API and it comes up on your phone within seconds. The app is $2.99, but you can sent up to 1,000 notifications per hour through the API at no additional cost. Seriously – would you ever need 1,000 notifications per hour?

First, you’ll need two ruby gems for this project. The first of which is the prowl gem by August Lilleaas, and the second is tmail. If you haven’t installed gems from github before, you’ll need to run this first:

gem sources -a http://gems.github.com

Install the gems:

gem install prowl tmail

Once they’re installed, you can toss this script into /usr/local/bin/twitterdm-prowl.rb:

#!/usr/bin/env ruby
require 'rubygems'
require 'tmail'
require 'prowl'
 
message = $stdin.read
mail = TMail::Mail.parse(message)
 
prowlevent = mail.subject.gsub(/Direct message from /,'').strip
prowlbody = mail.parts[0].body.split(/\n\n/)[0].strip
 
Prowl.add('your-prowl-api-key-goes-here', {
  :application => "TWITDM",
  :event => prowlevent,
  :description => prowlbody,
  :priority => 0,
})

At the beginning of the script, we rope in rubygems, tmail and prowl. We’re going to read a message in from standard input (I’ll show you how that works in a moment) and then parse it with tmail. The subject and body are stripped down to make it easier to display on your iPhone’s screen. Finally, we wrap up by sending the data to the Prowl API with a normal priority.

As far as customizations go, you’ll obviously need to input your Prowl API key for the script to work. You can adjust the regular expressions to include different parts of the e-mail if you need them. Also, feel free to adjust the application name (“TWITDM”) in the script to something else. The priority can run from 2 (OMG emergency) to -2 (I don’t care). I chose 0 (normal) for my script.

Now, make sure the script is executable:

chmod 0755 /usr/local/bin/twitterdm-prowl.rb

We’ll need to set up an alias in /etc/aliases to feed in this e-mails to our script:

twitterdm:	 "|/usr/bin/ruby /usr/local/bin/twitterdm-prowl.rb"

Run newaliases to ensure that your alias ends up in the hashed aliases table.

At this point, you’ll need to ensure that your emails actually make it into the alias you set up. You’ll need something that can filter your e-mails and forward them based on the filter. If you want a strictly Linux solution, you could use procmail for that. If you’re e-mail provider has server-side filters that can forward e-mails (as mine does), that’s the easiest method. Twitter provides some great e-mail headers that you can use for filtering:

X-Twittercreatedat: Sat Sep 19 17:26:31 +0000 2009
X-Twitterrecipientid: 14453057
X-Twitterrecipientscreenname: RackerHacker
X-Twitteremailtype: direct_message
X-Twitterdirectmessageid: 383469494
X-Twittersenderid: 25000734
X-Twittersendername: Major Hayden
X-Twittersenderscreenname: rkrhkr
X-Twitterrecipientname: Major Hayden

I use X-Twitteremailtype: direct_message to filter and forward, but it’s up to you on how you do it.

Twitter direct message notification via Prowl

Twitter direct message notification via Prowl

Now, use another account to send yourself a direct message and you should get a notification pretty quickly (see the image on the right). If you didn’t get a notification, try going to the Prowl site and send yourself a test notification. If the test notification fails, then you may have an issue with Prowl itself (this hasn’t ever happened to me).

If the test notification works, but your Twitter notifications don’t, check your mail logs. Your filters may not be sending the direct message e-mails to your alias, your alias may not be configured properly, or the permissions on the ruby script you made may be incorrect.

If you want to keep testing without sending yourself a ton of direct messages, just send yourself one direct message. Once you receive the e-mail notification from Twitter, open it up and view the source of the message in your e-mail client (command-option-U in Apple Mail). Copy the entire source of the message to your clipboard, paste it into your favorite text editor on your server, and save it. You can call your script with the e-mail just like your mail server would with the following:

cat "twitterdmmessage.txt" | /usr/local/bin/twitterdm-prowl.rb

By going that route, you be testing the script itself, and your e-mail server will be removed from the equation.

If you’re still having trouble, let me know and I’ll be glad to help!

Comments

  1. says

    I really like the way you explained this. I actually got Boxcar yesterday because you mentioned it.. and am finding some limitations in the timing too, so this is good stuff to see.

    I don’t think I’d do it this way though, but instead use the ‘twitter’ gem to read the DMs /and/ responses (I rarely get DMs) and send those as notifications, but this post still gets me half the way on that :)

  2. says

    The Boxcar limitations were upsetting to me as well. Also, I think the interface could use some improvements.

    You could easily pair up the twitter gem with the prowl gem to get alerts on a regular interval. The only downside with that is that you won’t get instant notification for direct messages. You’d have to wait until the next interval when your script runs.

  3. says

    This is great for us developers. One area you didn’t bring up is notifications for @Mentions, which are streamed in real time when using Boxcar.

    We poll every 2-3 minutes depending on load for new DMs, but that’s a result of us playing nice with Twitter’s API. We also pickup any mentions sent from private accounts at the same time, since those don’t show up in the streaming API.

    We also have a few additional services forthcoming, along with a refocused application to move away from being so DM-heavy. You can read more about it here: http://www.techcrunch.com/2009/08/27/boxcar-20-is-a-must-have-for-the-iphone-its-the-best-push-notification-app-yet/

    Peter, keep in mind that if you’re going to be polling for @mentions you’ll end up using your own API limits, unless you’re whitelisted of course.

    best,
    jonathan (@jdg)

Leave a Reply

Your email address will not be published. Required fields are marked *