python - Receiving "role" and/or "affiliation" with message in SleekXMPP -
apologies amateur question. i'm learning python , i'm fumbling around xmpp bot script using xmpp.
i have bot built using muc bot example sleekxmpp: http://sleekxmpp.com/getting_started/muc.html
where bot differs example script creates sqlite database , on each group_message event, parses xml retrieve nick , message body text , write database timestamp.
here part of bot that's recording msg output xmpp channel:
def groupchat_message(self, msg): if msg['type'] in ('groupchat'): raw = str(msg) # save raw xml string in database debugging purposes timestamp = datetime.utcnow().strftime('%y-%m-%d %h:%m:%s') fromuser = str(msg['from']) # convert "from" attribute string can split author = fromuser.split('/')[1] # split "from" attribute remove channel address leaving nick behind body = msg['body'] msginsert = [timestamp, author, body, raw] # database input list handed placeholders. db.execute("insert messages values (?,?,?,?)", msginsert) # "?" placeholder required automatic database sanitization. dbconn.commit() print("[",timestamp,"]",author,": ",body, sep='') else: print(msg)
the print statements debugging purposes can watch messages tick in terminal know script running.
in recorded information include user's role or affiliation in xmpp channel admins , moderators can singled out. i'm having trouble wrapping head around. seems should able based on sleekxmpp stanza docs i'm having trouble figuring out how message xml role/affiliation information.
how can take information provided in message xml , role and/or affiliation of user posted message?
for reference, raw xml output xmpp channel messages:
<message to="username@example.com" from="channelname@channels.example.com/user nick" id="1453" type="groupchat"> <body>this message body text</body> <x xmlns="jabber:x:event"> <composing /> </x> </message>
the role/affiliation isn't included in message, part of state of chat.
as reminder, xmpp has 3 different stanzas send: <message>
, had example, <iq>
, used retrieve or set things , <presence>
, indicates presence of things. role/affiliation information included in presence stanza. example in first presence stanzas client receives inform of present in room already, see example 21 xep-0045 (multi-user chat). client receives new presence stanzas whenever someone's nickname, role or affiliation changes or when leave room.
you should make sure store information yourself, because sleekxmpp doesn't you. can done creating dictionary stores, every nickname, role , 1 affiliation. on presence changes should make sure update information. can use dictionaries in message handler log role/affiliation.
so like:
def __init__(...): self.roles = dict() self.affiliations = dict() self.add_event_handler(""groupchat_presence"", self.muc_presence) ... def muc_presence(self, presence): nick = presence['muc']['nick'] self.roles[nick] = presence['muc']['role'] self.affiliations[nick] = presence['muc']['affiliation']
this general idea, you'll need more work make handle nickname changes , people leaving room properly.
Comments
Post a Comment