2014-11-21 46 views
0

我想處理使用以下處理程序SleekXMPP不處理標題類型的消息(Openfire的)

self.add_event_handler("message", self.onmessage) 

而且方法,由SleekXMPP客戶端(從Openfire的)接收的所有郵件

def onmessage(self,msg): 

打印( '我在onmessages .. !!') 如果MSG [ '類型'〕中( '錯誤', '標題', '羣聊'): 打印 「%S」 %MSG

我可以打印類型爲「groupchat」的郵件,但是當我收到類型爲「標題」的郵件時,我沒有收到任何打印出來的郵件。

我已驗證我的連接是通過啓用DBUG模式接收這些消息。

任何想法爲什麼我的消息處理程序不處理標題消息?

EX. groupchat message i got (which is successfully processed by the handler) 


<message to="[email protected]/resource" type="groupchat" id="m_444" 
from="[email protected]/Chatadmin1 HOST"><body>user06</body> 
<html xmlns=""><body xmlns="">user06</body></html></message>" 

EX. headline message I want to process with the same handler (which is not working ATM) 

<message to="[email protected]/resource" type="headline" from="chat"> 
<x xmlns="domain:mute"> 
<mute duration="5" reasonCode="mute.reason.swearing" expiryTime="1416483206670" /> 
</x></message> 

提前一個的解釋/溶液非常感謝)

+0

是不是SleekXMPP開源?如果是這樣,你不能只看源代碼並調試它,看看標題類型消息發生了什麼? – Flow 2014-11-21 14:03:09

+0

@Flow:我完全同意你的看法,但不幸的是,我最近開始自學python。儘管如此,我仍然試圖查看代碼,但無法完全理解,因此發佈在堆棧溢出上。無論如何非常感謝您的建議。 – 2014-11-21 18:58:44

+0

好吧,我真的建議利用開源的力量。解決這些問題也是最好的學習方式。 – Flow 2014-11-21 20:09:07

回答

1

回答上述問題在此說明(類似實施例): https://github.com/fritzy/SleekXMPP/tree/develop/examples/custom_stanzas

基本上我需要註冊自定義節自我們在節中包含了非常特定於我們域的名稱空間。

<message type="headline" from="chat" to="[email protected]/resource"> 
    <comp xmlns="xxxx:comp" type="currency"> 
     <currency xmlns="xxxx:currency"> 
      <amount>1.00</amount> 
      <iso-code>GBP</iso-code> 
     </currency> 
    </comp> 
</message> 

第1步:創建一類像下面

class Currency(ElementBase): 
    namespace = 'xxxx:currency' 
    name = 'currency' 
    plugin_attrib = 'currency' 
    interfaces = set(('amount', 'iso-code')) 
    sub_interfaces = interfaces 


class Comp(ElementBase): 
    namespace = 'xxxx:comp' 
    name = 'comp' 
    plugin_attrib = 'comp' 
    interfaces = set(('type', 'currency')) 
    sub_interfaces = interfaces 
    subitem = (Currency,) 

def getCurrency(self): 
    currency = {} 
    for cur in self.xml.findall('{%s}currency' % Currency.namespace): 
     cur = Currency(cur) 
     currency[cur['amount']] = cur['iso-code'] 
    return currency 

你可以有更多的util的方法,根據您的要求

第2步:

註冊像下面的節(這應該在客戶類別中完成(請參閱此示例))

register_stanza_plugin(Message, Comp) 

第3步:註冊事件

self.registerHandler(
     Callback('Comp Message', StanzaPath('{%s}message/{%s}comp' % (self.default_ns,self.default_ns)),self.oncomp)) 

def oncomp(self, msg): 
    self.event('custom_action', msg) 

第4步:處理該事件

self.add_event_handler('custom_action', 
          self.handle_action_event) 

def handle_action_event(self, msg): 
    print("I am in handle_action_event***************") 
    print(msg)