-
Notifications
You must be signed in to change notification settings - Fork 3
goodbot: Processing messages
The process()
method is the main way of handling user messages, called each time an event is triggered from Zulip's real-time event queue.
def process(self, msg):
sender_email = msg["sender_email"]
if sender_email == self.bot_mail: # quick return
return
content = msg["content"].strip().split()
sender_full_name = msg["sender_full_name"]
message_type = msg["type"] # "stream" or "private"
if message_type == "stream":
destination = msg["stream_id"] # destination is stream
else:
destination = sender_email # destination is PM
First we terminate the call early if the message received is the bot's own, then we split the message into separate words with split()
and finally changing the destination depending on whether it's a private message or the destination message.
The bot then evaluates the message one-by-one to see if it matches a command, like a fall-through in a switch case.
try:
greeting = f"{random.choice(self.greetings)} @**{sender_full_name}**!"
if sender_email == "notification-bot@zulip.com" and topic == "signups": # hack for reading #announce stream
...
if content[0].lower() == "!help" or content[0] == "@**goodbot**":
...
if content[0].lower() == "!gsoc":
...
We finally wrap the entire thing in a try-except to ensure the bot process keeps running even when we encounter any exceptions.
Copyright (C) 2020 QEDK Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".
- Home
- Building dependencies
- goodbot
- ircbot
- parsebot
- Web app
- Running
- Toolforge
- Style guidelines
- GNU Free Documentation License