Categories
Python

Building a Jabber Bot (Google Talk) with Python and Google App Engine

To develop a Jabber / Google Talk Bot, we first need to enable the XMPP inbound service for the application. Just add the lines:

to your app.yaml file. That would activate the service for our application. Have a look at my application’s app.yaml file:

Now, we have the app configured, we need to do the actual coding. Every GAE app has it’s own IM id: [application_id]@appspot.com 🙂 There are other extensive IDs also available but for this task we will use this format. So, my app has the ID: masnun-test@appspot.com as it’s Jabber / Google Talk ID. If the user adds this ID to his/her contact list, it’ll auto accept the friend invitation and appear online. Now when he/she sends a message, the message is sent as a POST request to the url: /_ah/xmpp/message/chat/ . This URL is not publicly accessible without admin rights. Our app can handle this URL like any other URL. So we need to setup a request handler for any POST request to this URL.

GAE has a nice API for it’s XMPP service which lets us build a Message object directly from the POSTed data. We will do that. Extract the message parts and send a response back. Here is source code for “masnun.py”:

Categories
Python

Handling JSON in Python

On php, I just use json_encode() and json_decode() to handle JSON data with ease. Python has a built in module named “json” for the same purpose. Here’s the link for the official documentation: http://docs.python.org/library/json.html 🙂 The documentation has pretty examples. Still, I wanted to try things out by myself. I used Python to parse the JSON output of a certain Yahoo! Pipes feed :

Output:

Categories
Python

Twitter Streaming API with Python

Hasin Vai has a good post about collecting tweets in real time with php. Here’s the post: http://hasin.wordpress.com/2009/06/20/collecting-data-from-streaming-api-in-twitter/ 🙂 If you are a php developer look into there.

Well, I am going to cover Python for the Twitter streaming API. You need the “tweetstream” python module which depends on the “anyjson” module. Please make sure you have these modules installed before you proceed.

Download Links: anyjson | tweetstream

To install these packages, use the built in setup.py of the individual packages :

In my case, I didn’t have the “setuptools” python module installed with my default python installation. So I had to install it to get the setup.py work:

I love the magic of debian packages 🙂

Now, you can install the modules and dive into our example:

That’s it! In real, you get the stream dictionary which you can use to manipulate the tweets.

The “tweetstream” module also lets you access the Track and Follow APIs in a superb fashion!

I love Twitter, I love Python! 🙂