Skip to content

The agent that grows with you

fix(telegram): replies to a channel or anonymous-admin post keep the notice

kshitijk4poorc419cf7c864a
diff --git a/plugins/platforms/telegram/telegram_context.py b/plugins/platforms/telegram/telegram_context.py
index 7c4ae5c25c2..58e26a0b236 100644
--- a/plugins/platforms/telegram/telegram_context.py
+++ b/plugins/platforms/telegram/telegram_context.py
@@ -44,23 +44,16 @@ def starts_with_other_participant_mention(adapter: "TelegramAdapter", message: "
)
-def _quoted_reply(message: "Message"):
+def _quoted_reply(message: "Message") -> Optional["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(message, "is_topic_message", False)
- and replied.message_id == getattr(message, "message_thread_id", None)):
+ and getattr(replied, "message_id", None) == 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]:
"""Whether an admitted message needs a reply; None keeps the visible silence fallback."""
if chat_type != "group":
@@ -76,10 +69,13 @@ 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):
+ if (replied := _quoted_reply(message)) is not None:
+ # Only a reply to another known member is chatter. An unknown author, or a post signed by a chat
+ # (a linked channel's auto-forward, an anonymous admin; Telegram fills from_user with a service
+ # account there), may still be meant for the bot.
+ unknown = getattr(replied, "from_user", None) is None or getattr(replied, "sender_chat", None) is not None
+ return None if unknown else False
+ if 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.
spoken = not text and any(getattr(message, kind, None) for kind in ("voice", "audio"))
diff --git a/tests/gateway/test_telegram_group_gating.py b/tests/gateway/test_telegram_group_gating.py
index 13ebcdd6191..309fc724cf8 100644
--- a/tests/gateway/test_telegram_group_gating.py
+++ b/tests/gateway/test_telegram_group_gating.py
@@ -1015,9 +1015,10 @@ def test_sibling_bot_explicit_mention_still_dispatches_and_is_not_observed():
assert adapter._should_observe_unmentioned_group_message(human) is False
-def _replying_to(message, *, user_id=None, message_id=10):
+def _replying_to(message, *, user_id=None, message_id=10, sender_chat=None):
message.reply_to_message = SimpleNamespace(
- from_user=SimpleNamespace(id=user_id) if user_id else None, message_id=message_id, text="earlier", caption=None)
+ from_user=SimpleNamespace(id=user_id) if user_id else None, message_id=message_id, text="earlier", caption=None,
+ sender_chat=SimpleNamespace(id=sender_chat, type="channel") if sender_chat else None)
return message
@@ -1041,6 +1042,8 @@ def _with_media(message, kind):
(_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),
+ (_replying_to(_group_message("when does it start?"), user_id=777000, sender_chat=-1009), None),
+ (_replying_to(_group_message("agreed"), user_id=1087968824, sender_chat=-100), 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),
@@ -1049,7 +1052,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",
+ "reply-to-unknown-author-statement", "reply-to-channel-autoforward", "reply-to-anonymous-admin",
"voice-note", "audio-file", "captioned-voice-chatter", "photo-without-caption",
])
def test_reply_expected_follows_who_the_group_message_addresses(message, expected):
diff --git a/website/docs/user-guide/messaging/telegram.md b/website/docs/user-guide/messaging/telegram.md
index 3c70a7f1054..c769604fdb0 100644
--- a/website/docs/user-guide/messaging/telegram.md
+++ b/website/docs/user-guide/messaging/telegram.md
@@ -656,7 +656,7 @@ A bot that reads every group message can answer exactly `[SILENT]` (or another [
| Any other message without a `?` | Stays silent |
| Any other message with a `?` (it may be a question for the bot) | Replaced by the notice |
-A message still gets the notice while the bot's own identity is unknown, or when it replies to a message whose author is unknown. A voice note or audio file without a caption also gets the notice, because its words are not in the message text. A forum topic's root message is not a reply to another member.
+A message still gets the notice while the bot's own identity is unknown, or when it replies to a message whose author is unknown or that was posted as a chat (a linked channel's post, an anonymous admin). A voice note or audio file without a caption also gets the notice, because its words are not in the message text. A forum topic's root message is not a reply to another member.
### Multiple Hermes bots in one group