Skip to content

The agent that grows with you

fix(telegram): a reply to an unknown author keeps the notice without a "?"

kshitijk4poor61794f3f173e
diff --git a/plugins/platforms/telegram/telegram_context.py b/plugins/platforms/telegram/telegram_context.py
index 339b63c8148..7c4ae5c25c2 100644
--- a/plugins/platforms/telegram/telegram_context.py
+++ b/plugins/platforms/telegram/telegram_context.py
@@ -44,17 +44,21 @@ def starts_with_other_participant_mention(adapter: "TelegramAdapter", message: "
)
-def _replies_to_other_member(adapter: "TelegramAdapter", message: "Message") -> bool:
- """A reply to another known member's message. In a forum topic Telegram also sets ``reply_to_message``
+def _quoted_reply(message: "Message"):
+ """The message this one replies to, or None. In a forum topic Telegram also sets ``reply_to_message``
to the topic's root on every plain message; that anchor is not a reply to a person."""
replied = getattr(message, "reply_to_message", None)
- if replied is None or getattr(replied, "from_user", None) is None or adapter._is_reply_to_bot(message):
- return False
- topic_root = (
- getattr(message, "is_topic_message", False)
- and replied.message_id == getattr(message, "message_thread_id", None)
- )
- return not topic_root
+ if replied is None or (getattr(message, "is_topic_message", False)
+ and replied.message_id == getattr(message, "message_thread_id", None)):
+ return None
+ return replied
+
+
+def _replies_to_other_member(adapter: "TelegramAdapter", message: "Message") -> bool:
+ """A reply to another known member's message."""
+ replied = _quoted_reply(message)
+ return (replied is not None and getattr(replied, "from_user", None) is not None
+ and not adapter._is_reply_to_bot(message))
def group_reply_expected(adapter: "TelegramAdapter", message: "Message", chat_type: str) -> Optional[bool]:
@@ -72,6 +76,9 @@ def group_reply_expected(adapter: "TelegramAdapter", message: "Message", chat_ty
return True
if not adapter._bot or not adapter._current_bot_username():
return None
+ replied = _quoted_reply(message)
+ if replied is not None and getattr(replied, "from_user", None) is None:
+ return None # the quoted author is unknown, so the reply may be to the bot
if _replies_to_other_member(adapter, message) or starts_with_other_participant_mention(adapter, message):
return False
# Words in a voice note or audio file are not in the message text, so the "?" rule cannot read them.
diff --git a/tests/gateway/test_telegram_group_gating.py b/tests/gateway/test_telegram_group_gating.py
index 3f3e7cdc1e7..13ebcdd6191 100644
--- a/tests/gateway/test_telegram_group_gating.py
+++ b/tests/gateway/test_telegram_group_gating.py
@@ -1040,6 +1040,7 @@ def _with_media(message, kind):
(_group_message("what did @alice say?", entities=[_mention_entity("what did @alice say?", "@alice")]), None),
(_replying_to(_group_message("anyone up for dinner?", thread_id=5), user_id=222, message_id=5), None),
(_replying_to(_group_message("agreed?")), None),
+ (_replying_to(_group_message("agreed")), None),
(_with_media(_group_message(None), "voice"), None),
(_with_media(_group_message(None), "audio"), None),
(_with_media(_group_message(None, caption="the fridge arrives on saturday"), "voice"), False),
@@ -1048,6 +1049,7 @@ def _with_media(message, kind):
"mention", "reply-to-bot", "wake-word", "caption-command", "indented-command", "dm",
"reply-to-other-member", "opens-with-other-mention", "chatter",
"question", "mentions-other-mid-question", "forum-topic-root", "reply-to-unknown-author",
+ "reply-to-unknown-author-statement",
"voice-note", "audio-file", "captioned-voice-chatter", "photo-without-caption",
])
def test_reply_expected_follows_who_the_group_message_addresses(message, expected):