title: 公众号自动回复配置 - 智能回复设置指南 tags: [微信公众号, 自动化, 运营]


公众号自动回复配置 - 智能回复设置指南

详细介绍如何在 OpenCLAW 中配置微信公众号自动回复,实现智能客服和自动化交互。

自动回复类型概述

微信公众号支持多种自动回复类型:

类型 说明 配置位置
关键词回复 用户发送特定关键词时自动回复 关键词匹配
收到消息回复 用户发送任何消息时自动回复 默认回复
被关注回复 新用户关注时自动回复 欢迎消息
自定义菜单回复 点击菜单时触发 菜单配置

基础自动回复配置

1. 关键词回复

# config/wechat.yaml
wechat:
  message:
    keyword_reply:
      # 简单关键词匹配
      - keywords: ["hello", "hi", "你好"]
        response: "你好!很高兴见到你 👋"

      - keywords: ["帮助", "help"]
        response: |
          📋 功能说明

          1. 输入"天气" + 城市名,查询天气
          2. 输入"新闻",获取最新资讯
          3. 输入"客服",联系人工服务

      # 模糊匹配
      - keywords: ["天气"]
        response: "请输入您想查询的城市,例如:北京天气"
        match_type: fuzzy  # 包含即匹配

      # 精确匹配
      - keywords: ["人工客服"]
        response: "正在为您转接人工客服,请稍候..."
        match_type: exact

2. 默认回复(收到消息回复)

wechat:
  message:
    # 默认回复配置
    default_reply:
      enabled: true
      response: |
        感谢您的消息!

        我是智能助手,可以帮你:
        - 解答问题
        - 查询信息
        - 提供帮助

        输入"帮助"查看更多功能。

      # 仅对关键词未匹配的消息生效
      fallback: true

    # 所有消息都回复
    # reply_to_all: false  # 默认

3. 欢迎消息(新关注回复)

wechat:
  user:
    # 新关注自动回复
    welcome:
      enabled: true

      # 欢迎消息内容
      message: |
        🎉 欢迎关注!

        我是 AI 智能助手,可以帮助你:
        • 解答各类问题
        • 提供实用信息
        • 智能对话交互

        输入"帮助"查看全部功能!

      # 是否同步设置公众号后台
      sync_to_mp: true

    # 二次关注(取关后再关注)
    re_welcome:
      enabled: true
      message: |
        👋 欢迎回来!

        感谢您再次关注我。
        上次离开后,我们增加了很多新功能,
        输入"帮助"了解一下吧!

AI 智能回复配置

配置 Agent 驱动的智能回复

wechat:
  message:
    # 使用 AI Agent 处理消息
    handler: agent

    agent:
      # Agent 名称
      name: wechat-assistant

      # 模型配置
      model:
        provider: openai
        model: gpt-4
        temperature: 0.7
        max_tokens: 200

      # 系统提示词
      system_prompt: |
        你是一个微信公众号的智能助手。

        回复要求:
        - 简洁,不超过100字
        - 友好亲切
        - 有帮助

        知识截止:2024-01

      # 内存配置
      memory:
        type: file
        path: ./memory/wechat

      # 首次对话设置
      first_message:
        # 新用户首次发送消息时的回复
        response: "你好!有什么可以帮你的?"

混合模式:关键词 + AI

wechat:
  message:
    # 关键词优先,AI 作为后备
    handler: hybrid

    # 关键词规则
    keyword_reply:
      - keywords: ["人工", "客服"]
        response: "正在为您转接人工客服..."
        priority: 10

      - keywords: ["价格", "收费"]
        response: "我们的服务是免费的"
        priority: 5

    # AI 作为默认处理器
    agent:
      name: wechat-assistant

    # 关键词未匹配时的处理
    fallback:
      use_agent: true
      use_default: false

多轮对话配置

wechat:
  message:
    agent:
      name: wechat-assistant

      # 多轮对话配置
      conversation:
        # 启用多轮对话
        enabled: true

        # 对话超时时间(秒)
        timeout: 300

        # 最大轮数
        max_turns: 20

        # 会话管理
        session:
          # 按用户 ID 区分会话
          by_user: true

          # 会话过期时间
          expire_after: 3600

关键词高级配置

1. 正则表达式匹配

wechat:
  message:
    keyword_reply:
      # 天气查询:匹配 "北京天气"、"上海天气" 等
      - keywords:
          pattern: "(.+)(天气|天气预报)"
        response: "查询天气功能正在开发中,敬请期待!"
        match_type: regex

      # 数字匹配
      - keywords:
          pattern: "^\\d+$"
        response: "你输入了一个数字:${match}"
        match_type: regex

      # 手机号
      - keywords:
          pattern: "1[3-9]\\d{9}"
        response: "请不要在消息中泄露个人信息"
        match_type: regex

2. 关键词组

wechat:
  message:
    keyword_reply:
      # 同义词组
      - keywords_group:
          # 第一组:问候
          - ["你好", "您好", "hello", "hi", "hey"]
        response: "你好!有什么可以帮你的?"

      - keywords_group:
          # 第二组:再见
          - ["再见", "拜拜", "bye", "goodbye"]
        response: "再见!欢迎下次再来 👋"

      - keywords_group:
          # 第三组:感谢
          - ["谢谢", "感谢", "thanks", "thank you"]
        response: "不客气!很高兴能帮到你 😊"

3. 关键词优先级

