fix: GIF sent as static image in Telegram adapter#6329
Conversation
Summary of ChangesHello, 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! 此拉取请求解决了 Astrbot 在 Telegram 适配器中发送 GIF 表情时,GIF 以静态图片而非动图形式呈现的问题。通过引入 GIF 文件检测机制并调整发送逻辑,确保所有通过 Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The
_is_gifhelper performs synchronous disk I/O in the hot async send path; consider moving GIF detection closer to where the file is created or cached (e.g., as metadata on theImageobject) to avoid reopening the file each time it’s sent. - Catching a bare
Exceptionin_is_gifmay hide unexpected errors (e.g., permission issues); consider narrowing this toOSError/IOErroror at least logging unexpected exceptions before returningFalse.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The `_is_gif` helper performs synchronous disk I/O in the hot async send path; consider moving GIF detection closer to where the file is created or cached (e.g., as metadata on the `Image` object) to avoid reopening the file each time it’s sent.
- Catching a bare `Exception` in `_is_gif` may hide unexpected errors (e.g., permission issues); consider narrowing this to `OSError`/`IOError` or at least logging unexpected exceptions before returning `False`.
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/telegram/tg_event.py" line_range="28-34" />
<code_context>
from astrbot.core.utils.metrics import Metric
+def _is_gif(path: str) -> bool:
+ if str(path).lower().endswith('.gif'):
+ return True
+ try:
+ with open(path, 'rb') as f:
+ return f.read(6) in (b'GIF87a', b'GIF89a')
+ except Exception:
+ return False
+
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Avoid catching bare `Exception` in `_is_gif` and consider narrowing or handling specific I/O errors
This masks unrelated failures (e.g., permission/IO errors) and makes real problems look like a non‑GIF. Please narrow the except to expected I/O exceptions (e.g., `OSError`/`IOError`) and/or log unexpected errors before returning `False` so they’re visible during debugging.
Suggested implementation:
```python
import logging
from astrbot.core.utils.metrics import Metric
logger = logging.getLogger(__name__)
```
```python
def _is_gif(path: str) -> bool:
if str(path).lower().endswith(".gif"):
return True
try:
with open(path, "rb") as f:
return f.read(6) in (b"GIF87a", b"GIF89a")
except (OSError, IOError):
# Expected I/O-related problems (file not found, permission denied, etc.)
return False
except Exception:
# Unexpected failure: log with stack trace so it is visible during debugging
logger.exception("Unexpected error while checking if %r is a GIF", path)
return False
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
Fixes AstrBot’s Telegram adapter so that GIFs sent via the Image component are delivered as animations (not static photos) by routing GIFs to Telegram’s send_animation API.
Changes:
- Added
_is_gif()helper to detect GIFs via file extension or GIF magic header. - Updated
send_with_client()to usesend_animationfor detected GIFs, otherwisesend_photo. - Updated
_process_chain_items()to usesend_animationfor detected GIFs and useChatAction.UPLOAD_VIDEOfor the corresponding upload action.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
* fix(telegram): route GIF files to send_animation instead of send_photo * fix: narrow exception in _is_gif to OSError Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * refactor: simplify image send dispatch in send_with_client Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * refactor: simplify image dispatch in _process_chain_items * ruff format --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Soulter <905617992@qq.com>
* fix(telegram): route GIF files to send_animation instead of send_photo * fix: narrow exception in _is_gif to OSError Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * refactor: simplify image send dispatch in send_with_client Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * refactor: simplify image dispatch in _process_chain_items * ruff format --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> Co-authored-by: Soulter <905617992@qq.com>
在 Astrbot 中 使用 Telegram bot 时,bot 发送的表情如果为 GIF ,则会以静态图片形式呈现,而非动图。原因是适配器将所有 Image 组件统一路由到 send_photo,而 send_photo 只发送静态图片,不支持动图。此问题影响所有通过
Image组件发送 GIF 的插件。Modifications / 改动点
修改了
astrbot/core/platform/sources/telegram/tg_event.py:新增辅助函数
_is_gif(),通过文件扩展名或魔数(GIF87a/GIF89a)检测 GIF 文件在
send_with_client()中:检测到 GIF 时改用send_animation发送在
_process_chain_items中:检测到 GIF 时改用send_animation,并将 ChatAction 修正为UPLOAD_VIDEOThis is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
修复前截图




修复后截图
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
Bug Fixes: