Skip to content

feat: add OneBot V11 file API support for sending files#6404

Merged
LIghtJUNction merged 2 commits intoAstrBotDevs:devfrom
rin259:feat/onebot-file-send
Mar 16, 2026
Merged

feat: add OneBot V11 file API support for sending files#6404
LIghtJUNction merged 2 commits intoAstrBotDevs:devfrom
rin259:feat/onebot-file-send

Conversation

@rin259
Copy link
Contributor

@rin259 rin259 commented Mar 15, 2026

Description

Add support for sending files via OneBot V11's dedicated file APIs ( / ) instead of regular message API.

Changes

Modified :

  • When sending a segment, now uses:
    • for group messages
    • for private messages
  • Supports both local file paths () and URLs ()
  • Added warning log for invalid session_id

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:

  • Use OneBot V11 group and private file APIs when sending file segments instead of the generic message API.
  • Allow file segments to be sent using either local file paths or URLs as the file source.

Bug Fixes:

  • Prevent attempts to send files when the session_id is invalid by validating it before dispatching.

Enhancements:

  • Log a warning when a file send is skipped due to an invalid session_id to aid in debugging message routing issues.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 15, 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 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

  • OneBot V11 File API Integration: Implemented support for sending files using OneBot V11's dedicated file APIs (send_group_file and send_private_file) instead of the generic message API when a File segment is encountered.
  • Contextual File Sending: The system now intelligently uses send_group_file for group messages and send_private_file for private messages, ensuring proper routing of file transfers.
  • Flexible File Sources: The file sending mechanism now supports both local file paths and URLs as sources for the files being sent.
  • Improved Error Handling: A warning log has been added to alert users when a file cannot be sent due to an invalid session_id.
Changelog
  • astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py
    • Modified the send_message function to use bot.send_group_file for group messages and bot.send_private_file for private messages when handling File segments.
    • Added logic to extract file_path from either seg.file_ or seg.url.
    • Included a warning log for cases where session_id is invalid during file sending.
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.

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

  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

@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 found 1 issue, and left some high level feedback:

  • 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.
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>

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 15, 2026
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

你好,感谢你的贡献!

这个 PR 增加了通过 OneBot V11 专用文件 API 发送文件的支持,这是一个很棒的功能。

代码的整体实现是正确的,但我发现了一些可以改进的地方,主要是在处理文件路径和 session_id 的逻辑上。具体来说:

  • 当文件路径为空时,代码可能会尝试发送一个空路径的文件,这可能会导致错误。
  • session_id 的有效性检查可能会错误地将 ID 0 视为无效。

我在代码中提出了一个具体的修改建议,它修复了这些问题,并简化了逻辑,使其更加健壮和清晰。请查看我的评论。

再次感谢!

@rin259
Copy link
Contributor Author

rin259 commented Mar 15, 2026

Thanks for the review!

I've addressed the feedback in the latest commits:

  • Added check for empty file_path before sending
  • Fixed session_id check to handle ID 0 properly (using instead of truthy check)
  • Simplified the conditional logic

The changes are now more robust and handle edge cases correctly.

@LIghtJUNction
Copy link
Member

提交到dev将更快合并

@rin259 rin259 changed the base branch from master to dev March 16, 2026 02:19
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 16, 2026
@LIghtJUNction
Copy link
Member

你的源分支的基分支最好是dev分支,不然可能会导致冲突。

@rin259 rin259 force-pushed the feat/onebot-file-send branch from 13c9e46 to 0a16df2 Compare March 16, 2026 06:35
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Mar 16, 2026
@rin259
Copy link
Contributor Author

rin259 commented Mar 16, 2026

你的源分支的基分支最好是dev分支,不然可能会导致冲突。

base branch已经是dev

@LIghtJUNction
Copy link
Member

@rin259 合并后到这里跟踪进度:#6325

@LIghtJUNction LIghtJUNction merged commit 9c7c0ec into AstrBotDevs:dev Mar 16, 2026
2 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. size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: add support for sending files via OneBot V11 file API

2 participants