文件上传与下载
如果你要发送图片、音频、视频或文件,通常需要先上传资源,再在发送消息时引用上传结果。
上传资源
接口说明
上传文件到 TypeX 服务器,上传成功后会返回资源地址,可用于后续消息发送。
| 项目 | 说明 |
|---|---|
| 地址 | https://api-coco.typex.im/open/robot/upload |
| 方法 | POST |
| Content-Type | multipart/form-data |
请求头
Authorization: Bearer <bot_token>
Content-Type: multipart/form-data
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
chat_id | string | 是 | 会话 ID |
file_name | string | 是 | 文件名 |
file_type | string | 是 | 文件类型,支持:image / audio / video / application |
file_content | file | 是 | 文件内容,以 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_key | string | 是 | 上传时返回的 objectKey |
示例
curl -G 'https://api-coco.typex.im/open/robot/chat/file' \
-H 'Authorization: Bearer <bot_token>' \
--data-urlencode 'object_key=image/e2/e2f0069312cd5548'