wechat:
  message:
    keyword_reply:
      # 高优先级:精确匹配
      - keywords: ["人工客服"]
        response: "转接人工客服中..."
        priority: 100
        match_type: exact

      # 中优先级
      - keywords: ["客服"]
        response: "请输入"人工客服"转接人工"
        priority: 50

      # 低优先级:模糊匹配
      - keywords: ["客服"]
        response: "我可以帮你解答问题,请直接提问"
        priority: 10
        match_type: fuzzy

消息内容配置

1. 文本消息

- keywords: ["帮助"]
  response:
    type: text
    content: |
      📋 帮助信息

      功能列表:
      1. 天气查询
      2. 新闻资讯
      3. 百科问答

      如需人工帮助,请输入"人工客服"

2. 图文消息

- keywords: ["最新文章"]
  response:
    type: news
    articles:
      - title: "AI 技术发展趋势"
        description: "深入分析 AI 技术的最新发展"
        pic_url: "https://example.com/cover.jpg"
        url: "https://example.com/article/ai-trend"

      - title: "如何使用智能助手"
        description: "新手入门指南"
        pic_url: "https://example.com/cover2.jpg"
        url: "https://example.com/article/guide"

3. 图片消息

- keywords: ["二维码"]
  response:
    type: image
    media_id: "your_media_id"

4. 语言消息

- keywords: ["语音"]
  response:
    type: voice
    media_id: "your_voice_media_id"

事件处理配置

1. 菜单点击事件

wechat:
  events:
    # 菜单点击事件
    click:
      - key: "MENU_SERVICE"
        response: |
          🛎️ 服务中心

          1. 账户查询
          2. 问题反馈
          3. 联系人工

          请输入对应的数字

      - key: "MENU_ABOUT"
        response: |
          关于我们

          我们是一家专注于 AI 的科技公司...

2. 位置事件

wechat:
  events:
    location:
      enabled: true
      handler: agent

      # 或自定义处理
      response: |
        📍 您的位置已收到

        正在为您查找附近的服务点...

3. 扫描二维码事件

wechat:
  events:
    scan:
      # 关注后扫描二维码
      - event: "SCAN"
        scene_id: "promotion_123"
        response: |
          🎁 恭喜获得优惠券!

          您的优惠码:NEWUSER2024

      - event: "SCAN"
        scene_id: "login"
        response: "正在验证身份..."

智能路由配置

按用户类型回复

wechat:
  message:
    # 用户分路由
    routing:
      # 根据用户标签
      - condition: user.tag == "vip"
        handler: vip_agent

      - condition: user.tag == "new"
        handler: welcome_flow

      # 根据用户来源
      - condition: user.source == "qr_code"
        handler: promotion_response

      # 默认处理
      - condition: true
        handler: default_agent

按消息类型回复

wechat:
  message:
    routing:
      # 文本消息
      - condition: message.type == "text"
        handler: text_handler

      # 图片消息
      - condition: message.type == "image"
        handler: image_handler

      # 语音消息
      - condition: message.type == "voice"
        handler: voice_handler

      # 视频消息
      - condition: message.type == "video"
        handler: video_handler

高级功能

1. 消息限流

wechat:
  message:
    # 限流配置
    rate_limit:
      enabled: true

      # 每分钟最多回复同一用户
      per_user: 5

      # 全局限流
      global:
        per_minute: 100
        per_hour: 1000

      # 超出限制的响应
      exceeded:
        response: "消息太多,请稍后再试"

2. 消息缓存

wechat:
  message:
    # 消息缓存
    cache:
      enabled: true

      # 相同消息的默认回复缓存
      dedup:
        enabled: true
        window: 60  # 60秒内相同消息只回复一次

3. 黑名单管理

wechat:
  security:
    # 黑名单
    blacklist:
      enabled: true
      users:
        - "user_id_1"
        - "user_id_2"

      # 黑名单用户响应
      response: "抱歉,您暂时无法使用本服务"

统计与监控

消息统计

# 查看消息统计
openclaw wechat stats messages

# 输出示例
# 今日消息统计:
# - 收到消息:1,234
# - 成功回复:1,189
# - 关键词匹配:456
# - AI 处理:733
# - 失败:45

关键词分析

# 查看热门关键词
openclaw wechat stats keywords --period 7d

# 输出示例
# 热门关键词 TOP 10:
# 1. 帮助 - 234 次
# 2. 天气 - 189 次
# 3. 你好 - 156 次
# 4. 新闻 - 123 次
# ...

性能监控

# 实时监控
openclaw wechat monitor

# 设置告警
openclaw wechat alerts \
  --on-failure-rate-gt 5 \
  --on-response-time-gt 2000

最佳实践

1. 回复内容优化

# ❌ 过于简单
response: "好的"

# ✅ 有价值
response: "收到!我已经帮你记录下来,稍后会处理 😊"

2. 关键词覆盖

# 考虑用户各种表达方式
keywords:
  - ["天气", "气温", "温度", "多少度"]
  - ["新闻", "资讯", "最新消息", "热点"]

3. 兜底策略

# 关键词 + AI + 默认,三重保障
keyword_reply:
  - keywords: ["常见问题"]
    response: "请查看常见问题解答..."

fallback:
  use_agent: true

default_reply:
  response: "抱歉我没理解你的意思,输入'帮助'查看更多选项"

继续学习: - 公众号内容自动发布 - 实现自动化内容运营