🔀 Merge pull request #425

Feature: 添加飞书适配器支持
This commit is contained in:
Ju4tCode
2021-07-18 22:42:37 +08:00
committed by GitHub
25 changed files with 2486 additions and 106 deletions

View File

@ -1,11 +1,11 @@
const path = require("path");
module.exports = context => ({
module.exports = (context) => ({
base: process.env.VUEPRESS_BASE || "/",
title: "NoneBot",
description: "跨平台 Python 异步 QQ 机器人框架",
markdown: {
lineNumbers: true
lineNumbers: true,
},
/**
* Extra tags to be injected to the page HTML `<head>`
@ -21,26 +21,26 @@ module.exports = context => ({
["meta", { name: "apple-mobile-web-app-capable", content: "yes" }],
[
"meta",
{ name: "apple-mobile-web-app-status-bar-style", content: "black" }
{ name: "apple-mobile-web-app-status-bar-style", content: "black" },
],
[
"link",
{ rel: "apple-touch-icon", href: "/icons/apple-touch-icon-180x180.png" }
{ rel: "apple-touch-icon", href: "/icons/apple-touch-icon-180x180.png" },
],
[
"link",
{
rel: "mask-icon",
href: "/icons/safari-pinned-tab.svg",
color: "#ea5252"
}
color: "#ea5252",
},
],
[
"meta",
{
name: "msapplication-TileImage",
content: "/icons/mstile-150x150.png"
}
content: "/icons/mstile-150x150.png",
},
],
["meta", { name: "msapplication-TileColor", content: "#ea5252" }],
[
@ -48,16 +48,16 @@ module.exports = context => ({
{
rel: "stylesheet",
href:
"https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css"
}
]
"https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5/css/all.min.css",
},
],
],
locales: {
"/": {
lang: "zh-CN",
title: "NoneBot",
description: "跨平台 Python 异步 QQ 机器人框架"
}
description: "跨平台 Python 异步 QQ 机器人框架",
},
},
theme: "nonebot",
@ -83,7 +83,7 @@ module.exports = context => ({
{ text: "进阶", link: "/advanced/" },
{ text: "API", link: "/api/" },
{ text: "商店", link: "/store" },
{ text: "更新日志", link: "/changelog" }
{ text: "更新日志", link: "/changelog" },
],
sidebarDepth: 2,
sidebar: {
@ -97,8 +97,8 @@ module.exports = context => ({
"installation",
"getting-started",
"creating-a-project",
"basic-configuration"
]
"basic-configuration",
],
},
{
title: "编写插件",
@ -109,15 +109,20 @@ module.exports = context => ({
"creating-a-plugin",
"creating-a-matcher",
"creating-a-handler",
"end-or-start"
]
"end-or-start",
],
},
{
title: "协议适配",
collapsable: false,
sidebar: "auto",
children: ["cqhttp-guide", "ding-guide", "mirai-guide"]
}
children: [
"cqhttp-guide",
"ding-guide",
"mirai-guide",
"feishu-guide",
],
},
],
"/advanced/": [
{
@ -130,15 +135,15 @@ module.exports = context => ({
"permission",
"runtime-hook",
"export-and-require",
"overloaded-handlers"
]
"overloaded-handlers",
],
},
{
title: "发布",
collapsable: false,
sidebar: "auto",
children: ["publish-plugin"]
}
children: ["publish-plugin"],
},
],
"/api/": [
{
@ -148,86 +153,90 @@ module.exports = context => ({
children: [
{
title: "nonebot 模块",
path: "nonebot"
path: "nonebot",
},
{
title: "nonebot.config 模块",
path: "config"
path: "config",
},
{
title: "nonebot.plugin 模块",
path: "plugin"
path: "plugin",
},
{
title: "nonebot.message 模块",
path: "message"
path: "message",
},
{
title: "nonebot.matcher 模块",
path: "matcher"
path: "matcher",
},
{
title: "nonebot.handler 模块",
path: "handler"
path: "handler",
},
{
title: "nonebot.rule 模块",
path: "rule"
path: "rule",
},
{
title: "nonebot.permission 模块",
path: "permission"
path: "permission",
},
{
title: "nonebot.log 模块",
path: "log"
path: "log",
},
{
title: "nonebot.utils 模块",
path: "utils"
path: "utils",
},
{
title: "nonebot.typing 模块",
path: "typing"
path: "typing",
},
{
title: "nonebot.exception 模块",
path: "exception"
path: "exception",
},
{
title: "nonebot.drivers 模块",
path: "drivers/"
path: "drivers/",
},
{
title: "nonebot.drivers.fastapi 模块",
path: "drivers/fastapi"
path: "drivers/fastapi",
},
{
title: "nonebot.drivers.quart 模块",
path: "drivers/quart"
path: "drivers/quart",
},
{
title: "nonebot.adapters 模块",
path: "adapters/"
path: "adapters/",
},
{
title: "nonebot.adapters.cqhttp 模块",
path: "adapters/cqhttp"
path: "adapters/cqhttp",
},
{
title: "nonebot.adapters.ding 模块",
path: "adapters/ding"
path: "adapters/ding",
},
{
title: "nonebot.adapters.mirai 模块",
path: "adapters/mirai"
}
]
}
]
}
}
}
path: "adapters/mirai",
},
{
title: "nonebot.adapters.feishu 模块",
path: "adapters/feishu",
},
],
},
],
},
},
},
},
plugins: [
@ -239,9 +248,9 @@ module.exports = context => ({
serviceWorker: true,
updatePopup: {
message: "发现新内容",
buttonText: "刷新"
}
}
buttonText: "刷新",
},
},
],
[
"versioning",
@ -250,16 +259,16 @@ module.exports = context => ({
pagesSourceDir: path.resolve(context.sourceDir, "..", "pages"),
onNewVersion(version, versionDestPath) {
console.log(`Created version ${version} in ${versionDestPath}`);
}
}
},
},
],
[
"container",
{
type: "vue",
before: '<pre class="vue-container"><code>',
after: "</code></pre>"
}
]
]
after: "</code></pre>",
},
],
],
});

View File

@ -30,5 +30,13 @@
"desc": "在原 CQHTTP Adapter 的基础上进行修改以便更好地适配 go-cqhttp",
"author": "Jigsaw111",
"repo": "Jigsaw111/nonebot-adapter-gocq"
},
{
"id": "nonebot_adapter_feishu",
"link": "nonebot-adapter-feishu",
"name": "feishu",
"desc": "飞书协议",
"author": "StarHeartHunt",
"repo": "nonebot/nonebot2/tree/master/packages/nonebot-adapter-feishu"
}
]
]

View File

@ -11,5 +11,5 @@ features:
details: 精心设计的消息处理流程使得你可以很方便地将原型扩充为具有大量实用功能的完整聊天机器人,并持续保证扩展性。
- title: 高性能
details: 采用异步 I/O利用 WebSocket 进行通信,以获得极高的性能;同时,支持使用多账号同时接入,减少业务宕机的可能。
footer: MIT Licensed | Copyright © 2018 - 2020 NoneBot Team
footer: MIT Licensed | Copyright © 2018 - 2021 NoneBot Team
---

View File

@ -59,3 +59,6 @@
* [nonebot.adapters.mirai](adapters/mirai.html)
* [nonebot.adapters.feishu](adapters/feishu.html)

251
docs/api/adapters/feishu.md Normal file
View File

@ -0,0 +1,251 @@
---
contentSidebar: true
sidebarDepth: 0
---
# NoneBot.adapters.feishu 模块
# NoneBot.adapters.feishu.config 模块
## _class_ `Config`
钉钉配置类
* **配置项**
* `app_id` / `feishu_app_id`: 飞书开放平台后台“凭证与基础信息”处给出的 App ID
* `app_secret` / `feishu_app_secret`: 飞书开放平台后台“凭证与基础信息”处给出的 App Secret
* `encrypt_key` / `feishu_encrypt_key`: 飞书开放平台后台“事件订阅”处设置的 Encrypt Key
* `verification_token` / `feishu_verification_token`: 飞书开放平台后台“事件订阅”处设置的 Verification Token
* `tenant_access_token` / `feishu_tenant_access_token`: 请求飞书 API 后返回的租户密钥
# NoneBot.adapters.feishu.exception 模块
## _exception_ `ActionFailed`
基类:[`nonebot.exception.ActionFailed`](../exception.md#nonebot.exception.ActionFailed), `nonebot.adapters.feishu.exception.FeishuAdapterException`
* **说明**
API 请求返回错误信息。
* **参数**
* `retcode: Optional[int]`: 错误码
## _exception_ `NetworkError`
基类:[`nonebot.exception.NetworkError`](../exception.md#nonebot.exception.NetworkError), `nonebot.adapters.feishu.exception.FeishuAdapterException`
* **说明**
网络错误。
* **参数**
* `retcode: Optional[int]`: 错误码
# NoneBot.adapters.feishu.bot 模块
## `_check_at_me(bot, event)`
* **说明**
检查消息开头或结尾是否存在 @机器人,去除并赋值 `event.reply`, `event.to_me`
* **参数**
* `bot: Bot`: Bot 对象
* `event: Event`: Event 对象
## `_check_nickname(bot, event)`
* **说明**
检查消息开头是否存在昵称,去除并赋值 `event.to_me`
* **参数**
* `bot: Bot`: Bot 对象
* `event: Event`: Event 对象
## `_handle_api_result(result)`
* **说明**
处理 API 请求返回值。
* **参数**
* `result: Optional[Dict[str, Any]]`: API 返回数据
* **返回**
* `Any`: API 调用返回数据
* **异常**
* `ActionFailed`: API 调用失败
## _class_ `Bot`
基类:[`nonebot.adapters._base.Bot`](README.md#nonebot.adapters._base.Bot)
飞书 协议 Bot 适配。继承属性参考 [BaseBot](./#class-basebot) 。
### _async_ `handle_message(message)`
* **说明**
处理事件并转换为 [Event](#class-event)
### _async_ `call_api(api, **data)`
* **说明**
调用 飞书 协议 API
* **参数**
* `api: str`: API 名称
* `**data: Any`: API 参数
* **返回**
* `Any`: API 调用返回数据
* **异常**
* `NetworkError`: 网络错误
* `ActionFailed`: API 调用失败
# NoneBot.adapters.feishu.message 模块
## _class_ `MessageSegment`
基类:[`nonebot.adapters._base.MessageSegment`](README.md#nonebot.adapters._base.MessageSegment)[`Message`]
飞书 协议 MessageSegment 适配。具体方法参考协议消息段类型或源码。
## _class_ `Message`
基类:[`nonebot.adapters._base.Message`](README.md#nonebot.adapters._base.Message)[`nonebot.adapters.feishu.message.MessageSegment`]
飞书 协议 Message 适配。
## _class_ `MessageSerializer`
基类:`object`
飞书 协议 Message 序列化器。
## _class_ `MessageDeserializer`
基类:`object`
飞书 协议 Message 反序列化器。
# NoneBot.adapters.feishu.event 模块
## _class_ `Event`
基类:[`nonebot.adapters._base.Event`](README.md#nonebot.adapters._base.Event)
飞书协议事件。各事件字段参考
```
`飞书文档`_
```
## `get_event_model(event_name)`
* **说明**
根据事件名获取对应 `Event Model``FallBack Event Model` 列表
* **返回**
* `List[Type[Event]]`

View File

@ -0,0 +1,57 @@
# 飞书机器人使用指南
基于飞书开放平台事件回调与 API 进行机器人适配,目前仅适配企业自建应用。
## 安装 NoneBot 飞书 适配器
```bash
pip install nonebot-adapter-feishu
```
## 创建应用与启用应用“机器人”能力
::: tip
此部分可参考[飞书开放平台-快速开发机器人-创建应用](https://open.feishu.cn/document/home/develop-a-bot-in-5-minutes/create-an-app)部分文档。
:::
## 开启应用权限
应用拥有所需权限后,才能调用飞书接口获取相关信息。如果需要用到所有飞书平台的 API请开启所有应用权限。
在仅群聊功能的情况下,需要为应用开启用户、消息、通讯录和群聊权限组所有权限。
## 配置飞书事件订阅
::: tip
在添加事件订阅时请注意,带有**(历史版本)**字样的事件的格式为**不受支持的旧版事件格式**,请使用对应的**新版事件(不带历史版本字样)作为替代**。
:::
目前,飞书适配器支持以下事件:
| 事件名称 | 事件描述|
| ---- | ---- |
|接收消息|机器人接收到用户发送的消息。|
|消息已读|用户阅读机器人发送的单聊消息。|
|群解散|群组被解散。|
|群配置更改|群组配置被修改后触发此事件,包含:群主转移、群基本信息修改、群权限修改。|
|机器人进群|机器人被添加至群聊。|
|机器人被移出群|机器人被移出群聊。|
|用户进群|新用户进群。|
|撤销拉用户进群|撤销拉用户进群。|
|用户被移出群|用户主动退群或被移出群聊。|
## 在 NoneBot 配置中添加相应配置
`.env` 文件中添加以下部分
```
APP_ID=<yourAppId>
APP_SECRET=<yourAppSecret>
VERIFICATION_TOKEN=<yourVerificationToken>
```
复制所创建应用**“凭证和基础信息”**中的**App ID**与**App Secret**及**“事件订阅”**中的**Verification Token**,替换上面相应的配置的值。
大功告成!现在可以试试向机器人发送消息进行测试了。