?

概述

Matrix 传输(internal/transport/matrix/)直接使用 Matrix 客户端-服务器 API,未采用第三方 SDK。入站使用 /sync 长轮询;出站使用 /rooms/{room}/send/{event_type}/{txn_id}。

可与任何符合规范的 homeserver 一起工作:Synapse、Dendrite、Conduit。

先决条件

  1. 一个 bot 账户,位于你选定的 homeserver 上。可通过标准 Matrix 客户端或通过 homeserver 管理 API 注册。

  2. 该账户的 access token。先用普通 Matrix 客户端登录一次 bot,然后从 设置 → 帮助与关于 → Access Token 复制令牌。也可以直接使用登录 API:

    curl -X POST https://matrix.org/_matrix/client/v3/login \
      -H 'Content-Type: application/json' \
      -d '{"type":"m.login.password","user":"bot","password":"..."}'
    
  3. bot 的完整 MXID(例如 @rousseau-bot:matrix.org),用于抑制自身消息回显。

配置

matrix:
  homeserver_url: "https://matrix.org"
  access_token: "syt_..."
  user_id: "@rousseau-bot:matrix.org"
  reply_header: ""
  allowlist:
    - "@alice:matrix.org"
    - "@bob:example.com"
字段 默认值 作用
homeserver_url 必填 基础 URL(https://matrix.org)。
access_token 必填 bot 用户的 access token。
user_id 空 bot 用户的完整 MXID。可选但推荐(用于抑制自身消息回显)。
reply_header 空 前置到每条出站回复上。
allowlist [] 允许处理消息的 MXID。

命令行

rousseau matrix \
  --homeserver-url https://matrix.org \
  --access-token syt_... \
  --user-id @rousseau-bot:matrix.org \
  --allow @alice:matrix.org

长轮询

PollTimeout 默认 30 秒。每次 /sync 响应中的 since 游标会保存在内存中,并用于下次调用,因此在进程生命周期内消息不会被重复投递。重启后,守护进程会回退到 homeserver 返回的仍然有效的最早游标 —— 这是标准的 sync 语义,与所有 Matrix 客户端一致。

房间邀请

bot 必须已是它要回复的每个房间的成员。请通过普通 Matrix 客户端进行邀请。rousseau 不会自动接受邀请;加入房间不在其职责范围内。

失败模式

现象 解决方式
/sync 返回 401 Access token 已过期或已失效。请重新登录。
bot 从未收到消息 确认 bot 已经是房间成员,而不仅仅是被邀请。
自身消息回显环路 在配置中设置 user_id,让 rousseau 过滤掉自身消息。

Type to search 150+ pages. Ranking: BM25 with title/description boost.