【问题标题】:How do I monitor the change of my gtalk status message?如何监控我的 gtalk 状态消息的变化?
【发布时间】:2013-07-04 19:10:51
【问题描述】:

我想写一个程序(最好用python)来监控我的gtalk状态消息,每当我发布一个新的gtalk状态消息时,这个程序都会得到这个消息的内容并将它发布到其他地方。

我是否可以注册以获取有关我的 gtalk 状态更改的通知?还是我必须不断地轮询我的状态?我在哪里可以找到执行此操作的 API?

【问题讨论】:

    标签: python xmpp google-talk


    【解决方案1】:

    我建议您使用 slimxmpp。您可以像这样注册回调:

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

    其中 self 是继承自 sleekxmpp.ClientXMPP 的类的实例。

    编辑:我刚刚为你制作了这段代码(随意使用)

    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("login@gmail.com") # The account to monitor
    xmpp.register_plugin('xep_0030')
    xmpp.register_plugin('xep_0199')
    if xmpp.connect():
        xmpp.process(threaded=False)
    

    您需要使用您的凭据创建一个文件config.ini:

    [general]
    jid=jid@host.org
    resource=presence_watcher
    password=yourpwd
    

    【讨论】:

      【解决方案2】:

      由于 Google Talk 本质上是一项 XMPP 服务,因此请使用该协议注册您的程序,以便获知新消息(以及其他更新,例如在线状态)。

      一些提示:

      【讨论】:

        猜你喜欢
        • 2012-05-18
        • 1970-01-01
        • 2010-11-14
        • 2011-01-31
        • 2014-12-11
        • 1970-01-01
        • 2015-05-23
        • 1970-01-01
        相关资源
        最近更新 更多