我想使用SleekXMPP,并自动接受所有聊天室邀请,发送给我。我知道xep_0045插件可以在收到邀请时进行检测,就像调试器中通知我的那样。我对Python还是个新手,任何帮助都将不胜感激。在
到目前为止,我在xep_插件中找到了一个名为handle_groupchat_invite
的函数。具体而言,本规范:
def plugin_init(self):
#...
self.xmpp.registerHandler(Callback('MUCInvite', MatchXMLMask("<message xmlns='%s'><x xmlns='http://jabber.org/protocol/muc#user'><invite></invite></x></message>" % self.xmpp.default_ns), self.handle_groupchat_invite))
#...
def handle_groupchat_invite(self, inv):
""" Handle an invite into a muc.
"""
logging.debug("MUC invite to %s from %s: %s", inv['from'], inv["from"], inv)
if inv['from'].bare not in self.rooms.keys():
self.xmpp.event("groupchat_invite", inv)
所以我看到这个方法在起作用,就像在终端日志中看到“MUC invite to…”消息一样。从那里,我希望我需要使用self.plugin['xep_0045'].joinMUC()
加入聊天室的URL(由inv["from"]
给出)。但是,我不太确定应该在脚本中将此代码调用到何处。在
再次感谢你的帮助。在
更新:我也尝试过在__init__
函数中使用add_event_handler
。具体来说,我的代码是:
从那里,我创建了sent_invite
函数,代码如下:
def sent_invite(self, inv):
self.plugin['xep_0045'].joinMUC(inv["from"], self.nick, wait=True)
但是,我在执行此操作时遇到以下错误:
File "muc.py", line 66, in init self.add_event_hander("groupchat_invite", self.sent_invite) AttributeError: 'MUCBot' object has no attribute 'add_event_hander'
但是在xep_插件中,我看到了以下代码:self.xmpp.event("groupchat_invite", inv)
。根据事件处理程序SleekXMPPwiki page
Stream events arise whenever particular stanzas are received from the XML stream. Triggered events are created whenever xmpp.event(name, data) is called (where xmpp is a SleekXMPP object).
有人能解释一下我为什么会出错吗?我也试过用
self.add_event_hander("muc::groupchat_invite", self.sent_invite)
但也没有成功。在
我刚从git下载了SleekXMPP,然后添加了
groupchat_invite
处理程序,这样就可以工作了:相关问题 更多 >
编程相关推荐