Search code examples
pythonxmppgoogle-talk

How do I monitor the change of my gtalk status message?


I want to write a program (preferably in python) which could monitor my gtalk status messages, and whenever I post a new gtalk status message, this program will get the content of this message and post it somewhere else.

Is there a way for me to register for notification of my gtalk status change? Or do I have to poll my status constantly? Where can I find the API to do it?


Solution

  • I'd suggest you to use sleekxmpp. You can register a callback like this:

    self.add_event_handler("changed_status", self.my_callback_function)
    

    Where self is your instance of a class that inherits from sleekxmpp.ClientXMPP.

    Edit: I've just made this code for you (free to use as you wish)

    import sleekxmpp
    from ConfigParser import ConfigParser
    
    class StatusWatcher(sleekxmpp.ClientXMPP):
        def __init__(self, jid_to_watch):
            self._jid_to_watch = jid_to_watch
            config = ConfigParser()
            config.read("config.ini")
            jid = config.get("general", "jid")
            resource = config.get("general", "resource")
            password = config.get("general", "password")
            sleekxmpp.ClientXMPP.__init__(self, jid + "/" + resource, password)
    
            self.add_event_handler("session_start", self.handle_XMPP_connected)
            self.add_event_handler("changed_status", self.handle_changed_status)
    
        def handle_XMPP_connected(self, event):
            print "connected"
            self.sendPresence(pstatus="I'm just a Bot.")
            self.get_roster()
    
        def handle_changed_status(self, pres):
            if pres['from'].bare == self._jid_to_watch:
                print pres['status']
    
    
    xmpp = StatusWatcher("[email protected]") # The account to monitor
    xmpp.register_plugin('xep_0030')
    xmpp.register_plugin('xep_0199')
    if xmpp.connect():
        xmpp.process(threaded=False)
    

    You need to create a file config.ini with your credentials:

    [general]
    [email protected]
    resource=presence_watcher
    password=yourpwd