Skip to content

调用示例 ​

以下示例共享已经验证的本机端口和 token。发送目标为自己的文件传输助手。

PowerShell 发送第一条文本 ​

powershell
$body = @{
  target = @{ type = 'contact'; id = 'filehelper' }
  type = 'text'
  text = '云客 API 接入测试'
} | ConvertTo-Json -Depth 4
Invoke-RestMethod "$api/v2/messages/validate" -Method Post -Headers $headers -ContentType 'application/json; charset=utf-8' -Body $body
Invoke-RestMethod "$api/v2/messages" -Method Post -Headers $headers -ContentType 'application/json; charset=utf-8' -Body $body

上面的 $api 和 $headers 来自实例配置。获取实例配置

Python 读取本人资料 ​

python
import json
import os
from urllib.request import Request, urlopen

port = int(os.environ["YUNKE_PORT"])
headers = {"Authorization": "Bearer " + os.environ["YUNKE_TOKEN"]}
request = Request(f"http://127.0.0.1:{port}/v2/account/self", headers=headers)
with urlopen(request, timeout=5) as response:
    data = json.load(response)["data"]
print(data["login"]["state"])

Node.js 读取一页事件 ​

js
const base = new URL(`http://127.0.0.1:${Number(process.env.YUNKE_PORT)}`)
const url = new URL('/v2/events?limit=50&wait_ms=1000', base)
const response = await fetch(url, {
  headers: { Authorization: `Bearer ${process.env.YUNKE_TOKEN}` },
  signal: AbortSignal.timeout(5000),
})
const payload = await response.json()
if (!response.ok || !payload.ok) throw new Error(payload.error?.code ?? 'request_failed')
const { session, next_cursor, items } = payload.data
console.log({ session, next_cursor, count: items.length })

易语言调用要点 ​

使用 HTTP 客户端模块向本机端口发送 GET 或 POST。构造 Bearer 请求头。发送前把 JSON 转成 UTF-8 字节集,响应按 UTF-8 解码。通过 JSON 解析模块取 ok、data 或 error.code。消息标识和事件游标保留为文本型,避免转换成数值造成精度损失。

从事件发送引用回复 ​

先保存事件对应的 owner、conversation.id 和 message.id。使用同一微信实例的端口,把 conversation.id 填入 target.id,把 message.id 填入 reference.message_id。引用回复