开放 API 接入文档
WeRobot 开放 API 面向已开通服务的用户,支持您将微信机器人能力接入自有订单、CRM、客服系统。 所有接口统一使用 JSON 格式,API Key 鉴权,异步任务模型。
简介
开放 API 只暴露写操作任务下发,查询类数据请直接通过管理后台查看。典型接入场景:
- 订单发货后,自动给客户微信发通知(好友 / 群聊)
- 运营系统定时给指定群发直播预告海报
- 朋友圈定时发布产品图文
- 活动预热时,批量给会员推送活动海报
获取 API Key
API Key 由用户账号在管理后台「API 密钥」页面创建,操作步骤:
- 登录平台管理后台(使用您的用户账号)
- 进入「API 密钥」模块,点击「创建密钥」
- 填写密钥名称(便于识别用途),创建后完整显示一次 —— 请立即复制保存
鉴权方式
所有开放接口均需在请求头中携带 API Key,支持两种写法,任选其一:
方式一:X-Api-Key(推荐)
X-Api-Key: wrk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
方式二:Authorization Bearer
Authorization: Bearer wrk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
服务端优先读取 X-Api-Key,缺失时再回退到 Authorization 头。
通用格式
请求
- 方法:所有开放接口均使用
POST - Content-Type:
application/json - 请求体:JSON 对象
统一响应结构
{
"status": 200, // HTTP 状态码
"success": true, // 是否成功
"data": { ... }, // 业务数据,失败时为空对象 {}
"msg": "错误描述" // 仅失败时返回
}
频率限制
超限返回 429 Too Many Requests。发送类任务进入队列异步执行,响应返回 taskId。
接口参考
全部开放接口汇总如下。两个入口均为同一个路径,通过 type 参数区分。
统一任务入口,type=message 发送消息(好友 / 群聊,文本 / 图片 / 卡片链接),type=moment 发布朋友圈。
发送消息(好友 / 群聊,文本 / 图片 / 卡片链接)
通过 type=message 发送消息。目标可以是单个好友或单个群聊,类型支持文本、图片、链接卡片。
请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | string | 否 | 固定传 message(默认值) |
| robotWxId | string | 是 | 执行发送的机器人 wxid(必须属于当前 API Key 关联的用户) |
| friendWxId | string | 是 | 目标 wxid。好友传 wxid_xxx;群聊传以 @chatroom 结尾的群 wxid |
| content | string | 是* | 文本正文 / 图片 URL。卡片消息可省略,改由卡片字段生成 |
| contentType | int | 否 | 1=文本(默认),2=图片,6=链接卡片 |
| delaySeconds | int | 否 | 延迟发送秒数,0–86400,默认 0 即立即发送 |
| card | object | 否 | 链接卡片内容对象,见下方「卡片字段」 |
| title / desc / url / thumb / source | string | 否 | 卡片字段的平铺写法,等价于 card 内的同名字段 |
contentType 取值
| 值 | 类型 | content 字段含义 | 示例 |
|---|---|---|---|
| 1 | 文本 | 消息正文,直接传文字 | "您好,您的订单已发货" |
| 2 | 图片 | 可公开访问的 HTTP/HTTPS 图片 URL | "https://cdn.example.com/a.jpg" |
| 6 | 链接卡片 | 由 card 或平铺字段构建,content 可省略 | 见下方卡片示例 |
卡片字段(contentType=6)
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| title | string | 是 | 卡片标题 |
| url | string | 是 | 点击卡片跳转的链接,必须 http:// 或 https:// 开头 |
| desc | string | 否 | 卡片描述文字 |
| thumb | string | 否 | 卡片缩略图 URL(兼容 thumbUrl),必须 http/https |
| source | string | 否 | 卡片来源名称(如公众号名) |
friendWxId 取值
| 目标 | friendWxId 格式 | 示例 |
|---|---|---|
| 好友 | 普通 wxid | wxid_abc123xxxxxxxx |
| 群聊 | 以 @chatroom 结尾 | 29xxxxxxxxxxxxxxxxx@chatroom |
http:// 或 https:// 开头的公开可访问链接,微信设备端能够直接下载。推荐使用七牛云、阿里云 OSS、腾讯云 COS 等对象存储的公开访问地址。
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "wxid_yyyyyyyyyyyyyyyy",
"content": "您好,您的订单 20261124001 已发货,快递单号 SF1234567890,请注意查收。",
"contentType": 1
}
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "wxid_yyyyyyyyyyyyyyyy",
"content": "https://cdn.example.com/images/product_demo.jpg",
"contentType": 2
}
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "29xxxxxxxxxxxxxxxxx@chatroom",
"content": "各位好,今晚 20:00 我们在视频号有一场「年终大促玩法」直播,欢迎准时进入直播间。",
"contentType": 1
}
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "29xxxxxxxxxxxxxxxxx@chatroom",
"content": "https://cdn.example.com/images/event_poster.jpg",
"contentType": 2
}
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "wxid_yyyyyyyyyyyyyyyy",
"contentType": 6,
"card": {
"title": "年终大促正式开启",
"desc": "全场低至 3 折,还有超值满减等你来",
"url": "https://example.com/promo",
"thumb": "https://cdn.example.com/images/promo_cover.jpg",
"source": "官方商城"
}
}
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "29xxxxxxxxxxxxxxxxx@chatroom",
"contentType": 6,
"title": "今晚 20:00 直播预告",
"desc": "年终大促玩法详解,点击预约直播间",
"url": "https://example.com/live/20261124",
"thumb": "https://cdn.example.com/images/live_cover.jpg",
"source": "官方直播间"
}
card 对象或平铺字段均可,二者等价(card 内字段优先)。
只要传了 card 对象或 url,服务端会自动按链接卡片下发,无需额外指定 contentType。
{
"type": "message",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "wxid_yyyyyyyyyyyyyyyy",
"content": "温馨提醒:您预约的服务将于今天 14:00 开始,请准时到达门店。",
"contentType": 1,
"delaySeconds": 1800
}
统一响应
{
"status": 200,
"success": true,
"data": {
"taskId": 10000001234,
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"friendWxId": "wxid_yyyyyyyyyyyyyyyy",
"scheduleTime": 1732416000
}
}
taskId — 任务唯一 ID;scheduleTime — 实际执行时间的 Unix 时间戳。
发布朋友圈
通过 type=moment 向指定机器人发布朋友圈,支持纯文本、图片、链接卡片。
请求参数
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| type | string | 是 | 固定传 moment |
| robotWxId | string | 是 | 发布朋友圈的机器人 wxid |
| content | string | 是* | 朋友圈正文,与附件至少一项不能为空 |
| attachmentType | int | 否 | 0=无附件 / 1=图片 / 2=链接 |
| attachmentContent | string | 否 | 附件内容:图片时传 URL 或 JSON 数组(最多 4 张);链接时传 JSON |
| sendSlow | bool | 否 | 慢速发送,默认 false |
attachmentContent 取值
| attachmentType | attachmentContent 格式 | 示例 |
|---|---|---|
| 1(图片) | 单张 URL 或 JSON 数组(最多 4 张) | "https://cdn.example.com/a.jpg" 或 ["url1","url2"] |
| 2(链接) | JSON 对象:{title, desc, url, thumb, source} | 见下方示例 |
{
"type": "moment",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"content": "今日份下午茶,和团队一起庆祝本月超额完成 KPI 🎉"
}
{
"type": "moment",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"content": "新品发布会现场,感谢每一位到场的朋友!",
"attachmentType": 1,
"attachmentContent": "https://cdn.example.com/images/launch_event.jpg"
}
{
"type": "moment",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"content": "欢迎点击查看本期活动详情",
"attachmentType": 2,
"attachmentContent": "{\"title\":\"年终大促正式开启\",\"desc\":\"全场低至 3 折,还有超值满减\",\"url\":\"https://example.com/promo\",\"thumb\":\"https://cdn.example.com/banner.jpg\",\"source\":\"官方公众号\"}"
}
统一响应
{
"status": 200,
"success": true,
"data": {
"taskId": 10000001235
}
}
自定义推送通道(Webhook)
开放 API 是 您调用我们 下发任务;自定义推送通道是 我们主动推给您 事件。 当微信设备收到消息、或设备上线/下线/到期时,平台会向您配置的 Webhook URL 异步 POST 一份 JSON。
配置入口
在管理后台 → 系统设置 → 推送通知中填写以下字段:
| 字段 | 用途 | 说明 |
|---|---|---|
| 消息推送开关(好友) | 收到好友消息时推送 | 勾选后,私人消息才会推送 |
| 消息推送开关(群) | 收到群消息时推送 | 勾选后,群消息才会推送 |
| 消息推送 URL | 自定义 Webhook URL | 必须是可公开访问的 HTTP/HTTPS 地址 |
| 设备上线通知 | 设备上线时触发 | 可选企业微信 / 钉钉 Webhook,或指定机器人给指定好友发消息 |
| 设备下线通知 | 设备离线(持续 60 秒)时触发 | 同上线通道 |
| 到期提醒 | 会员到期前 N 天 | 每天最多提醒一次 |
推送方向与通路
请求格式
| 方法 | POST |
| Content-Type | application/json; charset=utf-8 |
| 鉴权 | 无(Webhook URL 本身就是凭证,请妥善保管,避免在客户端泄露) |
| 响应 | 任意 HTTP 2xx 即视为成功;4xx/5xx 会记录 warn 日志 |
事件类型与 Payload
所有推送的 Content-Type 均为 application/json,body 是一个顶层包含 event 字段的对象,
字段随事件类型不同而不同。
机器人收到好友的微信消息时触发。需在后台开启「消息推送开关(好友)」。
Payload 字段
| 字段 | 类型 | 说明 |
|---|---|---|
| event | string | 固定 message |
| chatType | string | private |
| robotWxId | string | 接收消息的机器人 wxid |
| robotNick | string | 机器人昵称(可能为空) |
| friendWxId | string | 发送者 wxid |
| friendNick | string | 发送者昵称 |
| fromWxId | string | 发送者原始 wxid,与 friendWxId 一致 |
| msgType | string | text / image / voice / video / emoji 等 |
| content | string | 消息内容。文本即原文;图片为缩略图信息或 JSON;语音为音频地址 |
| time | int | 推送时间的 Unix 时间戳(秒) |
| datetime | string | 推送时间,格式 YYYY-MM-DD HH:mm:ss |
Payload 示例
{
"event": "message",
"chatType": "private",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"robotNick": "客服小张",
"friendWxId": "wxid_yyyyyyyyyyyyyyyy",
"friendNick": "张三",
"fromWxId": "wxid_yyyyyyyyyyyyyyyy",
"msgType": "text",
"content": "你好,请问订单 F20261124001 发货了吗?",
"time": 1732416000,
"datetime": "2026-11-24 10:00:00"
}
机器人所在的群收到消息时触发。需在后台开启「消息推送开关(群)」。
Payload 字段
| 字段 | 类型 | 说明 |
|---|---|---|
| event | string | 固定 message |
| chatType | string | group |
| robotWxId | string | 机器人 wxid |
| robotNick | string | 机器人昵称 |
| groupWxId | string | 群 wxid,以 @chatroom 结尾 |
| groupName | string | 群名称 |
| fromWxId | string | 群内发送者 wxid |
| targetWxId | string | 同 groupWxId |
| targetName | string | 同 groupName |
| msgType | string | text / image / voice 等 |
| content | string | 消息正文(群 @ 某人时文本里不含 @ 提及) |
| time | int | 推送时间戳 |
| datetime | string | 推送时间 |
Payload 示例
{
"event": "message",
"chatType": "group",
"robotWxId": "wxid_xxxxxxxxxxxxxxxx",
"robotNick": "客服小张",
"groupWxId": "29xxxxxxxxxxxxxxxxx@chatroom",
"groupName": "产品交流群",
"fromWxId": "wxid_aaaaaaaaaaaaaaaa",
"targetWxId": "29xxxxxxxxxxxxxxxxx@chatroom",
"targetName": "产品交流群",
"msgType": "text",
"content": "请问新版本什么时候上线?",
"time": 1732416300,
"datetime": "2026-11-24 10:05:00"
}
这两个事件不走自定义 Webhook URL,而是按后台「推送通知」设置推送到企业微信 / 钉钉 Webhook,或通过指定机器人给指定好友发送文本消息。
接收端实现示例
以下为自建后端接收推送的参考实现,按实际语言选用其一。所有示例都做了:返回 200、异步处理、日志完整。
from flask import Flask, request import json app = Flask(__name__) @app.route('/wechat/webhook', methods=['POST']) def webhook(): payload = request.get_json(force=True) event = payload.get('event') if event == 'message': chat_type = payload.get('chatType') if chat_type == 'private': # 好友消息:同步订单等业务系统 print(f"[好友] {payload.get('friendNick')}: {payload.get('content')}") handle_friend_message(payload) elif chat_type == 'group': # 群消息 print(f"[群] {payload.get('groupName')} - {payload.get('content')}") handle_group_message(payload) return json.dumps({'ok': True}), 200, {'Content-Type': 'application/json'} def handle_friend_message(payload): pass # 你的业务逻辑:比如转发给客服系统、触发自动回复等 def handle_group_message(payload): pass
const express = require('express'); const app = express(); app.use(express.json({ limit: '2mb' })); app.post('/wechat/webhook', (req, res) => { const payload = req.body; if (payload.event === 'message') { if (payload.chatType === 'private') { console.log(`[好友] ${payload.friendNick}: ${payload.content}`); handleFriend(payload); } else if (payload.chatType === 'group') { console.log(`[群] ${payload.groupName} - ${payload.content}`); handleGroup(payload); } } res.json({ ok: true }); }); function handleFriend(payload) { // TODO: 转发到客服系统 / 触发自动回复 / 写入数据库 } function handleGroup(payload) {}
$body = file_get_contents('php://input');
$payload = json_decode($body, true);
if (!is_array($payload)) {
http_response_code(400);
echo json_encode(['ok' => false, 'msg' => 'invalid json']);
exit;
}
$event = $payload['event'] ?? '';
if ($event === 'message') {
$chatType = $payload['chatType'] ?? '';
if ($chatType === 'private') {
// 好友消息
error_log("[好友] " . ($payload['friendNick'] ?? '') . ": " . ($payload['content'] ?? ''));
} elseif ($chatType === 'group') {
// 群消息
error_log("[群] " . ($payload['groupName'] ?? '') . " - " . ($payload['content'] ?? ''));
}
}
// 必须尽快返回 200,后续业务逻辑建议异步处理
http_response_code(200);
header('Content-Type: application/json');
echo json_encode(['ok' => true]);
状态码
快速开始
把 YOUR_DOMAIN 替换为您的服务域名,wrk_your_api_key_here 替换为您在后台创建的 API 密钥。
# 1. 给好友发文本 — 订单通知 curl -X POST "YOUR_DOMAIN/api/open/task" \ -H "X-Api-Key: wrk_your_api_key_here" \ -H "Content-Type: application/json" \ -d '{ "type": "message", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "friendWxId": "wxid_yyyyyyyyyyyyyyyy", "content": "您好,您的订单已发货,请注意查收。" }' # 2. 给群聊发图片 — 活动海报 curl -X POST "YOUR_DOMAIN/api/open/task" \ -H "X-Api-Key: wrk_your_api_key_here" \ -H "Content-Type: application/json" \ -d '{ "type": "message", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "friendWxId": "29xxxxxxxxxxxxxxxxx@chatroom", "content": "https://cdn.example.com/promo.jpg", "contentType": 2 }' # 3. 给好友发链接卡片 curl -X POST "YOUR_DOMAIN/api/open/task" \ -H "X-Api-Key: wrk_your_api_key_here" \ -H "Content-Type: application/json" \ -d '{ "type": "message", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "friendWxId": "wxid_yyyyyyyyyyyyyyyy", "contentType": 6, "card": { "title": "年终大促正式开启", "desc": "全场低至 3 折,超值满减等你来", "url": "https://example.com/promo", "thumb": "https://cdn.example.com/promo_cover.jpg", "source": "官方商城" } }' # 4. 发朋友圈 — 图文 curl -X POST "YOUR_DOMAIN/api/open/task" \ -H "X-Api-Key: wrk_your_api_key_here" \ -H "Content-Type: application/json" \ -d '{ "type": "moment", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "content": "新品发布,欢迎围观!", "attachmentType": 1, "attachmentContent": "https://cdn.example.com/product.jpg" }'
import requests API_KEY = "wrk_your_api_key_here" DOMAIN = "YOUR_DOMAIN" headers = {"X-Api-Key": API_KEY} # 给好友发文本 resp = requests.post(f"{DOMAIN}/api/open/task", headers=headers, json={ "type": "message", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "friendWxId": "wxid_yyyyyyyyyyyyyyyy", "content": "您好,您的订单已发货,请注意查收。" }) print(resp.json()) # 给群聊发图片 resp = requests.post(f"{DOMAIN}/api/open/task", headers=headers, json={ "type": "message", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "friendWxId": "29xxxxxxxxxxxxxxxxx@chatroom", "content": "https://cdn.example.com/promo.jpg", "contentType": 2 }) print(resp.json()) # 给好友发链接卡片 resp = requests.post(f"{DOMAIN}/api/open/task", headers=headers, json={ "type": "message", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "friendWxId": "wxid_yyyyyyyyyyyyyyyy", "contentType": 6, "card": { "title": "年终大促正式开启", "desc": "全场低至 3 折,超值满减等你来", "url": "https://example.com/promo", "thumb": "https://cdn.example.com/promo_cover.jpg", "source": "官方商城" } }) print(resp.json()) # 发朋友圈 resp = requests.post(f"{DOMAIN}/api/open/task", headers=headers, json={ "type": "moment", "robotWxId": "wxid_xxxxxxxxxxxxxxxx", "content": "新品发布,欢迎围观!", "attachmentType": 1, "attachmentContent": "https://cdn.example.com/product.jpg" })
const API_KEY = 'wrk_your_api_key_here'; const DOMAIN = 'YOUR_DOMAIN'; async function send(payload) { return fetch(`${DOMAIN}/api/open/task`, { method: 'POST', headers: { 'X-Api-Key': API_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }).then(r => r.json()); } // 给好友发文本 send({ type: 'message', robotWxId: 'wxid_xxxxxxxxxxxxxxxx', friendWxId: 'wxid_yyyyyyyyyyyyyyyy', content: '您好,您的订单已发货,请注意查收。' }).then(console.log); // 给群聊发图片 send({ type: 'message', robotWxId: 'wxid_xxxxxxxxxxxxxxxx', friendWxId: '29xxxxxxxxxxxxxxxxx@chatroom', content: 'https://cdn.example.com/promo.jpg', contentType: 2 }).then(console.log); // 给好友发链接卡片 send({ type: 'message', robotWxId: 'wxid_xxxxxxxxxxxxxxxx', friendWxId: 'wxid_yyyyyyyyyyyyyyyy', contentType: 6, card: { title: '年终大促正式开启', desc: '全场低至 3 折,超值满减等你来', url: 'https://example.com/promo', thumb: 'https://cdn.example.com/promo_cover.jpg', source: '官方商城' } }).then(console.log); // 发朋友圈 send({ type: 'moment', robotWxId: 'wxid_xxxxxxxxxxxxxxxx', content: '新品发布,欢迎围观!', attachmentType: 1, attachmentContent: 'https://cdn.example.com/product.jpg' }).then(console.log);
$apiKey = 'wrk_your_api_key_here'; $domain = 'YOUR_DOMAIN'; function open($domain, $apiKey, $payload) { $ch = curl_init("$domain/api/open/task"); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ "X-Api-Key: $apiKey", "Content-Type: application/json", ], CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE), ]); $resp = curl_exec($ch); curl_close($ch); return json_decode($resp, true); } // 给好友发文本 open($domain, $apiKey, [ 'type' => 'message', 'robotWxId' => 'wxid_xxxxxxxxxxxxxxxx', 'friendWxId' => 'wxid_yyyyyyyyyyyyyyyy', 'content' => '您好,您的订单已发货,请注意查收。', ]); // 给群聊发图片 open($domain, $apiKey, [ 'type' => 'message', 'robotWxId' => 'wxid_xxxxxxxxxxxxxxxx', 'friendWxId' => '29xxxxxxxxxxxxxxxxx@chatroom', 'content' => 'https://cdn.example.com/promo.jpg', 'contentType' => 2, ]); // 给好友发链接卡片 open($domain, $apiKey, [ 'type' => 'message', 'robotWxId' => 'wxid_xxxxxxxxxxxxxxxx', 'friendWxId' => 'wxid_yyyyyyyyyyyyyyyy', 'contentType' => 6, 'card' => [ 'title' => '年终大促正式开启', 'desc' => '全场低至 3 折,超值满减等你来', 'url' => 'https://example.com/promo', 'thumb' => 'https://cdn.example.com/promo_cover.jpg', 'source' => '官方商城', ], ]);