fc33b3eb68
* docs: transfer AstrBotDevs/AstrBot-docs to AstrBotDevs/AstrBot * refactor: reorder imports and improve type hints in sync_docs_to_wiki.py and upload_doc_images_to_r2.py * feat: add GitHub Actions workflow to sync wiki with documentation Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com> Co-authored-by: anka-afk <110004162+anka-afk@users.noreply.github.com> Co-authored-by: zouyonghe <62183434+zouyonghe@users.noreply.github.com> Co-authored-by: shuiping233 <49360196+shuiping233@users.noreply.github.com> Co-authored-by: LIghtJUNction <106986785+LIghtJUNction@users.noreply.github.com> Co-authored-by: Sjshi763 <179909421+Sjshi763@users.noreply.github.com> Co-authored-by: xiewoc <70128845+xiewoc@users.noreply.github.com> Co-authored-by: QingFeng-awa <151742581+QingFeng-awa@users.noreply.github.com> Co-authored-by: PaloMiku <96452465+PaloMiku@users.noreply.github.com> Co-authored-by: shangxueink <138397030+shangxueink@users.noreply.github.com> Co-authored-by: IGCrystal-A <244300990+IGCrystal-A@users.noreply.github.com> Co-authored-by: RC-CHN <67079377+RC-CHN@users.noreply.github.com> Co-authored-by: MC090610 <113341105+MC090610@users.noreply.github.com> Co-authored-by: Waterwzy <196913419+Waterwzy@users.noreply.github.com> Co-authored-by: Lanhuace-Wan <186303160+Lanhuace-Wan@users.noreply.github.com> Co-authored-by: LiAlH4qwq <61769640+LiAlH4qwq@users.noreply.github.com> Co-authored-by: HSOS6 <209910899+HSOS6@users.noreply.github.com> Co-authored-by: th-dd <162813557+th-dd@users.noreply.github.com> Co-authored-by: miaoxutao123 <81676466+miaoxutao123@users.noreply.github.com> Co-authored-by: nuomicici <143102889+nuomicici@users.noreply.github.com> Co-authored-by: nasyt233 <210103278+nasyt233@users.noreply.github.com> Co-authored-by: jlugjb <7426462+jlugjb@users.noreply.github.com> Co-authored-by: Raven95676 <176760093+Raven95676@users.noreply.github.com> Co-authored-by: Futureppo <180109455+Futureppo@users.noreply.github.com> Co-authored-by: MliKiowa <61873808+MliKiowa@users.noreply.github.com> Co-authored-by: Fridemn <150212937+Fridemn@users.noreply.github.com> Co-authored-by: BakaCookie520 <138355736+BakaCookie520@users.noreply.github.com> Co-authored-by: YumeYuka <125112916+YumeYuka@users.noreply.github.com> Co-authored-by: xming521 <32786500+xming521@users.noreply.github.com> Co-authored-by: ywh555hhh <121592812+ywh555hhh@users.noreply.github.com> Co-authored-by: stevessr <89645372+stevessr@users.noreply.github.com> Co-authored-by: roeseth <41995115+roeseth@users.noreply.github.com> Co-authored-by: ikun-1145141 <265925499+ikun-1145141@users.noreply.github.com> Co-authored-by: evpeople <54983536+evpeople@users.noreply.github.com> Co-authored-by: Yue-bin <60509781+Yue-bin@users.noreply.github.com> Co-authored-by: W1ndys <109416673+W1ndys@users.noreply.github.com> Co-authored-by: TheFurina <218887821+TheFurina@users.noreply.github.com> Co-authored-by: Seayon <12275933+Seayon@users.noreply.github.com> Co-authored-by: OnlyblackTea <38585636+OnlyblackTea@users.noreply.github.com> Co-authored-by: ocetars <74854972+ocetars@users.noreply.github.com> Co-authored-by: railgun19457 <117180744+railgun19457@users.noreply.github.com> Co-authored-by: JunieXD <107397009+JunieXD@users.noreply.github.com> Co-authored-by: advent259141 <197440256+advent259141@users.noreply.github.com> Co-authored-by: Doge2077 <91442300+Doge2077@users.noreply.github.com> Co-authored-by: Bocity <23430545+Bocity@users.noreply.github.com> Co-authored-by: Aurora-xk <192227833+Aurora-xk@users.noreply.github.com>
151 lines
3.9 KiB
Markdown
151 lines
3.9 KiB
Markdown
---
|
||
outline: deep
|
||
---
|
||
|
||
# AstrBot HTTP API
|
||
|
||
从 v4.18.0 开始,AstrBot 提供基于 API Key 的 HTTP API,开发者可以通过标准 HTTP 请求访问核心能力。
|
||
|
||
## 快速开始
|
||
|
||
1. 在 WebUI - 设置中创建 API Key。
|
||
2. 在请求头中携带 API Key:
|
||
|
||
```http
|
||
Authorization: Bearer abk_xxx
|
||
```
|
||
|
||
也支持:
|
||
|
||
```http
|
||
X-API-Key: abk_xxx
|
||
```
|
||
|
||
3. 对于对话接口,`username` 为必填参数:
|
||
|
||
- `POST /api/v1/chat`:请求体必须包含 `username`
|
||
- `GET /api/v1/chat/sessions`:查询参数必须包含 `username`
|
||
|
||
## Scope 权限说明
|
||
|
||
创建 API Key 时可配置 `scopes`。每个 scope 控制可访问的接口范围:
|
||
|
||
| Scope | 作用 | 可访问接口 |
|
||
| --- | --- | --- |
|
||
| `chat` | 调用对话能力、查询对话会话 | `POST /api/v1/chat`、`GET /api/v1/chat/sessions` |
|
||
| `config` | 获取可用配置文件列表 | `GET /api/v1/configs` |
|
||
| `file` | 上传附件文件,获取 `attachment_id` | `POST /api/v1/file` |
|
||
| `im` | 主动发 IM 消息、查询 bot/platform 列表 | `POST /api/v1/im/message`、`GET /api/v1/im/bots` |
|
||
|
||
如果 API Key 未包含目标接口所需 scope,请求会返回 `403 Insufficient API key scope`。
|
||
|
||
## 常用接口
|
||
|
||
**对话类**
|
||
|
||
调用 AstrBot 内建的 Agent 进行对话交互。支持插件调用、工具调用等能力,与 IM 端对话能力一致。
|
||
|
||
- `POST /api/v1/chat`:发送对话消息(SSE 流式返回,不传 `session_id` 会自动创建 UUID)
|
||
- `GET /api/v1/chat/sessions`:分页获取指定 `username` 的会话
|
||
- `GET /api/v1/configs`:获取可用配置文件列表
|
||
|
||
**文件上传**
|
||
|
||
- `POST /api/v1/file`:上传附件
|
||
|
||
**IM 消息发送**
|
||
|
||
- `POST /api/v1/im/message`:按 UMO 主动发消息
|
||
- `GET /api/v1/im/bots`:获取 bot/platform ID 列表
|
||
|
||
## `message` 字段格式(重点)
|
||
|
||
`POST /api/v1/chat` 和 `POST /api/v1/im/message` 的 `message` 字段支持两种格式:
|
||
|
||
1. 字符串:纯文本消息
|
||
2. 数组:消息段(message chain)
|
||
|
||
### 1. 纯文本格式
|
||
|
||
```json
|
||
{
|
||
"message": "Hello"
|
||
}
|
||
```
|
||
|
||
### 2. 消息段数组格式
|
||
|
||
```json
|
||
{
|
||
"message": [
|
||
{ "type": "plain", "text": "请看这个文件" },
|
||
{ "type": "file", "attachment_id": "9a2f8c72-e7af-4c0e-b352-111111111111" }
|
||
]
|
||
}
|
||
```
|
||
|
||
支持的 `type`:
|
||
|
||
| type | 必填字段 | 可选字段 | 说明 |
|
||
| --- | --- | --- | --- |
|
||
| `plain` | `text` | - | 文本段 |
|
||
| `reply` | `message_id` | `selected_text` | 引用回复某条消息 |
|
||
| `image` | `attachment_id` | - | 图片附件段 |
|
||
| `record` | `attachment_id` | - | 音频附件段 |
|
||
| `file` | `attachment_id` | - | 通用文件段 |
|
||
| `video` | `attachment_id` | - | 视频附件段 |
|
||
|
||
* reply 消息段目前仅适配 `/api/v1/chat`,不适用于 `POST /api/v1/im/message`。
|
||
|
||
|
||
说明:
|
||
|
||
- `attachment_id` 来自 `POST /api/v1/file` 上传结果。
|
||
- `reply` 不能单独作为唯一内容,至少需要一个有实际内容的段(如 `plain/image/file/...`)。
|
||
- 仅 `reply` 或空内容会返回错误。
|
||
|
||
### Chat API 的 `message` 用法
|
||
|
||
`POST /api/v1/chat` 额外需要 `username`,可选 `session_id`(不传会自动创建 UUID)。
|
||
|
||
```json
|
||
{
|
||
"username": "alice",
|
||
"session_id": "my_session_001",
|
||
"message": [
|
||
{ "type": "plain", "text": "帮我总结这个 PDF" },
|
||
{ "type": "file", "attachment_id": "9a2f8c72-e7af-4c0e-b352-111111111111" }
|
||
],
|
||
"enable_streaming": true
|
||
}
|
||
```
|
||
|
||
### IM Message API 的 `message` 用法
|
||
|
||
`POST /api/v1/im/message` 需要 `umo` + `message`。
|
||
|
||
```json
|
||
{
|
||
"umo": "webchat:FriendMessage:openapi_probe",
|
||
"message": [
|
||
{ "type": "plain", "text": "这是主动消息" },
|
||
{ "type": "image", "attachment_id": "9a2f8c72-e7af-4c0e-b352-222222222222" }
|
||
]
|
||
}
|
||
```
|
||
|
||
## 示例
|
||
|
||
```bash
|
||
curl -N 'http://localhost:6185/api/v1/chat' \
|
||
-H 'Authorization: Bearer abk_xxx' \
|
||
-H 'Content-Type: application/json' \
|
||
-d '{"message":"Hello","username":"alice"}'
|
||
```
|
||
|
||
## 完整 API 文档
|
||
|
||
交互式 API 文档请查看:
|
||
|
||
- https://docs.astrbot.app/scalar.html
|