Message callback
Next, we define the callback to handle incoming
<message/>
elements:
def messageCB(con, msg): type = msg.getType() if type == None: type = 'normal'
As usual, we’re expecting the message callback to be passed the
connection object (in con
)
and the message object itself (msg
). How this
callback is to proceed is determined by the type
of message received. We determine the type (taken from the
<message/>
element’s type
attribute) and store it in the variable called type
.
Remember that if no type
attribute is present, a
message type of normal
is assumed.
(See Section 5.4.1.1 for details of
<message/>
attributes.)
The two types of incoming messages we’re expecting this script to receive
are those conveying the room’s conversation—in
groupchat
-type messages—and those over which
the commands such as watch and ignore are carried, which we expect in the
form of normal
- or chat
-type
messages.
The first main section of the messageCB
handler
deals with incoming commands:
# Deal with interaction if type == 'chat' or type == 'normal': jid = str(msg.getFrom()) message = split(msg.getBody(), None, 1); reply = "" if message[0] == 'watch': addword(jid, message[1]) reply = "Okay, watching for " + message[1] if message[0] == 'ignore': delword(jid, message[1]) reply = "Okay, now ignoring " + message[1] if message[0] == 'list': if keywords.has_key(jid): reply = "Watching for: " + join(keywords[jid].keys(), ", ") else: reply = "Not watching for any keywords" if message[0] == 'stop': ...
Get Programming Jabber now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.