Skip to content

fix(telegram): add dedicated handler for forum topic service messages#5488

Open
willy-scr wants to merge 3 commits into
NousResearch:mainfrom
willy-scr:fix/forum-topic-service-message-handler
Open

fix(telegram): add dedicated handler for forum topic service messages#5488
willy-scr wants to merge 3 commits into
NousResearch:mainfrom
willy-scr:fix/forum-topic-service-message-handler

Conversation

@willy-scr

Copy link
Copy Markdown
Contributor

Problem

Forum topic lifecycle events (forum_topic_created, forum_topic_edited, forum_topic_closed, forum_topic_reopened) were never processed by the gateway because they are delivered as service messages — they carry neither text nor media content. The existing handler registration only covered TEXT, COMMAND, LOCATION, and MEDIA filters, so these events were silently dropped.

The auto-discovery logic added in _build_message_event (checking message.forum_topic_created) was unreachable because no handler filtered for service message types.

Root Cause

# Existing handlers — none match service messages
self._app.add_handler(TelegramMessageHandler(filters.TEXT & ~filters.COMMAND, ...))
self._app.add_handler(TelegramMessageHandler(filters.COMMAND, ...))
self._app.add_handler(TelegramMessageHandler(filters.LOCATION | ..., ...))
self._app.add_handler(TelegramMessageHandler(filters.PHOTO | filters.VIDEO | ..., ...))

forum_topic_created messages have no .text, no media — they only have the .forum_topic_created attribute. None of the registered filters match, so the update is discarded.

Fix

Add a dedicated MessageHandler filtered on filters.StatusUpdate.FORUM_TOPIC_CREATED | FORUM_TOPIC_EDITED | FORUM_TOPIC_CLOSED | FORUM_TOPIC_REOPENED that directly calls _persist_group_topic / _persist_group_topic_edit.

This ensures:

  • New topics are auto-registered in config.yaml immediately when created by any admin (not just the bot)
  • Topic renames are tracked
  • Topic close/reopen status is persisted
  • Works for all group forum events regardless of who triggers them

Changes

  • Added _handle_forum_topic_event handler with filters.StatusUpdate.FORUM_TOPIC_* filters
  • Handler directly calls existing _persist_group_topic / _persist_group_topic_edit methods
  • Includes full group forum topic infrastructure from prior work:
    • _group_topics_config init
    • _persist_group_topic, _persist_group_topic_edit
    • _get_group_topic_info, _reload_group_topics_from_config
    • Topic name/skill/role resolution in _build_message_event

Testing

Confirmed on live Telegram group:

  1. Bot created topic via API → no forum_topic_created event received (Telegram behavior — no echo to self)
  2. Human admin created topic → event received and auto-registered with the fix
  3. Without the fix: zero forum topic events reached _build_message_event (verified in gateway logs)

@willy-scr willy-scr force-pushed the fix/forum-topic-service-message-handler branch from ad86dde to a158c04 Compare April 6, 2026 10:19
Akah-dev added 3 commits April 9, 2026 17:41
Forum topic lifecycle events (created/edited/closed/reopened) are
delivered as service messages that carry neither text nor media.  The
existing handler registration only covered TEXT, COMMAND, LOCATION and
MEDIA filters, so forum_topic_created updates were silently dropped
and the auto-discovery logic in _build_message_event was unreachable.

Add a new MessageHandler filtered on
filters.StatusUpdate.FORUM_TOPIC_{CREATED,EDITED,CLOSED,REOPENED}
that directly calls _persist_group_topic / _persist_group_topic_edit,
ensuring new topics are registered in config.yaml immediately when
created by any admin (not just the bot itself).

Also includes group forum topic infrastructure:
- _group_topics_config init from extra.group_topics
- _persist_group_topic: auto-register new topics to config.yaml
- _persist_group_topic_edit: handle rename/close/reopen
- _get_group_topic_info: resolve topic name/skill/role
- _reload_group_topics_from_config: hot-reload without restart
- _build_message_event: resolve group topic context for messages
… error

When Telegram returns 'thread not found' BadRequest, the topic has
been deleted. Call _remove_group_topic to clean up the stale entry
from config.yaml so future messages don't repeatedly hit the same
error.

Zero performance impact — piggybacks on existing error handling.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/telegram Telegram bot adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants