Python, XMPP, Google Talk



Yesterday, my friend Hemanth and I were talking and he came to mention how easy it was to use Python and that a lot of things just require few lines of code. He also mentioned about the large amount of Python packages and modules available. Me, well, I am a cynic when it comes to Python. I’ve made my hands dirty using Python before and I didn’t like it. It is not that Python isn’t easy or “awesome”, but my old programming mind cannot imagine a code without curly braces! I love those curly braces that are used to specify the start and end of a block of statements. And even though one part of my mind tells me to move on and accept Python with two hands, I just can’t do it. But I thought it was cool when Hemanth mentioned about a script he had with him that can automate replies to GTalk.  I wanted something like that. And so I started digging up information about how to do this.

The first thing that I did, like any sensible person would do, was to Google! And voila! There were so many hits. GTalk, like many other, uses the Jabber/XMPP protocol. I went on a googling spree for some time, getting information about XMPP and Python packages or modules that provide a library for the protocol. I stumbled on a few. Some of my options included using the Google Apps SDK, JabberPy or XMPPPY. I settled for XMPPPY and quickly moved on to install the python-xmpp package on my Ubuntu machine!

I am not going to explain things that have already been explained pretty well in other places. So here are two links that would help you understand this article a bit more.

The XMPP Protocol

The XMPPPY Library

When it comes to Python, I’m a “noob”. So, even after installing the package, I had little idea on what I should do next. Sarath Lakshman of helped me through the article he wrote: Use XMPP to Create Your Own Google Talk Client. It was exactly the thing that I needed.  So taking the  information from the article and a lot of googling, I managed to implement a few things that I liked. So here it goes.

The first thing is how to connect using a username and password. For Google Talk, the username is your email id and the password is, well, the password of your Google account. The following code initialises the account and sends initial presence.

The connection.Process(1) is an infinite loops which processes responses. The code at this moment does not do much.

Quoted from LinuxForU : Switch the debugging on: change theconnection = xmpp.Client(server,debug=[]) line to connection = xmpp.Client(server) by removing the debug=[] parameter. Now when you run this script, you can see the XML stream.

Now let’s find out how to register a call back function so that we can reply when a message arrives.  The following code does this on line 10.

This code says to call the function named message_handler when a message stanza is received. The above code, if run, will result in an error since we have not defined any function of the name message_handler. So we define message_handler function.

The function accepts two arguments, the connection object and the received message node. We first print the message received. The getBody() method is defined for the Message object in XMPP.  We get a reply from the user and send the reply using the connection object. You could just define a static message and send it without requiring a user input. For eg., you can set up an away message. Simple! More information on XMPP Message Objects

Now since we have added a handler for message, why not add one for the Presence stanza. The Presence stanza informs about the change in availability/status of a user. We can add another handler like in the following code.

We now define the presence_handler function.

The getAttr() is a method of simplexml.Node object which is inherited by the presence object. The getShow() method returns the status (“away”,”chat”,”dnd”,”none”) of the user. More information about XMPP Presence Objects

Handlers can be attached to other stanzas like the IQ stanza and other stanzas defined in the XMPP protocol. Throw in a nice UI and some threads and you get a Google Talk client! That was easy. Well, of course, there is work to be done when it comes to doing such a project, but Python does make it easier, combined with the nice XMPPPY library.

In the end, I haven’t started loving Python, but I do find it useful. And though I would have to evolve  my mind to accept the Python principles, I sure hope that someday we become friends! Till then, I’ll keep experimenting doing silly programs, understanding Python and it’s awesomeness!

Seriously Crazy

3 thoughts on “Python, XMPP, Google Talk

  1. kartik


    I have implemented python-xmpp successfully. But I am looking for doing the same thing using username and access token. Without asking user to provide his/her password. Is this possible? Can you please guide me.

      1. Kartik

        Thanks for reply. I customized xmppy library to get that xouth2 thing work :) . But now I have another issue, how do i filter non-buddies and offline buddies in my account.

        rosterobject = C.getRoster()
        # I used the following loop just to check my contacts, it’s not need if you know the ID
        for i in rosterobject.getItems():
        print i

        Now how can I filter non-buddy so that I can send subscribe request ?. And how can we know whether particular jid is offline ?


Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">