世界上有一种孤独叫"消息发不出去",它让AI像个哑巴。 但有了message工具,你的Agent可以同时在Discord吹水、在飞书汇报、在Slack协作——真正成为连接各个世界的桥梁。
📋 message工具概览
# 消息工具支持的操作
message(action="send") # 发送消息
message(action="broadcast") # 广播到多个目标
message(action="read") # 读取消息历史
message(action="edit") # 编辑已发送消息
message(action="thread-reply") # 线程内回复
message(action="pin") # 置顶消息
message(action="react") # 添加表情反应
message(action="channel-list") # 列出频道
message(action="member-info") # 获取成员信息
核心参数
| 参数 | 说明 | 示例 |
|---|---|---|
| target | 目标频道/用户 | discord:general |
| message | 消息内容 | "Hello World" |
| channel | 频道ID/名称 | ai-news |
| silent | 静默发送(不通知) | true |
| replyTo | 回复指定消息 | message_id |
| threadId | 发送到线程 | thread_123 |
🎮 Discord集成
配置Bot Token
# 环境变量配置
export DISCORD_BOT_TOKEN="your_bot_token_here"
# 或通过OpenClaw Gateway配置
# ~/.config/openclaw/config.yaml
channels:
discord:
default: "1483699648890802201" # 默认频道ID
token: ${DISCORD_BOT_TOKEN}
发送消息
# 基础发送
message(
action="send",
target="discord",
channel="ai-news",
message="📰 今日AI新闻已更新!"
)
# 带格式的消息
message(
action="send",
target="discord",
message="""## 🚀 新功能发布
**OpenClaw v2.0** 现已支持:
- ✅ MCP协议集成
- ✅ 多智能体协作
- ✅ 增强型Memory系统
[查看详情](https://miaoquai.com)"""
)
# 回复特定消息
message(
action="send",
target="discord",
message="感谢反馈!已修复该问题。",
replyTo="1234567890"
)
# 创建线程并发送
message(
action="thread-reply",
target="discord",
message="这是线程内的第一条消息",
threadId="parent_message_id",
threadName="讨论:新功能设计"
)
Cron任务自动推送
{
"name": "discord-daily-share",
"schedule": { "kind": "cron", "expr": "0 10,16,22 * * *", "tz": "Asia/Shanghai" },
"payload": {
"kind": "agentTurn",
"message": "从网站内容中选择一条有趣的内容,生成适合Discord的分享文案,发送到#daily-share频道"
},
"delivery": {
"mode": "announce",
"channel": "discord:daily-share"
}
}
📱 飞书集成
配置飞书Bot
# ~/.config/openclaw/config.yaml
channels:
feishu:
app_id: "cli_xxxxxxxxxx"
app_secret: ${FEISHU_APP_SECRET}
default_chat: "oc_c942dfd09730eb94bf838c6519c115e9"
飞书专属操作
# 发送文本消息
message(
action="send",
target="feishu",
chatId="oc_xxxxxxxx",
contentType="text",
message="飞书消息测试"
)
# 发送富文本(卡片消息)
message(
action="send",
target="feishu",
card={
"header": {
"title": {"tag": "plain_text", "content": "任务完成通知"},
"template": "green"
},
"elements": [
{
"tag": "div",
"text": {"tag": "lark_md", "content": "✅ **SEO优化任务** 已完成\n- 生成页面:8个\n- 总字数:42KB\n- 耗时:5分钟"}
}
]
}
)
# 创建群聊日程
message(
action="event-create",
target="feishu",
eventName="周会",
startTime="2026-04-16T10:00:00+08:00",
durationMin=60
)
💼 Slack集成
# 发送Slack消息
message(
action="send",
target="slack",
channel="#general",
message="Hello from OpenClaw!",
blocks=[
{
"type": "section",
"text": {"type": "mrkdwn", "text": "*新报告已生成*"}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {"type": "plain_text", "text": "查看报告"},
"url": "https://miaoquai.com/report.html"
}
]
}
]
)
🎯 实战:智能客服Bot
# 统一入口的多平台客服Bot
class CustomerServiceBot:
def __init__(self):
self.channels = {
"discord": "discord:support",
"feishu": "feishu:service",
"slack": "slack:#help"
}
def route_message(self, message, source_channel):
"""智能路由消息到合适的Agent处理"""
# 1. 分类问题类型
category = self.classify(message)
# 2. 搜索知识库
kb_answer = self.search_kb(message)
# 3. 生成回复
if kb_answer.confidence > 0.8:
response = kb_answer.content
else:
response = self.escalate_to_human(message)
# 4. 回复到原频道
message(
action="send",
target=source_channel,
message=response,
threadId=message.thread_id # 保持线程上下文
)
def broadcast_announcement(self, content):
"""全平台广播公告"""
for platform, channel in self.channels.items():
try:
message(
action="send",
target=channel,
message=f"📢 系统公告\n\n{content}",
pin=True # 置顶
)
except Exception as e:
log_error(f"发送到{platform}失败: {e}")
# 使用
bot = CustomerServiceBot()
bot.broadcast_announcement("系统维护通知:今晚2-4点进行升级")
📊 消息模板化
# 预定义消息模板
TEMPLATES = {
"task_complete": """
✅ **任务完成**
| 项目 | 内容 |
|------|------|
| 任务 | {task_name} |
| 状态 | 已完成 |
| 耗时 | {duration} |
| 输出 | {output_link} |
{details}
""",
"error_alert": """
🚨 **错误告警**
任务 `{task_name}` 执行失败
```
{error_message}
```
请检查日志或联系管理员
""",
"daily_digest": """
📊 **{date} 日报**
📝 内容生成:{articles} 篇
🔗 SEO页面:{pages} 个
💬 社区互动:{interactions} 次
📈 流量增长:{traffic_growth}
[查看完整报告]({report_url})
"""
}
def send_from_template(template_name, **kwargs):
template = TEMPLATES[template_name]
content = template.format(**kwargs)
message(action="send", target="discord", message=content)
⚠️ 最佳实践与坑点
- 速率限制:Discord 5秒/5条,飞书 20条/秒,超限制会429
- 消息长度:Discord 2000字符,超长需分割或发文件
- 失败重试:实现指数退避,避免死循环
- 敏感信息:Token通过环境变量注入,不硬编码
- 线程管理:保持上下文,回复时指定threadId