/msg/sendText 的 content 是纯字符串。把图片 URL 或本地路径写进去,对端收到的仍是文本,不会出现缩略图,也不能点开下载文件。
附件要拆成两条链路:
- 上传素材,拿到文件 ID / 图片 ID
- 调用对应消息类型的发送接口,
toId填userId或roomId
消息类型不要混用
| 接口职责 | 可发送 | 不能当它用 |
|---|---|---|
| 发文本 | 通知、单号、话术 | 图片二进制、文件二进制 |
| 发图片 | jpg / png 等图片 | pdf、xlsx(应走文件) |
| 发文件 | pdf、xlsx、docx、zip | 想当长图预览的截图 |
私聊和客户群只是 toId 不同,消息类型选择规则一样。
两段式调用
统一入口:
http
POST /api/qw/doApi
Content-Type: application/json
X-QIWEI-TOKEN: YOUR_TOKEN
第一步:确认文本通道可用
python
import requests
API = "http://manager.qiweapi.com/qiwe/api/qw/doApi"
HEADERS = {
"X-QIWEI-TOKEN": "YOUR_TOKEN",
"Content-Type": "application/json",
}
def send_text(guid, to_id, content):
r = requests.post(API, headers=HEADERS, json={
"method": "/msg/sendText",
"params": {
"guid": guid,
"toId": to_id,
"content": content,
"isNoNeedRead": True,
}
}, timeout=30)
body = r.json()
assert body.get("code") == 0, body
return body
文本都失败,说明 Token、guid 或登录态有问题,不要继续传附件。
第二步:换方法名发图片 / 文件
python
def send_image(guid, to_id, image_id):
return requests.post(API, headers=HEADERS, json={
"method": "/msg/sendImage", # 以实际消息模块方法名为准
"params": {
"guid": guid,
"toId": to_id,
"imageId": image_id,
}
}, timeout=60).json()
def send_file(guid, to_id, file_id):
return requests.post(API, headers=HEADERS, json={
"method": "/msg/sendFile", # 以实际消息模块方法名为准
"params": {
"guid": guid,
"toId": to_id,
"fileId": file_id,
}
}, timeout=60).json()
imageId / fileId 必须来自上传接口的返回值,不要手写本地路径。上传接口的字段名以素材模块为准,顺序固定为:本地文件 → 上传 → 发送。
能用 QiWe API 时,三种发送都走同一个 doApi,只改 method 和 params,登录态不用变。
联调顺序
- 发给自己的
userId一张小 jpg(< 200KB) - 发给测试群
roomId同一张图 - 再发一个小 xlsx / pdf
- 最后把「查询到附件 → 上传 → 发送」接到业务代码
每一步都判断:
python
if result.get("code") != 0 or not result.get("data", {}).get("isSendSuccess"):
raise RuntimeError(result)
失败原因对照
| 现象 | 原因 |
|---|---|
| 对端只看到一串 http 链接 | 误用了 /msg/sendText |
| 提示素材不存在 | 未上传,或上传返回的 ID 没传对 |
| 图片发出去打不开 | 扩展名和真实格式不一致 |
| 文件发送超时 | 体积过大,先压测上限再发正式文件 |
| 群里没收到、私聊收到了 | toId 填成了 userId |
不要在一次请求里混传图片和文件。图片、视频、文件通常是不同 method。
总结
发附件不是给文本接口加一个 URL 字段。
先保证 /msg/sendText 通,再上传素材,最后用发图片 / 发文件的 method 把 ID 发给 userId 或 roomId。私聊和客户群验收都通过,这条链路才算接完。