Tuesday, May 3, 2011

Atomated Gtalk using python

import xmpp
import sys
import time
import thread
#this function for break the infinite loop-------or on log out--------
def StepOn(conn):
try:
conn.Process(1)
except KeyboardInterrupt:
return 0
return 1

def GoOn(conn):
while StepOn(conn):
pass
#---------------------------Sending SMS routine----------------------------------
def SendSMS(cl):
#for i in range(0,10):
cl.send(xmpp.Message( "dutta.dipankar08@gmail.com" ,"This is an Experimental WOrk..!!!!.." ))

##-------------------------------Reciving sms routine------------------------------------------------
def messageCB(cl,msg):
print "Sender: " + str(msg.getFrom())
print "Content: " + str(msg.getBody())
#print msg
SendSMS(cl);

##-------------------------------Presence sms routine------------------------------------------------
def presenceCB(conn,msg):
#print str(msg)
prs_type=msg.getType()
who=msg.getFrom()
status=msg.getStatus()
print msg.getType(),who,status
if prs_type == "subscribe":
conn.send(xmpp.Presence(to=who, typ = 'subscribed'))
#conn.send(xmpp.Presence(to=who, typ = 'subscribe'))


#----------update status1: TIMER---------------------------------------------
#def updateStatus1(conn):
# while(1):
# localtime = time.asctime( time.localtime(time.time()) )
# pres = xmpp.Presence()
# pres.setStatus(str(localtime))
# conn.send(pres)
# time.sleep(1)
#-------------------ROTATE GOOGLE CHAT STATUS>>>..........................
#def updateStatus2(conn):
# Status= " Hi ! This is Dipankar..."
# while(1):
# pres = xmpp.Presence()
# pres.setStatus(Status)
# conn.send(pres)
# Status=Status[1:len(Status)]+Status[0:1]
# print Status
# time.sleep(.5)


def RECV_MSG(cl):
cl.RegisterHandler('message', messageCB)



def main():
#userID = 'dutta.dipankar08@gmail.com'
userID = 'dutta.subhankar123@gmail.com'
password = 'xxxxxxx'
ressource = 'Script'

jid = xmpp.protocol.JID(userID)
cl = xmpp.Client(jid.getDomain(), debug=[])

connection = cl.connect(('talk.google.com',5222))

if not connection:
sys.stderr.write('Could not connect\n')
else:
sys.stderr.write('Connected with %s\n' % connection)

auth = cl.auth(jid.getNode(), password, ressource)
if not auth:
sys.stderr.write("Could not authenticate\n")
else:
sys.stderr.write('Authenticate using %s\n' % auth)

cl.sendInitPresence(requestRoster=1)


# check connection
print cl.isConnected()

#send message--
#SendSMS(cl)

##reciving message.............
#RECV_MSG(cl)
cl.RegisterHandler('message', messageCB)


#---get presence CB........
cl.RegisterHandler('presence', presenceCB)


#------------change the status...----------DONE
try:
#thread.start_new_thread(updateStatus,(cl,))
print 'hello'
except:
print " error is thread cration"

#--get contact list-----------------
ros=cl.getRoster()
for i in ros:
print "a"

#run the main thread for ever...
GoOn(cl)

cl.disconnect()

main()

No comments:

Post a Comment