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

0 投票
1 回答
1241 浏览
提问于 2025-04-18 09:14

我想用SleekXMPP这个库,自动接受所有发给我的聊天室邀请。我知道xep_0045这个插件可以检测到我收到邀请,因为我在调试器里看到了通知。我对Python还比较陌生,所以任何帮助都很感激。

到目前为止,我发现了一个叫做handle_groupchat_invite的函数在xep_0045插件里。具体来说,这段代码是:

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()来加入聊天室的链接(这个链接是由inv["from"]提供的)。不过,我不太确定在我的脚本中应该在哪里调用这段代码。

再次感谢你的帮助。

更新:我还尝试在__init__函数中使用add_event_handler。具体我的代码是:

def __init__(self, jid, password, room, nick):
    sleekxmpp.ClientXMPP.__init__(self, jid, password)

    self.room = room
    self.nick = nick

    # The session_start event will be triggered when
    # the bot establishes its connection with the server
    # and the XML streams are ready for use. We want to
    # listen for this event so that we we can initialize
    # our roster.
    self.add_event_handler("session_start", self.start)

    # The groupchat_message event is triggered whenever a message
    # stanza is received from any chat room. If you also also
    # register a handler for the 'message' event, MUC messages
    # will be processed by both handlers.
    self.add_event_handler("groupchat_message", self.muc_message)

    # The groupchat_presence event is triggered whenever a
    # presence stanza is received from any chat room, including
    # any presences you send yourself. To limit event handling
    # to a single room, use the events muc::room@server::presence,
    # 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_hander("groupchat_invite", self.sent_invite)

然后,我创建了sent_invite函数,代码如下:

def sent_invite(self, inv):
    self.plugin['xep_0045'].joinMUC(inv["from"], self.nick, wait=True)

但是,当我这样做的时候,出现了以下错误:

文件 "muc.py",第66行,在 init 中 self.add_event_hander("groupchat_invite", self.sent_invite) AttributeError: 'MUCBot'对象没有属性 'add_event_hander'

然而在xep_0045插件里,我看到了这段代码:self.xmpp.event("groupchat_invite", inv)。根据SleekXMPP的事件处理器维基页面

当从XML流中接收到特定的消息时,会产生流事件。每当调用xmpp.event(name, data)时(其中xmpp是一个SleekXMPP对象),就会触发事件。

有人能解释一下我为什么会得到这个错误吗?我也尝试过使用

self.add_event_hander("muc::groupchat_invite", self.sent_invite)

但也没有成功。

1 个回答

1

我刚从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):
         """

撰写回答