SleekXMPP自动接受所有聊天室邀请

2024-05-23 13:32:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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。具体来说,我的代码是:

^{pr2}$

从那里,我创建了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)

但也没有成功。在


Tags: 函数代码fromself插件eventadddef
1条回答
网友
1楼 · 发布于 2024-05-23 13:32:56

我刚从git下载了SleekXMPP,然后添加了groupchat_invite处理程序,这样就可以工作了:

diff  git a/examples/muc.py b/examples/muc.py
index 5b5c764..e327fac 100755
 - a/examples/muc.py
+++ b/examples/muc.py
@@ -61,7 +61,10 @@ class MUCBot(sleekxmpp.ClientXMPP):
         # muc::room@server::got_online, or muc::room@server::got_offline.
         self.add_event_handler("muc::%s::got_online" % self.room,
                                self.muc_online)
-
+        self.add_event_handler("groupchat_invite", self.accept_invite)
+
+    def accept_invite(self, inv):
+       print("Invite from %s to %s" %(inv["from"], inv["to"]))

     def start(self, event):
         """

相关问题 更多 >