send-to-wechat
> 把本地文件发送到用户微信(通过 OpenClaw openclaw-weixin 通道)。
触发场景
- 用户说「发微信」「发到我微信」「微信发给我」「send to wechat」
- 用户想把本地任意文件(PDF / docx / 图片 / 压缩包等)推送到自己的微信
前置条件
- OpenClaw 已配置
openclaw-weixin通道(用户已绑定微信) - 配置文件位置:
C:\Users\<user>\.qclaw\openclaw-weixin\accounts\<bot-id>-im-bot.json - 文件大小建议 < 25 MB(微信限制)
核心动作
只有一步:调用 message 工具发送文件
message(
action="send",
channel="openclaw-weixin",
target=<从 accounts 配置读取的 userId>,
media=<文件绝对路径,用正斜杠 / >,
filename=<文件名>,
message=<可选的文字说明>
)
步骤 1:读取微信 target(userId)
从 ~/.qclaw/openclaw-weixin/accounts/ 读取最新的 bot 配置:
import os, glob, json
def get_wechat_target():
"""从 openclaw-weixin accounts 目录读取最新的 userId"""
accounts_dir = os.path.expanduser(r'~\.qclaw\openclaw-weixin\accounts')
if not os.path.isdir(accounts_dir):
return None
# 找 *-im-bot.json 文件(不是 .sync.json 或 .context-tokens.json)
files = sorted(
glob.glob(os.path.join(accounts_dir, '*-im-bot.json')),
key=os.path.getmtime,
reverse=True
)
for f in files:
with open(f, 'r', encoding='utf-8') as fp:
data = json.load(fp)
uid = data.get('userId')
if uid:
return uid
return None
返回值形如:o9cq80_gPztSv6T0j2O0huaLEEKI@im.wechat
步骤 2:调用 message 工具发送
message(action=send,
channel="openclaw-weixin",
target=<step1 的 userId>,
media="C:/path/to/file.pdf", # 注意:用正斜杠 /
filename="file.pdf",
message="可选的文字说明"
)
步骤 3:判断结果
- 成功:返回值包含
result.messageId,格式形如openclaw-weixin:<timestamp>-<hash> - 失败:返回值
result.ok为 false,或返回 error
成功后告诉用户:「✅ 已发送,请查收微信」。
⚠️ 关键约束
通道选择
- 必须用
openclaw-weixin(这是绑定用户微信的真正通道) - 不要用
wechat-access(那是另一个未启用的旧配置,发了用户也收不到)
target 格式
- target 是
userId(格式o9cq80_xxxx@im.wechat),不是数字 ID - 数字 ID(如
1832932455)是wechat-access通道用的,不要混用
media 路径
- 用正斜杠
/,不要用反斜杠\ - 用绝对路径,不要用相对路径
文件大小
- 建议单文件 < 25 MB
- > 25 MB 可能发送失败,需拆分或上传云盘
完整调用示例(Python 准备 target,message 工具发送)
import os, glob, json
# 1. 读 target
accounts_dir = os.path.expanduser(r'~\.qclaw\openclaw-weixin\accounts')
files = sorted(glob.glob(os.path.join(accounts_dir, '*-im-bot.json')), key=os.path.getmtime, reverse=True)
target = None
for f in files:
with open(f, 'r', encoding='utf-8') as fp:
data = json.load(fp)
if data.get('userId'):
target = data['userId']
break
print(f'target: {target}')
print(f'file: C:/Users/26612/Desktop/plc/PLC零基础速成教学.pdf')
然后调用 message 工具:
message(action=send,
channel="openclaw-weixin",
target=target,
media="C:/Users/26612/Desktop/plc/PLC零基础速成教学.pdf",
filename="PLC零基础速成教学.pdf",
message="教学文档 PDF"
)
常见问题
Q1: message 返回 ok 但用户没收到
- 检查
channel是不是openclaw-weixin(不是wechat-access) - 检查
target是不是从accounts/*-im-bot.json里读的userId(不是数字 ID)
Q2: 找不到 accounts 目录
- 路径:
C:\Users\<user>\.qclaw\openclaw-weixin\accounts\ - 如果目录不存在,说明用户还没绑定微信,需要先在 QClaw 客户端绑定
Q3: 文件太大发不出
- 微信限制约 25 MB
- 解决方案:压缩、拆分、或上传云盘发链接
Q4: 多个 bot 配置选哪个
- 按
mtime排序选最新的(sorted(..., key=os.path.getmtime, reverse=True)[0])
适用文件类型
PDF / docx / xlsx / pptx / png / jpg / mp3 / mp4 / zip 等常见格式都支持。
限制
- 需要用户已绑定微信(
openclaw-weixin通道已配置) - 单文件 < 25 MB
- 不能主动发消息给未绑定用户
- 不能发到群聊(只能发到绑定的微信账号)