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.
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 LinuxForU.com 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.
user = "firstname.lastname@example.org" password="googlepass" jid = xmpp.JID(user) connection = xmpp.Client(server,debug=) connection.connect() result = connection.auth(jid.getNode(), password, "Some-Client-Name") connection.sendInitPresence() while connection.Process(1): pass
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 the
connection = 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.
user = "email@example.com" password="googlepass" jid = xmpp.JID(user) connection = xmpp.Client(server,debug=) connection.connect() result = connection.auth(jid.getNode(), password, "Some-Client-Name") connection.sendInitPresence() connection.RegisterHandler('message', message_handler) while connection.Process(1): pass
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.
def message_handler(conn_object,message_node): print message_node.getBody() message=raw_input("Enter reply : ") conn_object.send( xmpp.Message( message_node.getFrom() ,message))
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.
def presence_handler(conn_object,presence_node): print presence_node.getAttr("from") print presence_node.getShow()
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!