Skip to content

feat: support multiple Telegram bots in a single gateway instance#227

Closed
yibie wants to merge 2 commits into
bubbuild:mainfrom
yibie:feat/multi-telegram-bot
Closed

feat: support multiple Telegram bots in a single gateway instance#227
yibie wants to merge 2 commits into
bubbuild:mainfrom
yibie:feat/multi-telegram-bot

Conversation

@yibie

@yibie yibie commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Feature

一个 实例现在可以同时运行多个 Telegram bot,彼此完全隔离。

用法

向后兼容(单 bot 不变):

BUB_TELEGRAM_TOKEN=123:abc

多 bot 模式

BUB_TELEGRAM_BOTS='[{"name":"personal","token":"111:aaa","allow_users":"123"},{"name":"community","token":"222:bbb","allow_chats":"456"}]'

改动

  • 新增 BotConfig pydantic 模型,封装单个 bot 的完整配置(token、allow_users、allow_chats、proxy)
  • TelegramSettings.bots 字段支持 JSON 数组输入
  • TelegramSettings.bot_configs() 统一返回 list[BotConfig](兼容单 bot 和多 bot)
  • TelegramChannel 名称动态化:telegram-{name} 或默认 telegram
  • provide_channels 为每个 BotConfig 创建一个 TelegramChannel

隔离保证

维度 机制
Channel 路由 每个 bot 唯一 name
对话历史 session_id 包含 channel name,天然隔离
用户鉴权 每个 BotConfig 独立 allow_users/allow_chats
代理 每个 BotConfig 独立 proxy

验证

uv run pytest -q
187 passed

yibie added 2 commits June 7, 2026 01:20
Replace ContextVar.reset(token) with save/restore pattern to avoid
'Token was created in a different Context' ValueError when the async
generator cleanup runs in a different asyncio Task (e.g. cancellation,
TaskGroup exception propagation).
- Add BotConfig pydantic model for per-bot configuration
- Add TelegramSettings.bots field (JSON array) for multi-bot mode
- TelegramSettings.bot_configs() returns list of BotConfig
- Backward compatible: BUB_TELEGRAM_TOKEN still works for single bot
- TelegramChannel name is now dynamic: telegram-{name} or 'telegram'
- provide_channels creates one TelegramChannel per BotConfig
@yibie yibie closed this Jun 9, 2026
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.

1 participant