feat: add OneBot V11 file API support for sending files#6404
feat: add OneBot V11 file API support for sending files#6404LIghtJUNction merged 2 commits intoAstrBotDevs:devfrom
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! This pull request enhances the file sending capabilities within the OneBot V11 platform by integrating its dedicated file APIs. This change improves the robustness and specificity of file transfers, allowing for more direct and appropriate handling of files in both group and private message contexts, and provides better feedback for invalid session identifiers. Highlights
Changelog
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:
- When
seg.file_andseg.urlare both empty,file_pathbecomes an empty string and is still passed tosend_group_file/send_private_file; consider explicitly detecting this case and either skipping the send or raising a clearer error before calling the API. - The session_id handling currently just logs a warning if
session_idis non-numeric or missing; if this can legitimately happen (e.g., other platforms or synthetic sessions), consider falling back to the previous_dispatch_sendbehavior or otherwise making the failure mode more graceful.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- When `seg.file_` and `seg.url` are both empty, `file_path` becomes an empty string and is still passed to `send_group_file`/`send_private_file`; consider explicitly detecting this case and either skipping the send or raising a clearer error before calling the API.
- The session_id handling currently just logs a warning if `session_id` is non-numeric or missing; if this can legitimately happen (e.g., other platforms or synthetic sessions), consider falling back to the previous `_dispatch_send` behavior or otherwise making the failure mode more graceful.
## Individual Comments
### Comment 1
<location path="astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py" line_range="160-169" />
<code_context>
- d = await cls._from_segment_to_dict(seg)
- await cls._dispatch_send(bot, event, is_group, session_id, [d])
+ # 使用 OneBot V11 文件 API 发送文件
+ file_path = seg.file_ or seg.url or ""
+ file_name = seg.name or "file"
+ session_id_int = (
+ int(session_id) if session_id and session_id.isdigit() else None
+ )
+ if is_group and session_id_int:
+ await bot.send_group_file(
+ group_id=session_id_int, file=file_path, name=file_name
+ )
+ elif session_id_int:
+ await bot.send_private_file(
+ user_id=session_id_int, file=file_path, name=file_name
+ )
</code_context>
<issue_to_address>
**issue (bug_risk):** Consider validating that `file_path` is non-empty before calling the OneBot file APIs.
Previously, `_from_segment_to_dict` likely guaranteed a valid payload. With the new logic, if both `seg.file_` and `seg.url` are missing, `file_path` will be an empty string and still passed to `send_group_file`/`send_private_file`. Adding a guard for an empty path (e.g., log and return early) would prevent confusing downstream API errors.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Code Review
你好,感谢你的贡献!
这个 PR 增加了通过 OneBot V11 专用文件 API 发送文件的支持,这是一个很棒的功能。
代码的整体实现是正确的,但我发现了一些可以改进的地方,主要是在处理文件路径和 session_id 的逻辑上。具体来说:
- 当文件路径为空时,代码可能会尝试发送一个空路径的文件,这可能会导致错误。
- 对
session_id的有效性检查可能会错误地将 ID0视为无效。
我在代码中提出了一个具体的修改建议,它修复了这些问题,并简化了逻辑,使其更加健壮和清晰。请查看我的评论。
再次感谢!
astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py
Outdated
Show resolved
Hide resolved
|
Thanks for the review! I've addressed the feedback in the latest commits:
The changes are now more robust and handle edge cases correctly. |
|
提交到dev将更快合并 |
|
你的源分支的基分支最好是dev分支,不然可能会导致冲突。 |
13c9e46 to
0a16df2
Compare
base branch已经是dev |
Description
Add support for sending files via OneBot V11's dedicated file APIs ( / ) instead of regular message API.
Changes
Modified :
Related Issue
Fixes #6403
Testing
This change uses the standard OneBot V11 file APIs which are supported by most OneBot V11 implementations (Go-CQHTTP, LLOneBot, etc.)
Summary by Sourcery
Add support for sending file segments via OneBot V11 dedicated file APIs for group and private messages, with validation of target session identifiers.
New Features:
Bug Fixes:
Enhancements: