Skip to content

goodbot: Processing messages

qedk edited this page Jun 29, 2020 · 1 revision

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.