自动回复场景
使用 juhe-cli 实现消息自动回复功能
场景描述
监听微信消息,根据关键词或规则自动回复预设内容,适用于客服机器人、 常见问题解答、无人值守回复等场景。
实现步骤
- 部署 juhe-sync 并设置回调 URL
- 通过 sync API 获取新消息
- 解析消息内容和发送者
- 使用 msg send_text 发送回复
涉及命令
sync msg- 同步获取新消息msg send_text- 发送文本消息contact search- 搜索联系人
代码示例
以下示例展示如何轮询消息并自动回复:
bash
#!/bin/bash
# 自动回复脚本
while true; do
# 获取新消息
messages=$(juhe-cli sync msg '{}')
# 解析消息内容(需要使用 jq 或其他 JSON 工具)
echo "$messages" | jq -r '.[] | select(.type == 1) | {
from: .from_user,
content: .content
}' | while read -r msg; do
from=$(echo "$msg" | jq -r '.from')
content=$(echo "$msg" | jq -r '.content')
# 根据关键词自动回复
case "$content" in
"价格"|"多少钱"|"收费")
juhe-cli msg send_text "{\"to_wxid\": \"$from\", \"content\": \"我们的服务套餐:\n1. 基础版:¥99/月\n2. 专业版:¥299/月\n3. 企业版:¥999/月\"}"
;;
"客服"|"人工"|"转人工")
juhe-cli msg send_text "{\"to_wxid\": \"$from\", \"content\": \"正在为您转接人工客服,请稍候...\"}"
;;
"你好"|"hello"|"hi")
juhe-cli msg send_text "{\"to_wxid\": \"$from\", \"content\": \"您好!有什么可以帮您的吗?\"}"
;;
*)
# 默认回复
juhe-cli msg send_text "{\"to_wxid\": \"$from\", \"content\": \"收到您的消息:$content\n我们会尽快回复!\"}"
;;
esac
done
# 间隔 5 秒
sleep 5
donePython 示例
python
import json
import time
import subprocess
def run_command(cmd):
"""执行 CLI 命令"""
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
return json.loads(result.stdout)
def auto_reply():
"""自动回复主循环"""
while True:
# 获取新消息
messages = run_command('juhe-cli sync msg '{}'')
for msg in messages:
if msg.get('type') == 1: # 文本消息
from_user = msg.get('from_user')
content = msg.get('content', '')
# 关键词匹配
if any(keyword in content for keyword in ['价格', '多少钱', '收费']):
reply = '我们的服务套餐:\n1. 基础版:¥99/月\n2. 专业版:¥299/月'
elif any(keyword in content for keyword in ['客服', '人工']):
reply = '正在为您转接人工客服,请稍候...'
elif any(keyword in content for keyword in ['你好', 'hello']):
reply = '您好!有什么可以帮您的吗?'
else:
reply = f'收到您的消息:{content}\n我们会尽快回复!'
# 发送回复
reply_cmd = f'juhe-cli msg send_text '{{"to_wxid": "{from_user}", "content": "{reply}"}}''
run_command(reply_cmd)
time.sleep(5)
if __name__ == '__main__':
auto_reply()进阶:AI 智能回复
结合 SKILL 将 juhe-cli 接入 AI Agent(如 Claude Code 或 OpenClaw), 由 AI 根据消息内容生成智能回复,而不仅是关键词匹配。
bash
# AI 自动回复示例
messages=$(juhe-cli sync msg '{}')
for msg in messages; do
from=$(echo "$msg" | jq -r '.from_user')
content=$(echo "$msg" | jq -r '.content')
# 调用 AI 生成回复(假设有 AI API)
reply=$(curl -X POST https://api.ai.com/generate \
-d "message=$content" \
-d "context=客服助手")
# 发送 AI 生成的回复
juhe-cli msg send_text "{\"to_wxid\": \"$from\", \"content\": \"$reply\"}"
done部署建议
- 使用 systemd 或 supervisord 保持脚本持续运行
- 配置日志记录便于排查问题
- 设置消息去重避免重复回复
- 添加错误处理和重试机制