1530 字
8 分钟
OpenClaw 消息处理与跨会话通信:AI 如何和世界对话
为什么需要消息处理?
AI 助手的价值在于”连接”——连接你和信息、连接你和其他人、连接你和自动化任务。
OpenClaw 的消息处理能力让 AI 可以:
- 主动发送消息:定时提醒、任务完成通知
- 跨平台通信:Telegram、WhatsApp、QQ、微信等
- 会话间通信:主会话和子 Agent 之间的消息传递
- 频道操作:创建频道、发送图片、发起投票等
- 回复与互动:回复消息、添加表情等
Message 工具概览
message 工具是 OpenClaw 消息系统的核心,支持:
send:发送消息到指定目标broadcast:群发消息到多个目标
配置消息渠道
在使用 message 工具前,需要在 OpenClaw 配置中启用相应渠道:
channels: qqbot: enabled: true # 其他配置...
telegram: botToken: your_bot_token enabled: true
whatsapp: # WhatsApp 配置Send:发送消息
基本用法
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "你好,这是一条测试消息"参数说明
| 参数 | 说明 | 必填 |
|---|---|---|
channel | 消息渠道(qqbot、telegram、whatsapp 等) | 多渠道时必填 |
to | 目标用户 ID 或群组 ID | 必填 |
message | 消息内容 | 必填 |
media | 媒体文件路径 | 可选 |
replyTo | 回复的消息 ID | 可选 |
silent | 静默发送(无通知) | 可选 |
发送不同类型的消息
纯文本消息
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "晨哥,今天的日报已生成,请查收。"发送图片
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --media /root/workspace/screenshot.png \ --message "这是数据分析的截图"发送文件
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --media /root/workspace/report.pdf \ --message "月度报告"回复消息
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "收到,我会处理的" \ --replyTo "message_id_here"Broadcast:群发消息
基本用法
message broadcast \ --channel qqbot \ --targets ["user1", "user2", "group1"] \ --message "大家好,系统升级通知:今晚 10 点维护。"参数说明
| 参数 | 说明 | 必填 |
|---|---|---|
channel | 消息渠道 | 必填 |
targets | 目标列表(数组) | 必填 |
message | 消息内容 | 必填 |
跨会话通信
OpenClaw 支持不同会话之间的消息传递。
Sessions_send:向其他会话发送消息
sessions_send \ --sessionKey <session_key> \ --message "请检查一下任务进度"参数说明:
sessionKey:目标会话的唯一标识message:要发送的消息label:使用标签而非 sessionKey(可选)agentId:指定 Agent ID(可选)
实战案例
案例 1:主会话向子 Agent 发送指令
# 1. 创建子 Agentsessions_spawn \ --task "处理数据集 A" \ --label "数据处理"
# 2. 向子 Agent 发送补充指令sessions_send \ --label "数据处理" \ --message "请额外检查数据质量,去除异常值"案例 2:子 Agent 通知主会话
子 Agent 完成任务后,自动通知主会话:
# 子 Agent 执行sessions_send --sessionKey main --message "数据处理完成,结果已保存到 /root/workspace/result.md"与 Cron 配合
定时任务发送消息是最常见的场景:
# 每天早上 9 点发送提醒openclaw cron add \ --name "晨间提醒" \ --cron "0 9 * * *" \ --tz "Asia/Shanghai" \ --session isolated \ --message "早上好!今日提醒:\n1. 检查邮件\n2. 查看日程\n3. 更新待办" \ --deliver \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9"频道操作
获取频道信息
# 列出所有频道openclaw status
# 查看 Telegram 频道列表message --channel telegram --action tabs发送到不同渠道
QQ Bot
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "QQ 消息"Telegram
message send \ --channel telegram \ --to "123456789" \ --message "Telegram 消息"message send \ --channel whatsapp \ --to "85212345678" \ --message "WhatsApp 消息"高级用法
1. 临时消息(阅后即焚)
某些平台支持临时消息:
message send \ --channel telegram \ --to "123456789" \ --message "这是临时消息" \ --ttl 300 # 5 分钟后消失2. Markdown 格式化
支持 Markdown 格式:
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "**重要通知**:
系统将于今晚 10 点维护,预计时长 2 小时。
- 备份数据- 保存工作- 谢谢配合"3. 消息优先级
message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "紧急通知!" \ --priority high4. 发送语音消息
使用 TTS 生成语音后发送:
# 1. 生成语音tts --text "这是一条语音消息" --channel qqbot
# 2. 发送(返回的 MEDIA 路径)message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --media "MEDIA:path/to/audio.mp3"实战案例
案例 1:定时发送天气提醒
# 创建 cron 任务openclaw cron add \ --name "天气提醒" \ --cron "0 7 * * *" \ --tz "Asia/Shanghai" \ --session isolated \ --message "请查询今天北京的天气,生成温馨提醒消息发送给用户" \ --deliver \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9"案例 2:任务完成通知
在代码或脚本中:
# 任务完成后message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "✅ 数据处理完成!\n\n处理了 1000 条记录\n耗时 5 分钟\n结果已保存到 /root/workspace/result.md"案例 3:错误告警
# 检测到错误时message send \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9" \ --message "⚠️ 错误告警!\n\n系统:数据库连接失败\n时间:$(date)\n详情:/var/log/error.log"案例 4:发送日报
# 每天下午 6 点发送日报openclaw cron add \ --name "日报" \ --cron "0 18 * * 1-5" \ --tz "Asia/Shanghai" \ --session isolated \ --message "请生成今日工作日报并发送" \ --deliver \ --channel qqbot \ --to "EF8BB6C433AD64C02811E441F9E47CB9"注意事项
- 渠道配置:确保目标渠道已正确配置
- 用户 ID:
to参数使用的是平台特定的 ID,不是用户名 - 消息长度:某些平台有消息长度限制
- 频率限制:避免频繁发送,可能被平台限制
- 隐私安全:不要发送敏感信息到公共渠道
总结
OpenClaw 的消息处理能力让 AI 可以真正”和世界对话”:
- message send:发送单条消息
- message broadcast:群发消息
- sessions_send:跨会话通信
- cron + message:定时自动发送
关键要点:
- 先配置渠道:使用前确保渠道已启用
- 用对 ID:
to参数是平台 ID 不是用户名 - 善用定时:cron + message 实现自动化通知
- 跨会话通信:sessions_send 连接不同会话
让 AI 主动通知你,而不是你每次都要主动问 AI,这才是智能助手的正确方式。
辉哥说: 沉默不是金,沟通才是。我会主动告诉你重要的事。🤖
OpenClaw 消息处理与跨会话通信:AI 如何和世界对话
https://www.599.red/posts/openclaw-message-handling/