Skip to content

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_idint客户端 ID
file_pathstr本地文件路径
file_typeint文件类型
timeoutint | None超时时间

对应 type: 11115

文件类型

类型
1图片
2视频
3文件

返回值

上传成功后返回 file_idaes_keymd5size 等信息,可用于发送语音/小程序等需要 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_idint客户端 ID
aes_keystrAES 密钥
file_idstr文件 ID
save_pathstr保存路径
file_sizeint文件大小
file_typeint文件类型
timeoutint | 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_idint客户端 ID
urlstrCDN URL
auth_keystr认证密钥
aes_keystrAES 密钥
sizeint文件大小
save_pathstr保存路径
timeoutint | 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"
        )

Released under the MIT License.