Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

Merge pull request #484: Zulip Generic-bot @mention content fallback

Bingxi Zhao (Frank)e5846fe4624a
diff --cc deeptutor/tutorbot/channels/zulip.py
index 05a2ddb28,d3af671ef..f1220b51f
--- a/deeptutor/tutorbot/channels/zulip.py
+++ b/deeptutor/tutorbot/channels/zulip.py
@@@ -503,17 -456,33 +505,36 @@@ class ZulipChannel(BaseChannel)
return None
def _is_mentioned(self, message: dict) -> bool:
+ """Check if the bot is mentioned in the message.
+
+ For Generic bot, Zulip server may not set 'mentioned' flag in the
+ event payload, so we also detect @mention by scanning the message
+ content for patterns like @**BotName** or @**BotName|UserID**.
+ """
if self._bot_user_id is None:
+ logger.debug("Zulip _is_mentioned: bot_user_id is None, cannot check mention")
return False
+
- # 1. Check flags (works for Outgoing webhook bot and some Generic bot scenarios)
- mention_flags = {
- "mentioned",
- "wildcard_mentioned",
- "stream_wildcard_mentioned",
- "topic_wildcard_mentioned",
- }
++ # 1. Mention flags — set by Outgoing webhook bots and some Generic bot
++ # configurations.
for flag in message.get("flags", []):
- if isinstance(flag, str) and flag in mention_flags:
+ if isinstance(flag, str) and flag in MENTION_FLAGS:
+ logger.debug("Zulip _is_mentioned: found flag={}", flag)
return True
+
- # 2. Fallback: detect @mention in message content (for Generic bot)
++ # 2. Content fallback — Generic bot + Event Queue API often returns an
++ # empty ``flags`` array, so scan the message body for the literal
++ # ``@**Bot Full Name**`` mention syntax Zulip renders.
+ if self._bot_full_name:
- content = message.get("content", "")
+ mention_pattern = f"@**{self._bot_full_name}**"
- if mention_pattern in content:
++ if mention_pattern in message.get("content", ""):
++ logger.debug("Zulip _is_mentioned: matched content pattern {}", mention_pattern)
+ return True
+
+ logger.debug(
- "Zulip _is_mentioned: no mention flags found, flags={}",
++ "Zulip _is_mentioned: no mention flags or content match, flags={}",
+ message.get("flags", []),
+ )
return False
def _download_attachments(self, message: dict) -> list[str]:
diff --cc tests/services/tutorbot/test_zulip_channel.py
index 9820ea0cf,8624c6e08..6e2f4a902
--- a/tests/services/tutorbot/test_zulip_channel.py
+++ b/tests/services/tutorbot/test_zulip_channel.py
@@@ -213,6 -213,6 +213,26 @@@ class TestIsMentioned
ch._bot_user_id = 100
assert ch._is_mentioned({"flags": ["mentioned", "wildcard_mentioned"]}) is True
++ def test_content_fallback_when_flags_empty(self):
++ # Generic bot + Event Queue API returns empty flags; detect the mention
++ # from the rendered ``@**Bot Name**`` syntax in the message body.
++ ch = _make_channel()
++ ch._bot_user_id = 100
++ ch._bot_full_name = "DeepTutor Bot"
++ assert ch._is_mentioned({"flags": [], "content": "hi @**DeepTutor Bot** help"}) is True
++
++ def test_content_fallback_requires_full_name(self):
++ ch = _make_channel()
++ ch._bot_user_id = 100
++ ch._bot_full_name = ""
++ assert ch._is_mentioned({"flags": [], "content": "hi @**DeepTutor Bot**"}) is False
++
++ def test_content_fallback_no_match(self):
++ ch = _make_channel()
++ ch._bot_user_id = 100
++ ch._bot_full_name = "DeepTutor Bot"
++ assert ch._is_mentioned({"flags": [], "content": "no mention here"}) is False
++
class TestExtractUploadLinks:
def test_markdown_image(self):