Monday, September 7, 2009

Sending Google Talk IM using python


Google talk uses XMPP protocol for sending/receving IM. Any client that supports Jabber/XMPP can connect to the Google Talk service. The service is hosted at talk.google.com on port 5222, TLS is required for Google Talk and The only supported authentication mechanism is SASL PLAIN. This are very important thing to know before coding for Google Talk.


In Python for XMPP support, I have used xmpppy. First download and install it in your system, detail is here. After that you need to connect, authonticate and send the IM. This is really as simple as that. The sample code I have used to perform the job is this:
  • import xmpp
  • import time 
  • jid  =  xmpp.protocol.JID( 'your.gmail.id@gmail.com' )
  • cl  =  xmpp.Client('gmail.com')
  • cl.connect( ( 'talk.google.com', 5223 ) )
  • cl.auth( jid.getNode( ), 'your_gmail_password' )
  • IM = 'sending for testing'
  • cl.send( xmpp.protocol.Message( 'friends.gmail.id@gmail.com', IM,typ = "chat" ) )
  • time.sleep( 1 )
  • print str( IM ) + ' sent '
This is how you can send IM to a Google Talk friend using Python.

Thanks
 Sakib

1 comment:

  1. I was wondering what would need to be done in order to receive a message?

    ReplyDelete