#1667·DeepTutor

feat(partners/telegram): per-chat and per-topic response policy (open inside allowlisted topics, mention-only elsewhere)

Author: ngojcleeCreated Sep 29, 2026Updated Sep 29, 2026

Feature request: per-chat and per-topic response policy for the Telegram channel

Problem

group_policy is a single channel-wide switch with only two values, so a partner cannot have different behaviour in different groups, nor restrict auto-replies to specific forum topics inside one group.

Real-world case that motivated this: I run one partner bot that should be chatty in a small private group (auto-reply without being mentioned), but only inside a few allowlisted topics of that group — while in every other group it must stay quiet unless explicitly @mentioned.

With the current options I have to choose one extreme:

  • group_policy: mention → bot stays quiet in my own group too, and I must @ it in every single message, even inside my dedicated topics.
  • group_policy: open → bot replies to everything, everywhere: in any group it is added to, in any topic, including other people's conversations, and the General topic.

There is no way to express "open, but only in topics 1 / 44 / 115 of chat -100xxxxxxxxxx".

Current implementation

deeptutor/partners/channels/telegram.py:

python
class TelegramConfig(DeliveryOverrides, StreamingSupport):
    group_policy: Literal["open", "mention"] = "mention"
python
async def _is_group_message_for_bot(self, message) -> bool:
    """Allow group messages when policy is open, @mentioned, or replying to the bot."""
    if message.chat.type == "private" or self.config.group_policy == "open":
        return True
    # ...mention / reply-to-bot detection...

So the policy decision has no access to chat_id, and no concept of message_thread_id (grep for allowed_topics / allowed_threads in the channel returns nothing).

Proposed behaviour

Add per-chat policy overrides, with an allowlisted-topic mode:

yaml
group_policy: mention          # channel default, unchanged
chat_policies:                 # NEW: per-chat overrides, first match wins
  - chat_id: -1003921765626
    policy: topics_only
    allowed_topics: [1, 44, 115]    # Telegram message_thread_id values
  - chat_id: -1001111111111
    policy: open
  - chat_id: -1002222222222
    policy: mention

Semantics:

policy behaviour in that chat
open reply to every message (today's channel-wide open)
mention reply only when @mentioned or replying to one of the bot's messages (today's default, explicit form)
topics_only reply without a mention only inside an allowed_topics thread; everywhere else in that chat behave as mention (so a message in the General topic, or in any non-listed topic, needs a mention)

Details worth deciding during review:

  1. Private chats keep current behaviour (always answer) — chat_policies only applies to groups.
  2. A chat not listed in chat_policies falls back to the existing channel-wide group_policy. Keep the channel-wide key so existing configs keep working.
  3. allow_from (sender allowlist) stays orthogonal and unchanged — it is a who, this is a where.
  4. message_thread_id is None for the General topic; treat that as "not in allowed_topics" so General is mention-only under topics_only.
  5. Optional (nice to have): a * chat_id entry as the wildcard default, instead of adding a second key.
  6. Optional (nice to have): when a message is dropped by policy, log at debug level (dropped: chat=-100… topic=44 policy=topics_only) — makes misconfiguration obvious.

Why now

Outbound topic routing was recently fixed (#1461 / #1462), so partners now answer inside the topic the user wrote in. That makes "chat freely inside these topics" a safe, useful configuration — before that fix, the reply would have gone to General anyway. The remaining gap is purely the inbound policy decision.

Environment

  • Version: image built 2026-09-27, ghcr.io/hkuds/deeptutor:latest (contains the #1462 fix)
  • Channel: Telegram, long polling
  • UI note: the Partners channel card renders from /api/v1/partners/channels/schema, so a list[object] field would show up as an editable list — no UI work should be required beyond the schema.