Appearance
CDN
CDN 接口用于文件的上传和下载。
upload_c2c_cdn
上传文件到 C2C CDN。
python
result = wework.upload_c2c_cdn(
client_id: int,
file_path: str,
file_type: int,
timeout: Optional[int] = None
)| 参数 | 类型 | 说明 |
|---|---|---|
client_id | int | 客户端 ID |
file_path | str | 本地文件路径 |
file_type | int | 文件类型 |
timeout | int | None | 超时时间 |
对应 type: 11115
文件类型
| 值 | 类型 |
|---|---|
| 1 | 图片 |
| 2 | 视频 |
| 3 | 文件 |
返回值
上传成功后返回 file_id、aes_key、md5、size 等信息,可用于发送语音/小程序等需要 CDN 资源的消息。
download_c2c_cdn
从 C2C CDN 下载媒体文件(cdn_type=2)。
python
result = wework.download_c2c_cdn(
client_id: int,
aes_key: str,
file_id: str,
save_path: str,
file_size: int,
file_type: int,
timeout: Optional[int] = None
)| 参数 | 类型 | 说明 |
|---|---|---|
client_id | int | 客户端 ID |
aes_key | str | AES 密钥 |
file_id | str | 文件 ID |
save_path | str | 保存路径 |
file_size | int | 文件大小 |
file_type | int | 文件类型 |
timeout | int | None | 超时时间 |
对应 type: 11170
download_wx_cdn
从微信 CDN 下载文件(cdn_type=1)。
python
result = wework.download_wx_cdn(
client_id: int,
url: str,
auth_key: str,
aes_key: str,
size: int,
save_path: str,
timeout: Optional[int] = None
)| 参数 | 类型 | 说明 |
|---|---|---|
client_id | int | 客户端 ID |
url | str | CDN URL |
auth_key | str | 认证密钥 |
aes_key | str | AES 密钥 |
size | int | 文件大小 |
save_path | str | 保存路径 |
timeout | int | None | 超时时间 |
对应 type: 11171
使用示例
上传并发送语音
python
# 1. 上传语音文件到 CDN
upload_result = wework.upload_c2c_cdn(client_id, "voice.amr", 3)
# 2. 使用返回的信息发送语音
wework.send_voice(
client_id,
conversation_id,
file_id=upload_result["file_id"],
size=upload_result["size"],
aes_key=upload_result["aes_key"],
md5=upload_result["md5"]
)下载接收到的图片
python
@wework.handle(events.IMAGE_MESSAGE)
def on_image(bot: WeWork, event: dict):
data = event["data"]
# 根据 cdn_type 选择下载方式
if data.get("cdn_type") == 2:
bot.download_c2c_cdn(
event["client_id"],
aes_key=data["aes_key"],
file_id=data["file_id"],
save_path="./downloads/image.png",
file_size=data["size"],
file_type=1
)
else:
bot.download_wx_cdn(
event["client_id"],
url=data["url"],
auth_key=data["auth_key"],
aes_key=data["aes_key"],
size=data["size"],
save_path="./downloads/image.png"
)