Google Voice and Prowl

Google Voice and Prowl

UPDATE: due to a wordpress bug, the pictures for this article are all missing from the server. i am quite angry about this but im going to leave the post as-is because the text is still relevant and useful.

UPDATE: As some of you may know, all of the Google Voice apps are now gone from the App Store (GV Mobile is on Cydia now), however this method still works as it does not rely on anything but Prowl on the iPhone side of things.

This solution makes use of Prowl, Python, some python libraries, and a desktop or server machine of some kind to act as the intermediary between Google Voice and Prowl’s API, so you will need to have some command line experience, and a machine available to run this software on.

You will of course need a Google Voice account, without which most of this article will be just an interesting read for you. They are free, but available by invitation only. Google appears to be rapidly sending out invitations to those who apply and may be pushing the service into the public very soon, but you can apply for an invite here.

You will also need the Prowl application for the iPhone, which costs $2.99 in the App Store, however it is the only iPhone App i am aware of that allows arbitrary push notifications to be sent using a published API, so it is an essential component to this setup, and an all around cool App to have.

If you do not have one of the native Google Voice iPhone apps, you do not technically need one to receive notifications, but they make it much easier to interact with the Google Voice service from your iPhone. I have been testing GV Mobile by Sean Kovacs and am quite happy with it so far.

So with your Google Voice account information in hand, and the Prowl application installed and working (you don’t actually need the prowl plugin here), lets go ahead and install the scripts needed to get this all working. I will be doing the tests on Ubuntu 9.04, but this should work equally well on any machine with a Python installation, including Mac OS X, Windows, and any of the BSDs.

You will need to make a directory somewhere on your machine, i called mine prowl-gv in my home directory. This is where all of the custom scripts and files will go, including your Google Voice account password, so make it someplace permanent and safe.

The scripts used here were written by multiple individuals. First, the Prowl python interface was written by Jacob Burch and the code is available on github here. You will need to download the prowlpy.py file and put it in your prowl-gv directory. If you copy and paste the code, be sure to keep the indentations intact as Python depends on them, simply copy/pasting from the web interface on github into a text editor may not work.

Second, the interface between Prowl and Google Voice was written by Mike Krieger, and is available on his github account here. Download those files and stick them in your prowl-gv directory as well, again being careful for indentations if you copy/paste the code.

And now on to the dependencies as needed on Ubuntu:

steve@server:~/prowl-gv$ sudo apt-get install python-beautifulsoup python-simplejson python-httplib2

UPDATE:Some users have asked how to install these things on OS X. The answer is to use MacPorts and install the following packages:
py-simplejson
py-beautifulsoup
py-httplib2

Once you have the dependencies worked out, you will need to make some text files in your prowl-gv directory. The first should be called prowlkey, and it should contain the API key for your Prowl account. You can get this by going to the prowl website, log in, and go to the settings page. Here you can generate the key and copy it into the correct file on your local machine.

Your Prowl API Key would be listed there

Your Prowl API Key would be listed there

The second file should be called credentials, in it you should put your Google Voice credentials, in the form of user password (just a space between them).

You will want to change the permissions for these 2 sensitive files such that they are only readable by your user account, the one you intend to run the scripts under. One could potentially be used to spam your phone using Prowl, the other could compromise your entire Google account, so chmod each of them to 600 to deny all access to everyone else on the machine:

steve@server:~/prowl-gv$ chmod 600 prowlkey credentials
steve@server:~/prowl-gv$ ls -l
total 32
-rw------- 1 steve steve   28 2009-07-25 09:10 credentials
-rw-r--r-- 1 steve steve 3670 2009-07-16 04:44 googlevoicenotify.py
-rw-r--r-- 1 steve steve  853 2009-07-16 04:44 prowlgooglevoice.py
-rw------- 1 steve steve   41 2009-07-25 09:10 prowlkey
-rw-r--r-- 1 steve steve 1877 2009-07-20 17:16 prowlpy.py

With all that done, you can test things by running python prowlgooglevoice.py. If it works, you will see this:

<blockquote>
steve@server:~/prowl-gv$ python prowlgooglevoice.py
checking
done checking, will sleep for  60  seconds

When Google Voice gets an SMS, a line will show up in the console, and your iPhone should get a push message. To give you an idea of what it currently looks like……

Real Google Voice notification

Real Google Voice notification

Currently, the Google Voice scripting will only retrieve SMS messages, however Google Voice users can already choose to get e-mail notifications of voice mails. The scripts appear to be in development, so in the future they may do voice mail notification as well. One feature that would be nice, would be for the SMS number to be resolved to a Google contact name, assuming we know the person and have entered a contact for them.

If you only wish to run the script as-is, in a terminal, no further work is required. If you wish to background the script or have it run on every reboot, you will need to check on how to do this for your specific operating system. On Ubuntu, running the script with a trailing & will background it (but it may not keep running if you close the terminal), and a simple cron job would allow you to run the script at every reboot, or a more complicated init script could be used (or for the truly geeky, an upstart job).

UPDATE: For those of you who wish to simply let your crontab execute the command every so often, i have modified the prowlgooglevoice.py script to be a “run once” sort of thing so cron does the looping, rather than let the python script loop itself:

<blockquote>
from googlevoicenotify import GoogleVoiceNotify
from time import sleep
import sys
from os.path import join
class ProwlListener:
        def __init__(self):
                PROWL_API_KEY = file(join (sys.path[0], 'prowlkey'), 'r').read().strip()
                import prowlpy
                self.prowl = prowlpy.Prowl(PROWL_API_KEY)
        def on_notification(self, event, from_name, message):
                self.prowl.add('Google Voice %s' % event,from_name, message)

class PrintListener:
        def on_notification(self, event, name, message):
                print "%s: %s said %s" % (event, name, message)

if __name__ == '__main__':
        prowl_listener = ProwlListener()
        print_listener = PrintListener()
        name, passwd = file(join (sys.path[0], 'credentials'), "r").read().split()
        gv = GoogleVoiceNotify(name, passwd, listeners=(prowl_listener, print_listener))
        gv.check()

Basically i modified the file checks in python so that they work regardless of the current working directory (which is different for cron), then stripped out the while loop to allow the script to run gv.check() once and then exit. Once you have that done, you can add a line in your crontab (run crontab -e to edit) that looks like this, replacing the path with the real path to your scripts:

<blockquote>
# m h  dom mon dow   command
*/1 * * * * "python /home/steve/prowl-gv/prowlgooglevoice.py"

This will cause your script to run every minute, check google voice for messages and then exit. You can insert any number of minutes you like in the cron job, the default for the script was every 60 seconds, but if you want it to check every 5 minutes, replace the 1 with a 5.