| name | wecom-cs-automation |
| description | 企业微信客服自动化系统。自动同意好友添加、基于知识库的智能问答、未知问题人工介入提醒。适用于企业微信客服场景的 AI 助手机器人。 |
| allowed-tools | Bash, Read, Write, Edit, Exec, mcporter__* |
企业微信客服自动化系统
这是一个完整的企业微信客服 AI 助手解决方案,能够自动处理好友添加、智能问答、人工转接等场景。
核心功能
1. 自动同意好友添加
- 实时监听好友添加事件
- 自动通过好友请求
- 发送欢迎消息
- 标注用户来源和标签
2. 基于知识库的智能问答
- 向量知识库存储企业知识
- 语义搜索匹配问题
- LLM 生成专业回复
- 支持多轮对话上下文
3. 未知问题人工介入
- 置信度阈值判断
- 自动提醒人工客服
- 转接对话给人工
- 记录未解决问题用于优化
技术架构
┌─────────────┐
│ 企业微信 │
│ Webhook │
└──────┬──────┘
│
▼
┌─────────────────┐
│ 回调服务器 │
│ (Go/Python) │
└──────┬──────────┘
│
├──────────────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ 向量知识库 │ │ LLM API │
│ (PG+pgvector)│ │ (Kimi/GPT-4) │
└──────────────┘ └──────────────┘
│
▼
┌──────────────┐
│ 人工提醒 │
│ (Telegram) │
└──────────────┘
快速开始
第一步:配置企业微信应用
-
创建企业微信应用
- 登录企业微信管理后台
- 应用管理 → 创建应用 → 选择"微信客服"
- 记录以下信息:
corp_id: 企业 ID
agent_id: 应用 AgentId
secret: 应用 Secret
-
配置回调地址
URL: https://your-domain.com/wecom/callback
Token: 自定义验证令牌
EncodingAESKey: 自动生成
-
订阅所需事件
- 联系人变更事件
- 消息事件
- 外部联系人免验证添加事件
第二步:设置知识库
sudo apt install postgresql-14
sudo -u postgres psql -c "CREATE EXTENSION vector;"
sudo -u postgres createdb wecom_kb
psql wecom_kb < ~/clawd/skills/wecom-cs-automation/schema.sql
第三步:导入知识库数据
python3 ~/clawd/skills/wecom-cs-automation/scripts/import_kb.py \
--input knowledge.md \
--token $(pass show api/kimi)
psql wecom_kb -c "SELECT COUNT(*) FROM knowledge_chunks;"
第四步:启动回调服务
cat > .env << EOF
WECOM_CORP_ID=$(pass show api/wecom-corp-id)
WECOM_AGENT_SECRET=$(pass show api/wecom-agent-secret)
WECOM_TOKEN=your_webhook_token
WECOM_AES_KEY=your_aes_key
KB_DB_URL=postgresql://localhost/wecom_kb
LLM_API_KEY=$(pass show api/kimi)
LLM_API_BASE=https://api.moonshot.cn/v1
NOTIFICATION_CHANNEL=telegram:8518085684
EOF
uvicorn wecom_callback_server:app --host 0.0.0.0 --port 8000
go run cmd/server/main.go
第五步:验证服务
curl http://localhost:8000/health
curl -X POST http://localhost:8000/api/test_kb \
-H "Content-Type: application/json" \
-d '{"query": "如何退款?"}'
使用方法
场景 1:自动欢迎新好友
当用户添加客服为好友时:
async def handle_friend_add(user_id, user_name):
await wecom.accept_friend(user_id)
await wecom.add_external_tag(user_id, tags=["新客户"])
welcome_msg = f"""👋 欢迎来到{name}!
我是智能客服小助手,可以帮您:
• 查询订单状态
• 解答常见问题
• 处理售后问题
如有复杂问题,我会转接人工客服为您服务。"""
await wecom.send_text(user_id, welcome_msg)
场景 2:知识库问答
async def handle_question(user_id, question):
chunks = await search_knowledge(question, top_k=3)
context = "\n\n".join([c.content for c in chunks])
prompt = f"""基于以下知识库内容回答用户问题:
知识库:
{context}
用户问题:{question}
如果知识库中没有相关信息,请回复"抱歉,这个问题我暂时无法回答,已为您转接人工客服。\""""
answer = await call_llm(prompt)
if "暂时无法回答" in answer or chunks[0].similarity < 0.7:
await escalate_to_human(user_id, question)
else:
await wecom.send_text(user_id, answer)
场景 3:人工介入提醒
async def escalate_to_human(user_id, question):
await wecom.send_text(user_id, "⏳ 已为您转接人工客服,请稍候...")
user_info = await wecom.get_user_info(user_id)
notification = f"""🚨 需要人工介入
用户:{user_info.name} ({user_info.id})
问题:{question}
时间:{datetime.now().strftime('%Y-%m-%d %H:%M')}
请及时处理。"""
await send_telegram_message(notification)
await save_unknown_question(user_id, question)
目录结构
~/clawd/skills/wecom-cs-automation/
├── SKILL.md # 本文件
├── schema.sql # 数据库表结构
├── config/
│ ├── kb_config.yaml # 知识库配置
│ └── escalation_rules.yaml # 转人工规则
├── scripts/
│ ├── import_kb.py # 导入知识库
│ ├── search_kb.py # 测试知识库搜索
│ └── init_db.py # 初始化数据库
├── workflows/
│ ├── on_friend_add.py # 好友添加处理
│ ├── answer_question.py # 问答处理
│ └── escalate.py # 人工转接
├── server/
│ ├── main.py # FastAPI 主服务
│ ├── wecom_client.py # 企业微信 API 客户端
│ ├── kb_searcher.py # 知识库搜索
│ └── notification.py # 通知服务
└── knowledge/
└── sample.md # 示例知识文档
API 配置
所需密钥
pass insert api/wecom-corp-id
pass insert api/wecom-agent-secret
pass insert api/kimi
pass insert api/telegram-bot
配置文件
创建 ~/clawd/skills/wecom-cs-automation/.env:
# 企业微信配置
WECOM_CORP_ID=${WECOM_CORP_ID}
WECOM_AGENT_ID=1000002
WECOM_AGENT_SECRET=${WECOM_AGENT_SECRET}
WECOM_TOKEN=random_token_here
WECOM_ENCODING_AES_KEY=base64_key_here
# 数据库
KB_DB_URL=postgresql://postgres@localhost/wecom_kb
# LLM
LLM_PROVIDER=kimi
LLM_API_KEY=${LLM_API_KEY}
LLM_API_BASE=https://api.moonshot.cn/v1
LLM_MODEL=moonshot-v1-8k
# 知识库搜索
KB_SIMILARITY_THRESHOLD=0.7
KB_TOP_K=3
# 人工介入
NOTIFICATION_ENABLED=true
NOTIFICATION_CHANNEL=telegram:8518085684
工作流程详解
完整消息处理流程
graph TD
A[接收消息] --> B{是否为文本?}
B -->|是| C[搜索知识库]
B -->|否| D[其他类型处理]
C --> E{相似度 > 阈值?}
E -->|是| F[生成回答]
E -->|否| G[转人工]
F --> H[发送回复]
G --> I[通知人工客服]
D --> J[按类型处理]
数据流
@app.post("/wecom/callback")
async def wecom_callback(payload: WebhookPayload):
event = payload.Event[0]
if event.Event == "add_external_contact":
await handle_friend_add(event.UserId)
elif event.Event == "msg":
await handle_message(event)
return {"errcode": 0}
async def handle_message(event):
user_id = event.FromUserName
content = event.Content
results = search_kb(content)
if results[0].score > CONFIDENCE_THRESHOLD:
answer = generate_answer(results, content)
send_message(user_id, answer)
else:
escalate_to_human(user_id, content)
知识库管理
添加知识
python3 scripts/import_kb.py \
--input ~/clawd/knowledge/faq.md \
--category "常见问题"
psql wecom_kb
INSERT INTO knowledge_chunks (content, metadata)
VALUES (
'退货政策:7天无理由退货',
'{"category": "售后", "tags": ["退货", "政策"]}'
);
更新知识
python3 scripts/import_kb.py --input faq.md --refresh
测试搜索
python3 scripts/search_kb.py "如何退款?"
监控与维护
日志查看
tail -f /var/log/wecom-cs/server.log
tail -f /var/log/postgresql/postgresql-14-main.log
性能监控
from prometheus_client import Counter, Histogram
message_counter = Counter('messages_total', 'Total messages')
answer_latency = Histogram('answer_latency_seconds', 'Answer latency')
@answer_latency.time()
def handle_message():
message_counter.inc()
人工介入统计
SELECT
COUNT(*) as count,
SUBSTRING(content, 1, 30) as question_preview
FROM unknown_questions
GROUP BY question_preview
ORDER BY count DESC
LIMIT 10;
安全最佳实践
-
密钥管理
- 所有密钥使用
pass 存储
- 环境变量引用,不硬编码
-
数据隐私
-
访问控制
-
审计日志
故障排查
问题 1:回调接收不到消息
ss -ltnp | grep 8000
nginx -t
sudo ufw status
问题 2:知识库搜索无结果
psql wecom_kb -c "SELECT COUNT(*) FROM knowledge_chunks;"
python3 scripts/search_kb.py "测试查询"
python3 scripts/import_kb.py --rebuild
问题 3:人工提醒未发送
curl -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" \
-d "chat_id=8518085684&text=测试"
cat .env | grep NOTIFICATION
扩展功能
1. 多轮对话记忆
async def get_conversation_history(user_id):
return redis.get(f"conv:{user_id}")
async def append_message(user_id, role, content):
redis.rpush(f"conv:{user_id}", f"{role}:{content}")
2. 情感分析
async def analyze_sentiment(text):
result = openai.ChatCompletion.create(
model="gpt-4",
messages=[{
"role": "system",
"content": "判断用户情绪(正面/负面/中性),只返回一个词。"
}, {
"role": "user",
"content": text
}]
)
return result.choices[0].message.content
3. 主动营销
async def daily_promotion():
users = get_active_users(days=7)
for user_id in users:
await wecom.send_text(user_id, "今日特惠:...")
相关技能
- feishu-automation: 飞书平台自动化
- notion-automation: Notion 知识库集成
- telegram-automation: Telegram 通知集成
参考资源