Skip to main content

iMessage(旧版:imsg)

新的 iMessage 部署请使用 BlueBubblesimsg 集成为旧版方案,未来版本可能会移除。
状态:旧版外部 CLI 集成。网关启动 imsg rpc,通过 stdio 以 JSON-RPC 通信(没有独立守护进程或端口)。

快速设置

1

安装并验证 imsg

brew install steipete/tap/imsg
imsg rpc --help
2

配置 OpenClaw

{
  channels: {
    imessage: {
      enabled: true,
      cliPath: "/usr/local/bin/imsg",
      dbPath: "/Users/<你>/Library/Messages/chat.db",
    },
  },
}
3

启动网关

openclaw gateway
4

批准首次私信配对(默认 dmPolicy)

openclaw pairing list imessage
openclaw pairing approve imessage <>
配对请求会在 1 小时后过期。

要求和权限(macOS)

  • 执行 imsg 的 Mac 必须已登录 Messages。
  • 运行 OpenClaw/imsg 的进程上下文必须具有完整磁盘访问权限(以访问 Messages 数据库)。
  • 发送消息需要 Messages.app 的自动化权限。
权限是基于进程上下文授予的。如果网关以无头方式运行(LaunchAgent/SSH),请在同一上下文中执行一次交互命令以触发权限提示:
imsg chats --limit 1
# 或者
imsg send <handle> "test"

访问控制和路由

channels.imessage.dmPolicy 控制私信类型:
  • pairing(默认)
  • allowlist
  • open(要求 allowFrom 包含 "*"
  • disabled
允许名单字段:channels.imessage.allowFrom允许名单条目可以是句柄或聊天目标(chat_id:*chat_guid:*chat_identifier:*)。

部署模式

使用专用 Apple ID 和 macOS 用户,使机器人消息流与个人 Messages 配置隔离。通常流程:
  1. 创建/登录专用 macOS 用户。
  2. 在该用户中使用机器人 Apple ID 登录 Messages。
  3. 在该用户中安装 imsg
  4. 创建 SSH 包装脚本,使 OpenClaw 能在该用户上下文运行 imsg
  5. channels.imessage.accounts.<id>.cliPath.dbPath 指向该用户配置。
初次运行可能需要在该机器人用户会话中手动批准 GUI 权限(自动化与完整磁盘访问)。
常见拓扑:
  • 网关运行在 Linux/虚拟机上
  • iMessage + imsg 运行在 tailnet 中的 Mac 上
  • cliPath 包装脚本使用 SSH 运行 imsg
  • remoteHost 支持 SCP 附件获取
示例:
{
  channels: {
    imessage: {
      enabled: true,
      cliPath: "~/.openclaw/scripts/imsg-ssh",
      remoteHost: "bot@mac-mini.tailnet-1234.ts.net",
      includeAttachments: true,
      dbPath: "/Users/bot/Library/Messages/chat.db",
    },
  },
}
#!/usr/bin/env bash
exec ssh -T bot@mac-mini.tailnet-1234.ts.net imsg "$@"
请使用 SSH 密钥以实现 SSH 和 SCP 的无交互认证。 确保可信任主机密钥(例如执行 ssh bot@mac-mini.tailnet-1234.ts.net),以便填充 known_hosts
iMessage 支持在 channels.imessage.accounts 下进行多账号配置。每个账号可覆盖如 cliPathdbPathallowFromgroupPolicymediaMaxMb、历史设置及附件根目录允许名单等字段。

媒体、分块及投递目标

  • 可选接收附件:channels.imessage.includeAttachments
  • 设置 remoteHost 时可通过 SCP 获取远程附件路径
  • 附件路径需符合允许根目录:
    • channels.imessage.attachmentRoots(本地)
    • channels.imessage.remoteAttachmentRoots(远程 SCP 模式)
    • 默认模式:/Users/*/Library/Messages/Attachments
  • SCP 使用严格的主机密钥检查(StrictHostKeyChecking=yes
  • 发送媒体大小限制由 channels.imessage.mediaMaxMb 控制(默认 16 MB)
  • 文本分块限制:channels.imessage.textChunkLimit(默认 4000)
  • 分块模式:channels.imessage.chunkMode
    • length(默认)
    • newline(优先按段落分割)
推荐的显式目标格式:
  • chat_id:123(推荐用于稳定路由)
  • chat_guid:...
  • chat_identifier:...
也支持句柄目标:
  • imessage:+1555...
  • sms:+1555...
  • user@example.com
imsg chats --limit 20

配置写入

iMessage 默认允许通道发起的配置写入(针对启用了 commands.config: true/config set|unset 命令)。 禁用示例:
{
  channels: {
    imessage: {
      configWrites: false,
    },
  },
}

故障排查

验证二进制及 RPC 支持:
imsg rpc --help
openclaw channels status --probe
若探测显示不支持 RPC,请更新 imsg
检查:
  • channels.imessage.dmPolicy
  • channels.imessage.allowFrom
  • 配对批准(openclaw pairing list imessage
检查:
  • channels.imessage.groupPolicy
  • channels.imessage.groupAllowFrom
  • channels.imessage.groups 允许名单规则
  • 提及模式配置(agents.list[].groupChat.mentionPatterns
检查:
  • channels.imessage.remoteHost
  • channels.imessage.remoteAttachmentRoots
  • 网关主机的 SSH/SCP 密钥认证
  • 网关主机 ~/.ssh/known_hosts 是否包含主机密钥
  • 运行 Messages 的 Mac 上远程路径的可读性
在相同用户/会话上下文的交互式 GUI 终端中重新运行并批准权限:
imsg chats --limit 1
imsg send <handle> "test"
确认运行 OpenClaw/imsg 的进程上下文已授予完整磁盘访问和自动化权限。

配置参考指引