python-zulip-api icon indicating copy to clipboard operation
python-zulip-api copied to clipboard

Renaming bots can break them.

Open showell opened this issue 7 years ago • 4 comments

I recently renamed one of my bots from "Steve's Trivia Bot" to "Steve's Giphy Bot", and the bot program stopped working (it was running giphy, I just hadn't bothered to rename it till later). Once I re-started the program, it worked again.

I suspect this is a problem that affects many bots, and I just happened to see it with giphy.

I didn't investigate, but I think there's something going on on the bot side where the bot library is still trying to strip off the old name of the bot in the mentions or is looking for the mention. If so, we need to either a) use a regex to strip off mentions (and assume the mention is for us) or b) register for name change events so we can update our data structures. The (b) option seems like slight overkill, but name changes are rare, so it's maybe the proper solution and probably not that hard.

As I said, I haven't investigated this, so a lot of the effort here will be just running a bot, renaming the bot user, and getting to the bottom of what's going on. If you have multiple ways to solve it, you can discuss on the integrations stream.

showell avatar Jun 27 '18 21:06 showell

diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py
index 2281d66..4ca9bae 100644
--- a/zulip_bots/zulip_bots/lib.py
+++ b/zulip_bots/zulip_bots/lib.py
@@ -1,6 +1,7 @@
 import configparser
 import json
 import logging
+import re
 import os
 import signal
 import sys
@@ -229,7 +230,8 @@ def extract_query_without_mention(message: Dict[str, Any], client: ExternalBotHa
     If the bot is the first @mention in the message, then this function returns
     the stripped message with the bot's @mention removed.  Otherwise, it returns None.
     """
-    mention = '@**' + client.full_name + '**'
+    find_mentions = r'(?<![^\s\'\"\(,:<])@(\*\*[^\*]+\*\*)'
+    mention = re.search(find_mentions, message['content']).group()
     if not message['content'].startswith(mention):
         return None
     return message['content'][len(mention):].lstrip()

@showell I've tried the option a) and it's working properly, is there any possible case where the first mention don't belong to the bot. Here is the image of this working: working

shubhamdhama avatar Jun 28 '18 07:06 shubhamdhama

It's plausible that somebody could mention two different users in a message to the bot, but I don't think it's actually going to happen in practice.

I'm not sure the regex needs to be quite as complicated here.

I'm starting to lean toward the idea that the server should parse this for us.

showell avatar Jun 28 '18 11:06 showell

Slightly off context: Changes to a bot's email probably break it as well, only more dramatically?

roberthoenig avatar Jun 28 '18 11:06 roberthoenig

I'm not sure what happens with email changes. Most of the server-side code is smart about email changes, and I'm not sure we do anything bot-side with the email.

showell avatar Jun 28 '18 12:06 showell