【发布时间】:2014-05-18 19:13:58
【问题描述】:
对于这个业余问题,我们深表歉意。我只是在学习 Python,我正在使用 XMPP 摸索这个 XMPP 机器人脚本。
我有一个使用 SleekXMPP 的 MUC 机器人示例构建的机器人:http://sleekxmpp.com/getting_started/muc.html
我的机器人与示例的不同之处在于我的脚本创建了一个 SQLite 数据库,并在每个 group_message 事件上解析 XML 以检索昵称和消息正文文本,并将其写入带有时间戳的数据库。
这是我的机器人记录 XMPP 通道的 msg 输出的部分:
def groupchat_message(self, msg):
if msg['type'] in ('groupchat'):
raw = str(msg) # Save raw XML as a string in the database for debugging purposes
timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
fromuser = str(msg['from']) # Convert "from" attribute to string so it can be split
author = fromuser.split('/')[1] # Split "from" attribute to remove channel address leaving only nick behind
body = msg['body']
msginsert = [timestamp, author, body, raw] # Database input list to be handed to placeholders.
db.execute("INSERT INTO messages VALUES (?,?,?,?)", msginsert) # "?" placeholder is REQUIRED for automatic database sanitization.
dbconn.commit()
print("[",timestamp,"]",author,": ",body, sep='')
else:
print(msg)
打印语句仅用于调试目的,因此我可以在终端中查看消息滴答作响,因此我知道脚本正在运行。
在记录的信息中,我还想在 XMPP 频道中包含用户的角色或从属关系,以便可以挑选出管理员和版主。这就是我难以理解的问题。似乎我应该能够根据SleekXMPP stanza docs 做到这一点,但我无法弄清楚如何从消息 XML 获取角色/从属信息。
如何获取消息 XML 中提供的信息并找回发布该消息的用户的角色和/或从属关系?
作为参考,这是来自 XMPP 通道的消息的原始 XML 输出:
<message to="username@example.com" from="channelname@channels.example.com/User Nick" id="1453" type="groupchat">
<body>This is the message body text</body>
<x xmlns="jabber:x:event">
<composing />
</x>
</message>
【问题讨论】: