上传和获取文件
约 272 字小于 1 分钟
2025-04-22
上传文件 && 发送文件
提示
由于 NapCat 的一些原因, 发送视频建议以上传文件的形式进行.
通过专用接口发送
from ncatbot.core import BotClient
bot = BotClient()
api = bot.run_backend(bt_uin="456789", root="987654") # 这里写你 bot 的 QQ 号
api.post_private_file_sync(user_id="987654", file = "path/to/file")
api.post_group_file_sync(group_id="695847", video = "path/to/video")
- 当 NcatBot 和 NapCat 不在同一台机器上时,必须写文件在 NcatBot 上的路径。这种情况有一些已知缺陷。
- 支持相对或绝对路径、 base64 字符串、URL 等多种格式。
提示
URL 没有任何缺陷,推荐使用。
通过文件消息发送
参考 MessageArray 和 MessageSegment。
from ncatbot.plugin_system import NcatBotPlugin, command_registry, filter_registry
from ncatbot.core import PrivateMessageEvent
from ncatbot.core import File, MessageArray
class MyPlugin(NcatBotPlugin):
name = "FileSender"
version = "1.0.0"
dependencies = {}
@filter_registry.private_filter
@command_registry("sendfile")
async def send_file(self, msg: PrivateMessageEvent):
msg_arr = MessageArray(File("path/to/file"))
await msg.reply(rtf=msg_arr)
- 这里的示例是插件格式。
- 发送文件时,MessageArray 中只能有一个 MessageSegment。
版权所有
版权归属:huan-yp