Skip to content

C2C 消息 API

适用于好友/用户间 C2C(Customer-to-Customer)消息场景。

post_c2c_message

发送 C2C 消息。

源码位置: botpy/api.py 第 1426 行

API 路由: POST /v2/users/{openid}/messages

python
async def post_c2c_message(
    openid: str,
    msg_type: int = 0,
    content: str = None,
    embed: Embed = None,
    ark: Ark = None,
    message_reference: Reference = None,
    media: Media = None,
    msg_id: str = None,
    msg_seq: int = 1,
    event_id: str = None,
    markdown: MarkdownPayload = None,
    keyboard: KeyboardPayload = None,
) -> Message
参数类型默认值说明
openidstr必填目标用户的 openid
msg_typeint0消息类型:0 文本,1 图文混排,2 markdown,3 ark,4 embed,7 media 富媒体
contentstrNone消息的文本内容
embedEmbedNoneembed 消息,一种特殊的 ark
arkArkNoneark 模板消息
message_referenceReferenceNone消息引用配置
mediaMediaNone富媒体消息(需先通过 post_c2c_file 上传文件)
msg_idstrNone被回复消息的 ID,可从事件中获取
msg_seqint1回复消息的序号,与 msg_id 联合使用。相同的 msg_id + msg_seq 重复发送会失败
event_idstrNone被回复消息的事件 ID
markdownMarkdownPayloadNonemarkdown 消息内容
keyboardKeyboardPayloadNonekeyboard 消息按钮

返回: Message

注意:

  • 发送成功之后,会触发一个创建消息的事件
  • 被动回复消息有效期为 5 分钟
  • 发送消息接口要求机器人需要连接到 WebSocket gateway 保持在线状态

post_c2c_file

上传/发送 C2C 富媒体文件。

源码位置: botpy/api.py 第 1493 行

API 路由: POST /v2/users/{openid}/files

python
async def post_c2c_file(
    openid: str,
    file_type: int,
    url: str,
    srv_send_msg: bool = False,
) -> Media
参数类型默认值说明
openidstr必填目标用户的 openid
file_typeint必填媒体类型(见下方说明)
urlstr必填媒体资源的 URL
srv_send_msgboolFalse设为 True 时直接发送消息到目标端,会占用主动消息频次

file_type 说明:

类型说明
1图片png/jpg
2视频mp4
3语音silk
4文件暂不开放

返回: Media

Media 字段:

字段类型说明
file_uuidstr文件 ID
file_infostr文件信息,用于 post_c2c_messagemedia 字段
ttlint有效期(秒),到期后 file_info 失效,等于 0 时可长期使用

使用示例:

python
# 发送文本回复
await message._api.post_c2c_message(
    openid=message.author.user_openid,
    msg_type=0,
    msg_id=message.id,
    content=f"我收到了你的消息:{message.content}",
)

# 发送富媒体
uploadMedia = await message._api.post_c2c_file(
    openid=message.author.user_openid,
    file_type=1,
    url="https://example.com/image.png",
)
await message._api.post_c2c_message(
    openid=message.author.user_openid,
    msg_type=7,
    msg_id=message.id,
    media=uploadMedia,
)