Scan your whole stack
Are you using nonebot-adapter-matrix?
Find out what’s in your code.
Scan your apps free in minutes (opens in a new tab)Latest version: v0.6.0
nonebot-adapter-matrix was last scanned on 29 Nov 2026 at version 0.6.0. Risk score 0 of 100. No malicious behavior was detected.
| Version | Published | Status |
|---|---|---|
| 0.6.0 LATEST | 29 Nov 2026 | No rules fired |
| 0.5.0 | 29 Nov 2026 | No rules fired |
| 0.4.2 | 26 Nov 2026 | No rules fired |
| 0.4.1 | 13 Oct 2026 | Not shown |
| 0.4.0 | 10 Oct 2026 | Not shown |
Findings for v0.6.0. The score is a capped sum of the weights of the rules that fired — not CVSS, not exploitability, and not a probability. It is not comparable across packages with different scan coverage.
The score describes 0.6.0; other versions have their own rules, not their own score.
The publisher’s own README, as collected from the registry.
✨ Matrix Client-Server 协议适配 ✨
pip install nonebot-adapter-matrix
开发版本可从当前仓库构建安装:
pip install git+https://github.com/elysia-best/adapter-matrix.git@master
Matrix adapter 使用 Client-Server API 与 homeserver 通信,需要可发起 HTTP 请求的 NoneBot ForwardDriver。
DRIVER=~httpx
MATRIX_LOG_LEVEL=INFO
将 MATRIX_LOG_LEVEL 设置为 DEBUG 时,Matrix 同步错误日志会包含完整 traceback;默认 INFO 仅显示异常类型和消息。
适配器支持三种 token 管理模式:
适合已有 access token 的场景。不会自动获取或刷新 token。
MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "YOUR_ACCESS_TOKEN",
"user_id": "@bot:example.org"
}
]'
注意:此模式下如果没有额外提供登录凭据或 OAuth2 配置,协议上无法自动获取首个 refresh token。access token 过期后需要手动更新配置。
提供登录凭据,适配器启动时自动调用 /login 获取 token 对,并在需要时自动 refresh。
MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "",
"login_user": "@bot:example.org",
"login_password": "your-password",
"device_id": "BOTDEVICE",
"set_presence": "online"
}
]'
login_user:Matrix 用户 ID,用于 /login 请求。login_password:Matrix 账户密码。login_initial_device_display_name:可选;初始设备显示名称。session_type: "legacy_login",后续 refresh 走 /_matrix/client/v3/refresh。soft_logout: true,适配器会自动用密码重新登录。通过 OAuth2 Authorization Code + PKCE 登录,适合支持 Matrix next-gen auth / OIDC 的 homeserver。
最小配置:
MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "",
"oauth_enabled": true,
"oauth_server_url": "https://account.matrix.org",
"oauth_open_browser": true
}
]'
也可以手动指定已注册的 client:
MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "",
"oauth_enabled": true,
"oauth_server_url": "https://account.matrix.org",
"oauth_client_id": "your-client-id",
"oauth_redirect_uri": "https://your-app.example/callback"
}
]'
oauth_enabled:启用 OAuth2 登录流程。oauth_server_url:可选;直接指定 OAuth2/OIDC server 根地址,例如 https://account.matrix.org。若未提供,会优先尝试 Matrix /_matrix/client/v1/auth_metadata 自动发现。oauth_metadata_url:可选;直接指定 metadata 文档地址,优先级高于 oauth_server_url。oauth_client_id:可选;若未提供且 server 暴露 registration_endpoint,适配器会自动注册一个 OAuth2 client 并持久化保存。oauth_client_uri:可选;动态注册时写入 client metadata。某些 Matrix Authentication Service(例如 matrix.org)要求该字段存在;未配置时默认使用 homeserver 作为回退值。需要注意这里需要填写有效的域名oauth_redirect_uri:可选;若不提供,则默认使用 loopback 回调并自动选择一个可用随机端口。若提供 localhost / 127.0.0.1 回调地址,则必须显式写出端口,且注册、授权、换 token 全程使用该 URI 原样值;若想用自动随机端口,请直接省略 oauth_redirect_uri。若提供外部回调地址,则需要手动复制授权结果中的 code 或完整回调 URL 到终端。oauth_scope:可选;默认会构造符合 MSC2967 的 scope,并自动补上 urn:matrix:org.matrix.msc2967.client:device:{DEVICE_ID};若你自定义 scope,适配器仍会补 device scope。oauth_device_id:可选;指定请求的 Matrix device ID。未提供时会自动生成一个 12 位的大写字母数字 ID。oauth_open_browser:是否自动打开浏览器访问授权 URL,默认 false。oauth_callback_timeout:等待回调的超时时间(秒),默认 300。OAuth2 登录流程:
oauth_metadata_url → /_matrix/client/v1/auth_metadata(及其 unstable MSC2965 端点)→ oauth_server_url 兜底。response_type=code、response_mode=fragment 和 PKCE S256。oauth_client_id,则通过 metadata 返回的 registration_endpoint 自动注册 client。state、code_verifier/code_challenge,并构造符合 MSC2967 的 scope。oauth_open_browser: true 则自动打开浏览器)。grant_type=refresh_token)。MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "YOUR_ACCESS_TOKEN",
"refresh_token": "OPTIONAL_REFRESH_TOKEN",
"access_token_expires_at_ms": 1760000000000,
"refresh_before_expiry_ms": 60000,
"user_id": "@bot:example.org",
"device_id": "BOTDEVICE",
"sync_filter": {"room": {"timeline": {"limit": 50}}},
"set_presence": "online",
"auto_accept_invites": true,
"auto_accept_whitelist": null,
"auto_accept_blacklist": []
}
]'
homeserver:Matrix homeserver 根地址(必填)。access_token:当前使用的 Matrix access token(使用登录模式时可留空)。refresh_token:通常由登录流程获得,不要求手动填写。持久化到 MATRIX_TOKEN_STORE_PATH 后会自动加载。access_token_expires_at_ms:当前 access token 的绝对过期时间戳,单位毫秒。refresh_before_expiry_ms:距离过期多久前主动 refresh,默认 60000(1 分钟)。user_id:启动时通过 /account/whoami 校验 token 所属用户。device_id:记录当前 token 对应的 Matrix 设备 ID。sync_filter:传给 /sync 的 filter id 或 filter JSON。set_presence:online、offline 或 unavailable。auto_accept_invites:是否启用自动接受群聊邀请,默认 false。auto_accept_whitelist:允许自动接受邀请的用户白名单,值为用户 ID 列表(如 ["@alice:example.org"])。设为 null 表示允许所有用户,设为空列表 [] 表示不允许任何用户(行为同 auto_accept_invites: false)。auto_accept_blacklist:禁止自动接受邀请的用户黑名单,默认 []。黑名单优先级高于白名单。MATRIX_API_TIMEOUT=30.0
MATRIX_SYNC_TIMEOUT=30000
MATRIX_RETRY_INTERVAL=3.0
MATRIX_COMMAND_TO_ME=false
MATRIX_HANDLE_SELF_MESSAGE=false
MATRIX_HANDLE_OLD_EVENTS=false
MATRIX_PROXY='http://127.0.0.1:7890'
MATRIX_TOKEN_STORE_PATH='.data/matrix-tokens.json'
MATRIX_API_TIMEOUT:普通 API 请求超时时间,单位秒。MATRIX_SYNC_TIMEOUT:/sync long-poll 超时时间,单位毫秒。MATRIX_RETRY_INTERVAL:网络错误后的重试间隔,单位秒。MATRIX_COMMAND_TO_ME:是否将以 NoneBot 命令前缀开头的消息视为发给机器人,默认 false。MATRIX_HANDLE_SELF_MESSAGE:是否处理机器人自己发送的消息。MATRIX_HANDLE_OLD_EVENTS:是否处理早于本次启动时间的旧事件,默认丢弃旧事件。MATRIX_PROXY:可选 HTTP 代理。MATRIX_TOKEN_STORE_PATH:可选 token 状态文件路径;配置后会将 token 持久化到该文件,重启时自动加载。MATRIX_TOKEN_STORE_PATH,会优先读取状态文件中的最新 token 和 session_type。access_token_expires_at_ms 时,会在下一次 /sync 前主动 refresh。M_UNKNOWN_TOKEN,adapter 会尝试使用 refresh token 恢复。soft_logout: true:若配置了 login_password,自动重新登录。soft_logout:视为会话失效,等待重试。access_token、refresh_token、access_token_expires_at_ms,并写回状态 json 文件。.env 或其他 dotenv 文件;同样,/sync 的 next_batch 不会持久化。适配器支持 Matrix 端到端加密房间。E2EE 默认启用,底层使用上游 python3-olm 的 libolm Olm v1/Megolm v1,并用 cryptography 完成 Secret Storage、附件和本地密钥保护。需要明文设备时显式设置 e2ee_enabled: false。
MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "YOUR_ACCESS_TOKEN",
"user_id": "@bot:example.org",
"device_id": "BOTDEVICE",
"recovery_key": "EsTb ...",
"secret_storage_passphrase": "OPTIONAL_PASSPHRASE",
"e2ee_store_path": ".data/e2ee_store"
}
]'
recovery_key:Matrix Secret Storage recovery key,用于恢复交叉签名秘密和备份私钥;备份私钥可通过 bot.encryption().backups().enable(private_key) 单独导入。secret_storage_passphrase:用于读取现有 Secret Storage 及其备份密钥的 passphrase。recovery_code:recovery_key 的兼容别名,已弃用。e2ee_store_path:E2EE 加密状态的持久化目录路径。未设置时从 MATRIX_TOKEN_STORE_PATH 同目录或 .data/matrix/e2ee/ 自动派生,并绑定 homeserver、用户和设备 ID。e2ee_enabled:是否初始化 E2EE,默认 true。E2EE 工作流程:
recovery_key 或 Secret Storage passphrase,从服务端现有密钥备份恢复 Megolm 会话。m.room.encryption 状态事件时,自动标记该房间为加密。首次向加密房间发送消息前,会通过 Olm to-device 消息向所有成员设备共享 Megolm 会话密钥。m.room.encrypted 事件发送;收到的加密消息自动解密为明文后再分派给插件处理。/sync 中的 device_lists 变更,查询新设备的密钥,保持设备密钥缓存更新。注意事项:
matrix_auto_accept_verification 只接受验证请求,SAS/emoji 和二维码必须由插件比较后显式确认。bot.encryption() 的对象 API 访问。from nonebot import on_command
from nonebot.params import CommandArg
from nonebot.adapters.matrix import Bot, Message, MessageEvent, MessageSegment
matcher = on_command("echo")
@matcher.handle()
async def handle_echo(bot: Bot, event: MessageEvent, msg: Message = CommandArg()):
text = msg.extract_plain_text()
if text == "mention":
await matcher.finish(MessageSegment.mention_user(event.get_user_id()))
if text == "notice":
await matcher.finish(MessageSegment.notice("这是一条 Matrix notice"))
await bot.send(event, MessageSegment.text(text or "hello matrix"))
Matrix 媒体需要先上传到 media repository,消息正文再引用返回的 mxc:// URI;MessageSegment.image/file/audio/video 在传入 bytes 时会自动执行这个流程。
from pathlib import Path
from nonebot import on_command
from nonebot.adapters.matrix import Bot, MessageEvent, MessageSegment
matcher = on_command("image")
@matcher.handle()
async def handle_img_send():
cur_dir = os.path.dirname(__file__)
# Read img from current directory
content = Path(os.path.join(cur_dir, "./assets/test.jpg")).read_bytes()
await send_img.finish(
MessageSegment.image(
content,
filename="test.jpg",
content_type="image/jpg"
)
)
需要注意,content_type 字段需要传入正确的 MIME Type 才能在客户端显示!
await bot.react(event.room_id, event.event_id, "👍")
await bot.set_typing_state(event.room_id, typing=True, timeout=5000)
await bot.mark_read(event.room_id, event.event_id)
await bot.edit_message(event.room_id, message_event_id, "更新后的消息")
await bot.edit_media_caption(event.room_id, media_event_id, "新的说明")
await bot.edit_media_caption(event.room_id, media_event_id, None) # 清除说明,保留文件名
await bot.redact(event.room_id, event.event_id, reason="handled")
await bot.join_room("!roomid:example.org", reason="Auto-joined")
edit_message 和 edit_media_caption 仅能编辑机器人自己发送的原始消息;编辑媒体说明不会重新上传媒体。使用 redact 撤回消息。
适配器支持按照白名单 / 黑名单自动接受 Matrix 群聊邀请。当收到邀请时,适配器会从 invite_state 中提取邀请人信息,根据配置决定是否自动加入房间。
配置示例:
MATRIX_BOTS='[
{
"homeserver": "https://matrix.example.org",
"access_token": "YOUR_ACCESS_TOKEN",
"user_id": "@bot:example.org",
"auto_accept_invites": true,
"auto_accept_whitelist": ["@alice:example.org", "@bob:example.org"],
"auto_accept_blacklist": ["@spammer:matrix.org"]
}
]'
auto_accept_whitelist 设置为 null(或不配置),则允许任意用户邀请机器人入群。当前实现面向 Matrix Client-Server bot 场景:
/account/whoami 校验身份。/sync long-poll 接收 room timeline、state、typing、receipt 等事件。m.room.message、上传媒体、reaction、redaction、typing、receipt 和 join room。/sync 的 next_batch;进程内重连会复用内存状态,跨进程重启默认丢弃早于本次启动时间的旧事件。MessageSegment.markdown() 会把安全 CommonMark 转换为 Matrix HTML 富文本,并自动生成纯文本回退:
from nonebot.adapters.matrix import MessageSegment
await bot.send(
event,
MessageSegment.markdown("# 标题\n\n这是 **加粗** 文本。"),
)
Markdown 输入中的原始 HTML 会被转义;普通 MessageSegment.text() 不会自动解析 Markdown。
Find threats in your stack.
OX Radar can check high-priority threats, like critical package CVEs, against your context.
Each incident is marked Detected or Not Detected with a remediation plan and proof all tracked in the Response Center.
The probe returned only these fields for this server — auth requirement, protocol and tool list weren’t observed, so they’re not shown (never guessed).
The publisher’s own links, as the registry reports them. These are not sources for the assessment on this page.