我想使用SleekXMPP并自动接受发送给我的所有聊天室邀请。我知道xep_0045插件可以检测我何时收到邀请,因为我在调试器中收到通知。我仍然对Python很陌生,任何帮助都会受到赞赏。
到目前为止,我在xep_0045插件中找到了一个名为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邀请...”消息。从那里,我希望我需要使用self.plugin['xep_0045'].joinMUC()
加入聊天室的URL(由inv["from"]
给出)。但是,我不确定在我的脚本中应该在哪里调用此代码。
再次感谢您的帮助。
更新:我还尝试在add_event_handler
函数中使用__init__
。具体来说,我的代码是:
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)
但是,当我这样做时,我收到以下错误:
在 init 中输入第66行文件“muc.py” self.add_event_hander(“groupchat_invite”,self.sent_invite)AttributeError:'MUCBot'对象没有属性'add_event_hander'
然而在xep_0045插件中,我看到了这段代码:self.xmpp.event("groupchat_invite", inv)
。根据事件处理程序SleekXMPP wiki page,
只要从XML流接收到特定节,就会出现流事件。只要调用 xmpp.event(name,data)(其中xmpp是一个SleekXMPP对象),就会创建触发事件。
有人可以解释我收到错误的原因吗?我也尝试过使用
self.add_event_hander("muc::groupchat_invite", self.sent_invite)
但也没有成功。
答案 0 :(得分: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):
"""