A project idea came to me today because of a predicament. I am beginning an internship, and I was told that I could not access personal email while working. I unfortunately do not own a smartphone, so this really meant that I would be away from my precious Gmail for at least 8 hours a day. I do have an unlimited text messaging plan, though, so I thought it would be great if I received a text message every time a new email came to my inbox.
The service that immediately came to mind for sending SMS was Google Voice. I was lucky to find an excellent API for Google Voice written in Python. With this, sending an SMS is literally 7 lines of code.
I also needed a way to find any new unread messages. I could parse Gmail's Atom feed and keep track of the most recent unread message, but then I would need to deal with login credentials. I am no Python expert, and I was too lazy to look up how to do this.
I realized, though, that I was already using a program called gm-notify with Ubuntu 10.04 to notify me of new unread emails with the messaging icon. I also realized that this program was written in Python. Time to hack.
I edited the gm-notify.py file in /usr/bin and added a new send_sms() method to the CheckMail class. The method simply copied the code from the PyGoogleVoice SMS example to send a text message (a minor change I made was to use the Gmail credentials that gm-notify already retrieved from the GNOME keyring; all I had to do was read the username and password from the self.creds tuple). gm-notify nicely concatenates the sender and subject of every email it hasn't already notified about into a string. I simply call send_sms() every time the program launches a notification bubble because of a new string.
In the end, I've written about 1 line of code and reused the rest. It has also taken far longer to write this blog post than to research and implement the whole hack. In my limited testing so far, though, it's worked well. The caveat is that I have to leave my laptop on and running Ubuntu, but hopefully I can get a server someday and avoid that. Or I could use that money and get a data plan. In any case, I'm pretty amazed with what can be done with mashing some open-source Python code together. Now I can happily avoid inbox withdrawal...
The service that immediately came to mind for sending SMS was Google Voice. I was lucky to find an excellent API for Google Voice written in Python. With this, sending an SMS is literally 7 lines of code.
I also needed a way to find any new unread messages. I could parse Gmail's Atom feed and keep track of the most recent unread message, but then I would need to deal with login credentials. I am no Python expert, and I was too lazy to look up how to do this.
I realized, though, that I was already using a program called gm-notify with Ubuntu 10.04 to notify me of new unread emails with the messaging icon. I also realized that this program was written in Python. Time to hack.
I edited the gm-notify.py file in /usr/bin and added a new send_sms() method to the CheckMail class. The method simply copied the code from the PyGoogleVoice SMS example to send a text message (a minor change I made was to use the Gmail credentials that gm-notify already retrieved from the GNOME keyring; all I had to do was read the username and password from the self.creds tuple). gm-notify nicely concatenates the sender and subject of every email it hasn't already notified about into a string. I simply call send_sms() every time the program launches a notification bubble because of a new string.
In the end, I've written about 1 line of code and reused the rest. It has also taken far longer to write this blog post than to research and implement the whole hack. In my limited testing so far, though, it's worked well. The caveat is that I have to leave my laptop on and running Ubuntu, but hopefully I can get a server someday and avoid that. Or I could use that money and get a data plan. In any case, I'm pretty amazed with what can be done with mashing some open-source Python code together. Now I can happily avoid inbox withdrawal...
No comments:
Post a Comment