我想使用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邀请.“终端日志中的消息。从这里开始,我希望我需要使用self.plugin['xep_0045'].joinMUC()来加入聊天室的URL (由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 wiki页面,
每当从XML流接收到特定节时,就会发生流事件。每当调用xmpp.event(名称、数据)(其中xmpp是SleekXMPP对象)时,就会创建触发事件。
有人能解释一下为什么我会出错吗?我也试过用
self.add_event_hander("muc::groupchat_invite", self.sent_invite)但也没有成功。
发布于 2014-06-17 13:50:11
我刚刚从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):
"""https://stackoverflow.com/questions/24133662
复制相似问题