2013-07-07 47 views
1

我想編寫一個程序(最好在python中),它可以監視我的gtalk狀態消息,並且每當我發佈一個新的gtalk狀態消息時,該程序將獲取此消息的內容並將其發佈到其他地方。如何監控我的gtalk狀態消息的變化?

有沒有辦法讓我註冊我的gtalk狀態更改通知?或者我必須不斷地調查我的狀態?我在哪裏可以找到API來做到這一點?

回答

1

我建議你使用sleekxmpp。你可以這樣註冊一個回調:

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

自我在哪裏你是從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("[email protected]") # The account to monitor 
xmpp.register_plugin('xep_0030') 
xmpp.register_plugin('xep_0199') 
if xmpp.connect(): 
    xmpp.process(threaded=False) 

你需要創建一個文件config.ini使用您的憑據:

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