邮件自动化
配置邮件监听、自动回复与智能分类
本教程介绍如何将 Gmail 收件箱接入 Crab Claw,实现邮件自动监听、智能分类和自动回复。
架构
Gmail → Pub/Sub Push → Crab Claw Webhook → 智能体处理 → 回复/分类Gmail → Pub/Sub Push → Crab Claw Webhook → 智能体处理 → 回复/分类前置条件
- Crab Claw 已安装并运行
- Google Cloud 项目已创建
gcloudCLI 已安装并登录- 公网可访问的 HTTPS 端点(推荐 Tailscale Funnel)
第一步:启用 Hooks
编辑 ~/.crabclaw/crabclaw.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
- 在 Google Cloud Console 创建 Pub/Sub Topic
- 配置 Gmail Watch API 将邮件推送到该 Topic
- 设置 Pub/Sub Subscription 将消息推送到 Crab Claw Webhook URL
详细步骤请参考 Google Gmail Push Notifications 文档。
第三步:自定义处理规则
可以自定义邮件处理映射:
{
"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(上次使用的频道)或指定频道 |
第四步:设置模型
为邮件处理指定经济模型:
{
"hooks": {
"gmail": {
"model": "openai/gpt-4o-mini",
"thinking": "off"
}
}
}{
"hooks": {
"gmail": {
"model": "openai/gpt-4o-mini",
"thinking": "off"
}
}
}第五步:测试
- 重启 Gateway:
crabclaw gateway - 向你的 Gmail 发送一封测试邮件
- 观察 Crab Claw 是否收到 Webhook 并处理
查看日志:
crabclaw logs --filter hookscrabclaw logs --filter hooks高级用法
邮件过滤
通过 SMTP 过滤规则,只处理特定发件人或主题的邮件:
{
"channels": {
"email": {
"filters": {
"from": ["*@important-client.com"],
"subject": ["urgent*", "support*"]
}
}
}
}{
"channels": {
"email": {
"filters": {
"from": ["*@important-client.com"],
"subject": ["urgent*", "support*"]
}
}
}
}SMTP 发件
配置 SMTP 使智能体可以发送邮件回复:
{
"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 - 确认至少有一个活跃的消息频道