Skip to content

Instantly share code, notes, and snippets.

@nikdoof
Created October 8, 2012 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikdoof/3851794 to your computer and use it in GitHub Desktop.
Save nikdoof/3851794 to your computer and use it in GitHub Desktop.
Send a IM from Android with SL4A and Python
import sys, xmpp
import android
droid = android.Android()
# Default configuration options
config = {
'server': ('talk.google.com', 5223),
'IM_PRIORITY': 0,
'IM_TYPE': 'chat',
'IM_STATUS': 'Quick message...',
}
# Parse and validate config options from the intent data
for val in ['IM_USER', 'IM_PSWD', 'IM_TO', 'IM_TEXT', 'IM_TYPE', 'IM_PRIORITY', 'IM_STATUS']:
if val in droid.getIntent().result[u'extras'][u'%%%s' % val]:
config[val] = droid.getIntent().result[u'extras'][u'%%%s' % val]
# Check we have all the essentials.
for val in ['IM_USER', 'IM_PSWD', 'IM_TO', 'IM_TEXT']:
if not val in config:
droid.makeToast(u'%s value missing' % val)
sys.exit(1)
# Connect
jid = xmpp.protocol.JID(config['IM_USER'])
client = xmpp.Client(jid.getDomain(),debug=[])
conn = client.connect(server=config['server'])
if not conn:
droid.makeToast(u'ERROR: could not connect to %s:%s' % config['server'])
sys.exit(1)
# Authenticate
auth = client.auth(jid.getNode(), config['IM_PSWD'], resource='sendim')
if not auth:
droid.makeToast(u'ERROR: could not authenticate with user %s' % config['IM_USER'])
sys.exit(1)
# Send a initial presence at a low priorty as to not interfer with existing connections
client.send(xmpp.protocol.Presence(priority=config['IM_PRIORITY'], status=config['IM_STATUS']))
# Send the message and disconnect
client.send(xmpp.protocol.Message(config['IM_TO'], config['IM_TEXT'], typ=config['IM_TYPE']))
client.disconnect()
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment