文档
推荐给好友,福利领不停!好友同步开通最高 1000 万词元额度 · 后续消费分佣最高 30%。
+50万 Token生成链接

邮件自动化

配置邮件监听、自动回复与智能分类

本教程介绍如何将 Gmail 收件箱接入 Crab Claw,实现邮件自动监听、智能分类和自动回复。

架构

shell
Gmail Pub/Sub Push Crab Claw Webhook 智能体处理 回复/分类
Gmail Pub/Sub Push Crab Claw Webhook 智能体处理 回复/分类

前置条件

  • Crab Claw 已安装并运行
  • Google Cloud 项目已创建
  • gcloud CLI 已安装并登录
  • 公网可访问的 HTTPS 端点(推荐 Tailscale Funnel)

第一步:启用 Hooks

编辑 ~/.crabclaw/crabclaw.json

json
{
  "hooks": {
    "enabled": true,
    "token": "your-secret-token",
    "path": "/hooks",
    "presets": ["gmail"]
  }
}
{
  "hooks": {
    "enabled": true,
    "token": "your-secret-token",
    "path": "/hooks",
    "presets": ["gmail"]
  }
}

第二步:配置 Gmail Pub/Sub

  1. 在 Google Cloud Console 创建 Pub/Sub Topic
  2. 配置 Gmail Watch API 将邮件推送到该 Topic
  3. 设置 Pub/Sub Subscription 将消息推送到 Crab Claw Webhook URL

详细步骤请参考 Google Gmail Push Notifications 文档。

第三步:自定义处理规则

可以自定义邮件处理映射:

json
{
  "hooks": {
    "enabled": true,
    "token": "your-secret-token",
    "presets": ["gmail"],
    "mappings": [
      {
        "match": { "path": "gmail" },
        "action": "agent",
        "wakeMode": "now",
        "name": "Gmail",
        "sessionKey": "hook:gmail:{{messages[0].id}}",
        "messageTemplate": "新邮件来自 {{messages[0].from}}\n主题: {{messages[0].subject}}\n{{messages[0].snippet}}",
        "deliver": true,
        "channel": "last"
      }
    ]
  }
}
{
  "hooks": {
    "enabled": true,
    "token": "your-secret-token",
    "presets": ["gmail"],
    "mappings": [
      {
        "match": { "path": "gmail" },
        "action": "agent",
        "wakeMode": "now",
        "name": "Gmail",
        "sessionKey": "hook:gmail:{{messages[0].id}}",
        "messageTemplate": "新邮件来自 {{messages[0].from}}\n主题: {{messages[0].subject}}\n{{messages[0].snippet}}",
        "deliver": true,
        "channel": "last"
      }
    ]
  }
}

关键参数说明

参数说明
action处理方式: agent(智能体处理)、notify(仅通知)
wakeMode触发模式: now(立即)、queue(排队)
sessionKey会话标识模板,相同 key 的邮件共享上下文
messageTemplate传递给智能体的消息模板
deliver是否将结果投递到聊天频道
channel投递频道: last(上次使用的频道)或指定频道

第四步:设置模型

为邮件处理指定经济模型:

json
{
  "hooks": {
    "gmail": {
      "model": "openai/gpt-4o-mini",
      "thinking": "off"
    }
  }
}
{
  "hooks": {
    "gmail": {
      "model": "openai/gpt-4o-mini",
      "thinking": "off"
    }
  }
}

第五步:测试

  1. 重启 Gateway: crabclaw gateway
  2. 向你的 Gmail 发送一封测试邮件
  3. 观察 Crab Claw 是否收到 Webhook 并处理

查看日志:

bash
crabclaw logs --filter hooks
crabclaw logs --filter hooks

高级用法

邮件过滤

通过 SMTP 过滤规则,只处理特定发件人或主题的邮件:

json
{
  "channels": {
    "email": {
      "filters": {
        "from": ["*@important-client.com"],
        "subject": ["urgent*", "support*"]
      }
    }
  }
}
{
  "channels": {
    "email": {
      "filters": {
        "from": ["*@important-client.com"],
        "subject": ["urgent*", "support*"]
      }
    }
  }
}

SMTP 发件

配置 SMTP 使智能体可以发送邮件回复:

json
{
  "channels": {
    "email": {
      "smtp": {
        "host": "smtp.gmail.com",
        "port": 587,
        "username": "your@gmail.com",
        "password": "app-specific-password"
      }
    }
  }
}
{
  "channels": {
    "email": {
      "smtp": {
        "host": "smtp.gmail.com",
        "port": 587,
        "username": "your@gmail.com",
        "password": "app-specific-password"
      }
    }
  }
}

常见问题

Webhook 没有触发?

  • 确认公网端点可访问
  • 确认 Pub/Sub Subscription 状态正常
  • 检查 hooks.token 是否与 Pub/Sub 配置匹配

智能体处理后没有回复?

  • 检查 deliver 是否设为 true
  • 确认至少有一个活跃的消息频道