fix: 修复 NapCat 语音消息的 Record 文件路径解析问题#7687
fix: 修复 NapCat 语音消息的 Record 文件路径解析问题#7687AmeiMiao wants to merge 2 commits intoAstrBotDevs:masterfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the convert_to_file_path and convert_to_base64 methods in astrbot/core/message/components.py to support both url and file attributes and corrects the temporary file extension for base64-encoded audio to .amr. Feedback suggests using ValueError for invalid inputs to maintain consistency with other components, refactoring the download logic into a shared helper function to prevent code duplication, and ensuring correct file extensions for audio downloads. Additionally, it was noted that new functionality should be accompanied by unit tests.
| file_path = await download_image_by_url(self.file) | ||
| url = self.url or self.file | ||
| if not url: | ||
| raise Exception(f"not a valid file: {url}") |
There was a problem hiding this comment.
为了与 Image 组件保持一致,建议在未提供文件或 URL 时抛出 ValueError。此外,新增的语音处理功能属于新功能,根据项目规则,应附带相应的单元测试。
| raise Exception(f"not a valid file: {url}") | |
| raise ValueError("No valid file or URL provided") |
References
- New functionality, such as handling attachments, should be accompanied by corresponding unit tests.
There was a problem hiding this comment.
已修改,这里现在改为抛出 ValueError("No valid file or URL provided"),与 Image 组件保持一致。
| file_path = await download_image_by_url(url) | ||
| return os.path.abspath(file_path) |
There was a problem hiding this comment.
此处使用 download_image_by_url(url) 会导致语音文件被保存为 .jpg 后缀。建议将此处的下载逻辑重构为一个共享的助手函数,以便在处理不同来源的语音附件时复用,避免代码重复并确保后缀名正确。
| file_path = await download_image_by_url(url) | |
| return os.path.abspath(file_path) | |
| file_path = os.path.join(get_astrbot_temp_path(), f"recordseg_{uuid.uuid4()}.amr") | |
| await download_file(url, file_path) | |
| return os.path.abspath(file_path) |
References
- When implementing similar functionality for different cases (e.g., direct vs. quoted attachments), refactor the logic into a shared helper function to avoid code duplication.
There was a problem hiding this comment.
已修改。现在 Record 在处理 HTTP URL 时不再复用 download_image_by_url,而是使用 download_file 并显式保存为 .amr 临时文件,避免语音下载后缀和处理方式不准确。
| file_path = await download_image_by_url(self.file) | ||
| url = self.url or self.file | ||
| if not url: | ||
| raise Exception(f"not a valid file: {url}") |
There was a problem hiding this comment.
已修改,这里现在改为抛出 ValueError("No valid file or URL provided"),与 Image 组件保持一致。
| file_path = await download_image_by_url(url) | ||
| bs64_data = file_to_base64(file_path) |
There was a problem hiding this comment.
此处逻辑与上方重复。建议按照规则将下载和保存语音的逻辑重构为共享的助手函数,以提高代码的可维护性并确保后缀名正确。
| file_path = await download_image_by_url(url) | |
| bs64_data = file_to_base64(file_path) | |
| file_path = os.path.join(get_astrbot_temp_path(), f"recordseg_{uuid.uuid4()}.amr") | |
| await download_file(url, file_path) | |
| bs64_data = file_to_base64(file_path) |
References
- When implementing similar functionality for different cases (e.g., direct vs. quoted attachments), refactor the logic into a shared helper function to avoid code duplication.
There was a problem hiding this comment.
这次我先尽量保持修复范围最小,优先解决 NapCat + Docker 场景下语音消息 Record 无法继续进入后续 STT 的问题,因此暂时没有继续做额外重构。
变更说明
修复 Docker + NapCat 场景下语音消息
Record组件的文件路径解析问题。当前
Record.convert_to_file_path和Record.convert_to_base64主要只使用self.file,在 NapCat 场景下,这个值可能只是文件名,或者是 AstrBot 容器内不可直接访问的路径,导致语音预处理时报错:Voice processing failed: not a valid file: xxx.amr本次修改让
Record和Image的处理逻辑保持一致,优先使用:self.url or self.file问题原因
在 Docker + NapCat 部署下,语音文件虽然已经在协议端成功下载,但 AstrBot 收到的
Record.file可能只是文件名或不可直接访问的路径。由于
Record组件没有像Image一样优先使用url,因此会把本来有效的语音消息误判为无效文件。修改内容
修改文件:
astrbot/core/message/components.py调整内容:
Record.convert_to_file_path改为优先使用self.url or self.fileRecord.convert_to_base64改为优先使用self.url or self.file.jpg调整为更合理的.amr验证结果
已在本地 Docker + NapCat 环境中验证:
not a valid file: xxx.amrCloses #7686
Summary by Sourcery
Fix voice Record message path resolution to correctly handle NapCat and Docker deployments by aligning its source selection with Image handling.
Bug Fixes:
Enhancements: