Nostr adapter for nonebot2
- Python 100%
- Adapter/Bot/Event/Message/Config 架构,参照 nonebot-adapter-qq - intent-first 订阅:kind1/9/42 带 p tag + kind14(NIP-17) - Bot.send 按事件类型路由;NIP-42 SignerAuthenticator 自动认证 - Message 段 text/image/mention;多 Bot nsec/hex 私钥配置 - 单测 + ruff + ty 通过;真实中继 chat.srcz.one 端到端验证 Co-Authored-By: Claude Code <noreply@anthropic.com> |
||
|---|---|---|
| .devcontainer | ||
| .github | ||
| nonebot/adapters/nostr | ||
| tests | ||
| .editorconfig | ||
| .gitignore | ||
| .pre-commit-config.yaml | ||
| LICENSE | ||
| pyproject.toml | ||
| README.md | ||
| uv.lock | ||
NoneBot-Adapter-Nostr
✨ Nostr 协议适配 ✨
NoneBot2 的 Nostr 协议适配器。
基于 nostr-sdk(rust-nostr 的 Python 绑定,uniffi 生成),
将 Nostr 事件映射为 NoneBot 事件。
功能
第一版(文本收发)覆盖以下 非 unrecommended 协议:
- NIP-01:kind 1 文本笔记
- NIP-10:kind 1 的回复 / 提及(
e/ptag) - NIP-17:kind 14 加密私信(gift wrap)
- NIP-29:kind 9 中继群聊消息
- NIP-28:kind 40 / 42 文本频道
后续将对接更多客户端协议。
安装
uv pip install nonebot-adapter-nostr
配置
修改 NoneBot 配置文件 .env 或 .env.*。
Driver
本适配器使用 nostr-sdk 自带的网络栈连接中继(relay),不依赖 NoneBot driver 的
HTTP / WebSocket 能力。只需配置任一 driver 即可,例如:
DRIVER=~httpx+~websockets
DRIVER=~aiohttp
NOSTR_RELAYS
全局默认中继列表,单个 Bot 未配置 relays 时回退到该列表:
NOSTR_RELAYS='["wss://relay.damus.io", "wss://nos.lol"]'
NOSTR_BOTS
配置机器人账号。私钥支持 nsec 或 hex 格式:
NOSTR_BOTS='
[
{
"private_key": "nsec1...",
"relays": ["wss://relay.damus.io"],
"follows": ["npub1..."]
}
]
'
| 字段 | 说明 |
|---|---|
private_key |
私钥(nsec 或 hex),Bot 身份标识为对应公钥 hex |
relays |
该 Bot 连接的中继,可省略以使用全局 NOSTR_RELAYS |
follows |
预留的关注公钥列表(npub / hex),供后续「关注流」扩展 |
事件模型
事件依据 kind 进行私聊 / 群聊映射(二分法):
| 事件 | kind | 语义 |
|---|---|---|
PrivateMessageEvent |
14 | NIP-17 加密私信 |
GroupMessageEvent |
1, 9 | 公开文本笔记(NIP-01/10)、群聊消息(NIP-29) |
ChannelMessageEvent |
42 | 频道消息(NIP-28) |
订阅采用「意图优先」策略:只接收指向 Bot 的事件(kind 1 / 9 / 42 中带 p tag 指向 Bot,
以及 kind 14 发给 Bot 的私信)。
消息段
text:普通文本image:图片 URL(识别出站图片扩展名的 URL)mention:npub / nprofile 提及
发送
- 公开消息(kind 1 / 9 / 42):广播到所有已连接中继。
- NIP-17 私信:按对方 NIP-65 公告的 relay list 发送,无则广播兜底。
Bot API
除 send() 外,可通过 call_api 调用:
get_profile(public_key):获取指定公钥的 kind 0 元数据。send_event(event_json):发送一个已签名的 Nostr 事件(JSON 字符串)。
开发
uv sync --dev
uv run pytest
uv run ruff check nonebot tests
uv run ty check nonebot tests
