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 @gmail.com
PASSWORD = "hunter2"
RESOURCE = "gmail.com"

import sys

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

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

cl=Client(server='gmail.com',debug=[])
if not cl.connect(server=('talk.google.com',5222)):
	raise IOError('Can not connect to server.')
if not cl.auth(USERNAME, PASSWORD, RESOURCE):
    raise IOError('Can not auth with server.')
cl.send(Iq('set','google:shared-status', payload=[
		Node('show',payload=[sys.argv[1]]),
		Node('status',payload=[sys.argv[2] if len(sys.argv)>2 else ""])
]))
cl.disconnect()

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!

    Reply
  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!

    Reply
  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 *