Skip to content

fix(platform.tg_adapter): import Forbidden instead of deprecated Unauthorized (#6765)#6769

Merged
Soulter merged 1 commit intoAstrBotDevs:masterfrom
mzwing:master
Mar 22, 2026
Merged

fix(platform.tg_adapter): import Forbidden instead of deprecated Unauthorized (#6765)#6769
Soulter merged 1 commit intoAstrBotDevs:masterfrom
mzwing:master

Conversation

@mzwing
Copy link
Contributor

@mzwing mzwing commented Mar 21, 2026

Fix #6765 .

Modifications / 改动点

Edited file:

  • astrbot/core/platform/sources/telegram/tg_adapter.py

Functions:

  • Nothing but use Forbidden instead of python-telegram-bot's deprecated Unauthorized to let telegram bot adapter work again.

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

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

Log:

[2026-03-22 03:21:31.421] [Core] [INFO] [core.core_lifecycle:297]: AstrBot 启动完成。
[2026-03-22 03:21:31.422] [Core] [INFO] [utils.temp_dir_cleaner:127]: TempDirCleaner started. interval=600s cleanup_ratio=0.3
[2026-03-22 03:21:31.567] [Core] [INFO] [t2i.network_strategy:56]: Successfully got 2 official T2I endpoints.
[2026-03-22 03:21:32.054] [Core] [INFO] [utils.llm_metadata:63]: Successfully fetched metadata for 2213 LLMs.
[2026-03-22 03:21:44.262] [Core] [INFO] [routes.config:307]: Saving config, is_core=True
[2026-03-22 03:21:44.265] [Core] [INFO] [platform.manager:125]: 载入 telegram(mzwing's AstrBot) 平台适配器 ...
[2026-03-22 03:21:44.922] [Core] [DBUG] [platform.register:60]: 平台适配器 telegram 已注册
[2026-03-22 03:21:47.088] [Core] [INFO] [telegram.tg_adapter:171]: Starting Telegram polling...
[2026-03-22 03:21:47.447] [Core] [INFO] [telegram.tg_adapter:175]: Telegram Platform Adapter is running.

Note:

  • The log line which contains my bot token has been manually deleted.

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Bug Fixes:

  • Restore Telegram adapter error handling for invalid or unauthorized bot tokens by switching to the supported Forbidden exception.

Copilot AI review requested due to automatic review settings March 21, 2026 19:31
@auto-assign auto-assign bot requested review from LIghtJUNction and anka-afk March 21, 2026 19:31
@dosubot dosubot bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Mar 21, 2026
Copy link
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:

  • Now that Forbidden is used instead of the deprecated Unauthorized, consider updating the log message to reflect the broader meaning of Forbidden (e.g., chat/bot restrictions) rather than only saying the token is invalid or unauthorized.
  • Depending on how Forbidden is raised by python-telegram-bot, you may want to distinguish between invalid token (InvalidToken) and other forbidden scenarios in separate except blocks so you can react or message users differently.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Now that `Forbidden` is used instead of the deprecated `Unauthorized`, consider updating the log message to reflect the broader meaning of `Forbidden` (e.g., chat/bot restrictions) rather than only saying the token is invalid or unauthorized.
- Depending on how `Forbidden` is raised by python-telegram-bot, you may want to distinguish between invalid token (`InvalidToken`) and other forbidden scenarios in separate except blocks so you can react or message users differently.

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.

@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 Mar 21, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses an issue where the Unauthorized exception used in the Telegram bot adapter has been deprecated. It replaces this with the Forbidden exception to ensure the adapter continues to function correctly and handle authorization errors as intended. The change maintains compatibility and does not introduce any breaking changes or new dependencies.

Highlights

  • Error Handling: Replaced the deprecated Unauthorized exception with Forbidden from the python-telegram-bot library to handle authorization errors correctly.
  • Platform Adapter: Ensured the Telegram bot adapter continues to function by using the appropriate exception for handling permission issues.
  • Dependency Update: No new dependencies were introduced, maintaining the project's dependency structure.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes the Telegram platform adapter failing to load with newer python-telegram-bot versions by replacing the deprecated/removed Unauthorized import/exception with Forbidden, aligning the adapter with the dependency version constraints.

Changes:

  • Replace telegram.error.Unauthorized with telegram.error.Forbidden in imports.
  • Update polling startup exception handling to catch Forbidden instead of Unauthorized.

Copy link
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 correctly replaces the deprecated Unauthorized exception with Forbidden from the python-telegram-bot library. The changes in astrbot/core/platform/sources/telegram/tg_adapter.py are accurate and address the issue of using a deprecated API. The modifications are correct and align with the library's new API. I have reviewed the changes and found no issues.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Mar 22, 2026
@Soulter Soulter merged commit 3e68b7a into AstrBotDevs:master Mar 22, 2026
8 of 9 checks passed
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. lgtm This PR has been approved by a maintainer size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Cannot use Telegram Bot

3 participants