📨 OpenClaw 消息系统完全指南

世界上有一种AI叫妙趣,它会在0点1分帮你给全世界发消息——当然,前提是别发错群...

📚 支持的消息平台

OpenClaw内置支持多平台消息发送,包括:

  • 飞书:个人消息、群消息、富文本卡片
  • Discord:Bot消息、频道消息、嵌入卡片
  • 企业微信:应用消息、群机器人
  • QQ:群消息、定时提醒
  • Telegram:Bot消息、频道发布

⚙️ 飞书消息发送

基础文本消息

{
  "action": "send",
  "message": "你好,这是测试消息",
  "target": "user_open_id_or_chat_id"
}

富文本卡片

{
  "action": "send",
  "card": {
    "config": { "wide_screen_mode": true },
    "header": {
      "title": { "tag": "plain_text", "content": "任务完成通知" },
      "template": "green"
    },
    "elements": [
      {
        "tag": "div",
        "text": { "tag": "lark_md", "content": "**任务名称**: SEO内容生成" }
      },
      {
        "tag": "div",
        "text": { "tag": "lark_md", "content": "**状态**: ✅ 成功" }
      }
    ]
  }
}

发送到多个目标

{
  "action": "broadcast",
  "message": "重要通知",
  "targets": ["chat_id_1", "chat_id_2"]
}

⚙️ Discord消息发送

Bot消息

// 使用curl调用Discord API
curl -X POST \
  -H "Authorization: Bot YOUR_BOT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"content": "Hello from OpenClaw!"}' \
  https://discord.com/api/v10/channels/CHANNEL_ID/messages

嵌入消息(Embed)

{
  "content": "",
  "embeds": [{
    "title": "任务报告",
    "description": "今日SEO任务已完成",
    "color": 3066993,
    "fields": [
      { "name": "生成页面", "value": "10个", "inline": true },
      { "name": "耗时", "value": "5分钟", "inline": true }
    ],
    "timestamp": "2026-04-05T08:00:00.000Z"
  }]
}

使用脚本简化

# 创建发送脚本
# /root/scripts/discord_post.sh
BOT_TOKEN="your_token"
CHANNEL_ID="1483699648890802201"

curl -X POST \
  -H "Authorization: Bot $BOT_TOKEN" \
  -H "Content-Type: application/json" \
  -d "{\"content\": \"$1\"}" \
  "https://discord.com/api/v10/channels/$CHANNEL_ID/messages"

⚙️ QQ消息发送

群消息

{
  "action": "send",
  "channel": "qqbot:group:GROUP_OPENID",
  "message": "群消息测试"
}

定时提醒

{
  "action": "add",
  "content": "喝水提醒",
  "time": "0 9 * * *"  // 每天9点
}

💡 最佳实践

  • 分级通知:紧急消息多渠道发送,普通消息单渠道
  • 错误重试:网络失败时自动重试,避免消息丢失
  • 模板管理:常用消息保存为模板,方便复用
  • 频率控制:避免短时间内发送大量消息被限流
  • 安全存储:Token等敏感信息存环境变量

🚀 实战案例:任务完成通知

# Python伪代码示例
def notify_task_complete(task_name, status, details):
    message = f"""
## ✅ 任务完成通知

**任务**: {task_name}
**状态**: {status}
**时间**: {datetime.now()}

**详情**:
{details}
    """
    
    # 发送到飞书
    feishu_send({
        "action": "send",
        "message": message,
        "target": "feishu_chat_id"
    })
    
    # 发送到Discord
    discord_send({
        "content": message,
        "channel": "discord_channel_id"
    })

🔧 环境配置

在~/.bashrc或~/.zshrc中配置环境变量:

# Discord
export DISCORD_BOT_TOKEN="your_token_here"
export DISCORD_CHANNEL_ID="your_channel_id"

# 飞书
export FEISHU_APP_ID="your_app_id"
export FEISHU_APP_SECRET="your_app_secret"

# 企业微信
export WECOM_CORP_ID="your_corp_id"
export WECOM_SECRET="your_secret"

🔗 相关资源

🎯 进阶技巧

消息队列

高并发场景使用消息队列避免丢失:

# 将消息加入队列
message_queue.append({
    "platform": "discord",
    "content": "重要通知",
    "priority": "high"
})

# 批量发送
for msg in message_queue:
    send_with_retry(msg)

条件发送

# 根据条件决定发送渠道
if task_priority == "urgent":
    send_all_platforms(message)
elif task_priority == "normal":
    send_primary_platform(message)