Wednesday 12 August 2009

Sending Google talk messages using Python

Google Talk uses the Jabber (XMPP) protocol, and I wanted to use it to send myself notifications from my home weather station and CurrentCost electricity meter.

I found a python module xmpp which looked like it should do the trick, and got it working as follows...

Installed the python xmpp egg by doing:
  • sudo python ./ez_setup.py
  • sudo easy_install xmpppy
(NB: sudo apt-get install python-xmpp works just as well for Linux Debian releases)

Tried the code in the article and comments here: http://www.larsen-b.com/Article/214.html but kept getting authentication errors.

Failed SASL authentification:

This article led me to try turning off SASL with the following line:

client.auth(user='myuser', password='password', sasl=0)

Which dumped some more helpful errors, and eventually made me realise that (for my gmail address) the domain expected in the xmpp.Client is not 'gmail.com' but 'googlemail.com'

I fixed up the paraemters passed to the xmpp.Client constructor, and the following code worked:

import xmpp
client = xmpp.Client('googlemail.com')
client.connect(server=('talk.google.com',5222))
client.auth(user='myuser', password='password')
client.send( xmpp.Message('markrp@gmail.com', 'Hello World'))

Now I have proper push notification to my IM client (Pidgin)