Skip to content

fix: prevent KeyError in Telegram collect_commands when plugin handler not in star_map#7405

Open
michealmachine wants to merge 1 commit intoAstrBotDevs:masterfrom
michealmachine:fix/telegram-star-map-keyerror
Open

fix: prevent KeyError in Telegram collect_commands when plugin handler not in star_map#7405
michealmachine wants to merge 1 commit intoAstrBotDevs:masterfrom
michealmachine:fix/telegram-star-map-keyerror

Conversation

@michealmachine
Copy link
Copy Markdown

@michealmachine michealmachine commented Apr 7, 2026

Motivation / 动机

When using the astrbot_plugin_proactive_chat plugin (and potentially other plugins), the Telegram adapter logs the following error every 5 minutes repeatedly:

\
[ERRO] [telegram.tg_adapter:222]: 向 Telegram 注册指令时发生错误: 'data.plugins.astrbot_plugin_proactive_chat.core.message_events'
\\

The root cause is in \collect_commands()\ inside \ g_adapter.py. It iterates over \star_handlers_registry\ and accesses \star_map[handler_metadata.handler_module_path]\ directly using [], which raises a \KeyError\ when a plugin registers a handler whose module path is not yet present in \star_map\ (e.g. internal event modules that are scanned but not added to \star_map).

Confirmed on AstrBot v4.22.3 with \�strbot_plugin_proactive_chat\ v1.2.2.

Modifications / 改动点

  • \�strbot/core/platform/sources/telegram/tg_adapter.py\ line 231: added a key existence guard before accessing \star_map.

Before:
\\python
if not star_map[handler_metadata.handler_module_path].activated:
\\

After:
\\python
if handler_metadata.handler_module_path not in star_map or not star_map[handler_metadata.handler_module_path].activated:
\\

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

After applying the fix and restarting the container, the repeated error is gone:

\
[INFO] [telegram.tg_adapter:170]: Starting Telegram polling...
[INFO] [telegram.tg_adapter:174]: Telegram Platform Adapter is running.
\\


Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 My changes have been well-tested, and verification has been provided above.
  • 🤓 I have ensured that no new dependencies are introduced.
  • 😮 My changes do not introduce malicious code.

Summary by Sourcery

Bug Fixes:

  • Avoid KeyError in Telegram adapter command collection when a handler's module path is not present in star_map.

@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Apr 7, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • To avoid repeated dictionary lookups and keep the intent clearer, consider using star_map.get(handler_metadata.handler_module_path) once, assign it to a local variable, and then check both its existence and .activated state.
  • It might be useful to add a debug log when handler_module_path is not found in star_map so that unexpected plugin registrations can be detected and diagnosed more easily.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- To avoid repeated dictionary lookups and keep the intent clearer, consider using `star_map.get(handler_metadata.handler_module_path)` once, assign it to a local variable, and then check both its existence and `.activated` state.
- It might be useful to add a debug log when `handler_module_path` is not found in `star_map` so that unexpected plugin registrations can be detected and diagnosed more easily.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the Telegram adapter to include a safety check for the existence of a handler module path within the star map before accessing its activation status. This modification prevents potential KeyErrors during command collection. I have no feedback to provide as no review comments were submitted.

@dosubot dosubot bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant