Skip to content

feat: add require_mention option for Telegram group chats - #2000

Closed
butnull wants to merge 1 commit into
NousResearch:mainfrom
butnull:feat/telegram-mention-response
Closed

feat: add require_mention option for Telegram group chats#2000
butnull wants to merge 1 commit into
NousResearch:mainfrom
butnull:feat/telegram-mention-response

Conversation

@butnull

@butnull butnull commented Mar 18, 2026

Copy link
Copy Markdown

Summary

Add support for mention-based response in Telegram group chats. When enabled, the bot will only respond in groups when explicitly @mentioned, preventing noisy conversations when multiple bots are present.

Changes

  • gateway/config.py: Add config loading for telegram.require_mention via env TELEGRAM_REQUIRE_MENTION
  • gateway/platforms/telegram.py:
    • Fetch bot username on connect for mention checking
    • Add _is_bot_mentioned() helper method
    • Check mention in _handle_text_message() for group chats

Configuration

config.yaml:

Environment variable:

Behavior

Chat Type require_mention=true require_mention=false
DM (Private) Always respond Always respond
Group/Supergroup Only respond if @mentioned Always respond

Use Case

This feature is useful when running multiple AI agents (e.g., Hermes + OpenClaw) in the same Telegram group - users can control which bot responds by @mentioning the specific one they want.

Add support for respond_to_mentions_only behavior in Telegram groups:
- New config option telegram.require_mention (via env TELEGRAM_REQUIRE_MENTION)
- Bot fetches its own username on connect for mention checking
- In group chats, when enabled, only responds when @mentioned
- DMs are not affected (always respond)

Usage in config.yaml:
  telegram:
    require_mention: true

Or via env:
  TELEGRAM_REQUIRE_MENTION=true
@nidhishgajjar

Copy link
Copy Markdown

Orb Code Review (powered by GLM 5.1 on Orb Cloud)

Summary

This PR adds a require_mention option for Telegram group chats, allowing the bot to only respond when @mentioned. It includes config loading from YAML and a mention-detection method.

Architecture

Two changes: gateway/config.py (YAML→env var bridge) and gateway/platforms/telegram.py (mention check in _handle_text_message).

Issues

🔴 Critical: Duplicate of existing functionality

The Telegram adapter already has a complete require_mention system in the codebase:

  1. _telegram_require_mention() (line 1988) — already reads from config.extra or TELEGRAM_REQUIRE_MENTION env var
  2. _should_process_message() (line 2124) — already implements group chat filtering with mention checks
  3. _message_mentions_bot() (line 2077) — already detects @mentions via text and entities

The PR adds duplicate code:

  • New self._require_mention field → duplicates _telegram_require_mention()
  • New _is_bot_mentioned() → duplicates _message_mentions_bot() (but less complete)
  • New check in _handle_text_message() → runs before the existing _should_process_message() check on line 2165, making it doubly redundant

🟡 Moderate: New mention check is less complete

The new _is_bot_mentioned() method is a subset of the existing _message_mentions_bot():

  • ❌ Doesn't handle text_mention entities (users with hidden usernames)
  • ❌ Doesn't check message captions
  • ❌ Requires a separate get_me() API call to fetch bot username, while existing code uses self._bot.username directly

🟢 Useful: Config.py YAML bridge

The gateway/config.py change that reads telegram.require_mention from YAML config and bridges it to the env var is genuinely useful. This is the only part of the PR that adds value — the rest duplicates what _telegram_require_mention() already handles via config.extra.

Suggestions

  1. Keep only the config.py change — it correctly bridges YAML config to the env var that the existing system already reads
  2. Remove the duplicate code from telegram.py — the existing _should_process_message()_message_mentions_bot() flow is more complete and already works
  3. If the goal was to fix a bug where require_mention wasn't working, investigate why _telegram_require_mention() wasn't picking up the config (likely the YAML key wasn't being loaded into config.extra)

Cross-file Impact

Touches gateway/config.py and gateway/platforms/telegram.py. The config.py change is clean. The telegram.py changes conflict with existing logic.

Assessment

Request changes — The implementation is largely a duplicate of existing functionality. Keep the config.py YAML bridge (useful), but remove the redundant telegram.py changes and instead verify that the existing _telegram_require_mention() path correctly picks up the YAML-loaded env var.

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the contribution @butnull — closing as redundant. PR #3870 (merged March 29) added telegram.require_mention to main with the same behavior you proposed here. Your PR predated it by over a week and helped surface the need. Appreciate the work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants