跳到主要内容

文件上传与下载

如果你要发送图片、音频、视频或文件,通常需要先上传资源,再在发送消息时引用上传结果。

上传资源

接口说明

上传文件到 TypeX 服务器,上传成功后会返回资源地址,可用于后续消息发送。

项目说明
地址https://api-coco.typex.im/open/robot/upload
方法POST
Content-Typemultipart/form-data

请求头

Authorization: Bearer <bot_token>
Content-Type: multipart/form-data

请求参数

参数类型必填说明
chat_idstring会话 ID
file_namestring文件名
file_typestring文件类型,支持:image / audio / video / application
file_contentfile文件内容,以 multipart/form-data 上传

响应格式

{
"code": 0,
"data": {
"objectKey": "image/e2/e2f0069312cd5548",
"address": "https://api-coco.typex.im/chat/get/image/e2/e2f0069312cd5548/1",
"width": 1080,
"height": 719
},
"msg": "ok"
}
字段说明
data.objectKey资源唯一标识,下载时使用
data.address资源访问地址
data.width图片宽度(仅图片类型返回)
data.height图片高度(仅图片类型返回)

上传示例

上传图片

curl --location 'https://api-coco.typex.im/open/robot/upload' \
--header 'Authorization: Bearer <bot_token>' \
--form 'chat_id="7602566729281049630"' \
--form 'file_name="test_image"' \
--form 'file_type="image"' \
--form 'file_content=@"example.png"'

上传文件

curl --location 'https://api-coco.typex.im/open/robot/upload' \
--header 'Authorization: Bearer <bot_token>' \
--form 'chat_id="7602566729281049630"' \
--form 'file_name="test_file"' \
--form 'file_type="application"' \
--form 'file_content=@"example.zip"'

上传音频

curl --location 'https://api-coco.typex.im/open/robot/upload' \
--header 'Authorization: Bearer <bot_token>' \
--form 'chat_id="7602566729281049630"' \
--form 'file_name="test_voice"' \
--form 'file_type="audio"' \
--form 'file_content=@"example.wav"'

上传视频

curl --location 'https://api-coco.typex.im/open/robot/upload' \
--header 'Authorization: Bearer <bot_token>' \
--form 'chat_id="7602566729281049630"' \
--form 'file_name="test_video"' \
--form 'file_type="video"' \
--form 'file_content=@"example.mp4"'

下载回调中的文件

如果回调里出现文件资源地址,统一使用以下接口下载。

接口说明

项目说明
地址https://api-coco.typex.im/open/robot/chat/file
方法GET

请求头

Authorization: Bearer <bot_token>

请求参数

参数类型必填说明
object_keystring上传时返回的 objectKey

示例

curl -G 'https://api-coco.typex.im/open/robot/chat/file' \
-H 'Authorization: Bearer <bot_token>' \
--data-urlencode 'object_key=image/e2/e2f0069312cd5548'