Setting shared Google Talk / Gmail status programmatically

There are a few ways to programmatically set the Google Talk / Gmail status via XMPP, however they have some problems if you are signed in into the same account with Google Talk or Gmail chat:

  1. You need to keep the program running – otherwise, the status will revert when the connection is closed;
  2. Other logged-in applications will not see the status change.

Enter Google’s Shared Status Messages XMPP extension. Here’s a short Python script which uses the extension to set the status from the command line (needs xmpppy):

#!/usr/bin/env python

USERNAME = "thecybershadow"       # don't include
PASSWORD = "hunter2"

import sys

if len(sys.argv) < 2:
	print 'Usage: python <show> [<status>]'
	print '		<show> is either "default" or "dnd"'
	print '		<status> is the status string (optional)'

import warnings
warnings.filterwarnings("ignore") # silence DeprecationWarning messages
from xmpp import *

if not cl.connect(server=('',5222)):
	raise IOError('Can not connect to server.')
    raise IOError('Can not auth with server.')
cl.send(Iq('set','google:shared-status', payload=[
		Node('status',payload=[sys.argv[2] if len(sys.argv)>2 else ""])

3 thoughts on “Setting shared Google Talk / Gmail status programmatically

  1. Elliot Saba

    Hey, I was trying to write this from scratch and ran into the same problem that you described in your post. Just wanted to say this piece of software works FLAWLESSLY, and I’m so glad you posted it up here! Thank you!

  2. Todd Lewis

    I’ve been looking for a way to programmatically change my gtalk status, and this did the trick! It’s worth noting that this code works under Python 2.7, but not under 3.2; I think the sys file has been changed in 3.2. I’m a complete Python newbie, so take my guess with a grain of salt.

    Your code worked successfully on my gmail account, as well as my corporate Google Apps account after making changes to lines 5 and 19. Thank you very much!

  3. Pingback: How to send fortune to gmail status « Giovanni’s Blog

Leave a Reply

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