Tag Archives: Python

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()