Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]增加python-sdk的基础框架文档,需要继续完善,勿使用
- Loading branch information
Showing
17 changed files
with
1,731 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# Python SDK 接入指南 | ||
|
||
## 当前版本 | ||
![PyPI](https://img.shields.io/pypi/v/qq-bot) | ||
|
||
## 安装 | ||
|
||
``` bash | ||
pip install qq-bot | ||
``` | ||
|
||
更新包的话需要添加 ``--upgrade`` ``注:需要python3.7+`` | ||
|
||
## 使用示例 | ||
|
||
下面的例子,通过api获取当前机器人的相关信息: | ||
|
||
``` py | ||
import qqbot | ||
|
||
token = qqbot.Token("{appid}","{token}") | ||
api = qqbot.UserAPI(token, IS_SANDBOX) | ||
user = api.me() | ||
|
||
print(user.username) # 打印机器人名字 | ||
``` | ||
|
||
## ws消息监听 | ||
|
||
通过注册需要监听的事件并设置回调函数后,即可完成对事件的监听。 | ||
|
||
比如下面这个例子:需要监听机器人被@后消息并进行相应的回复。 | ||
|
||
``` py | ||
# 先初始化需要用的 `token` 对象 | ||
t_token = qqbot.Token(test_config["token"]["appid"], test_config["token"]["token"]) | ||
# 通过 `qqbot.HandlerType` 定义需要监听的事件(部分事件可能需要权限申请)可以注册多个 | ||
qqbot_handler = qqbot.Handler(qqbot.HandlerType.AT_MESSAGE_EVENT_HANDLER, _message_handler) | ||
# 通过 `qqbot.listen_events` 注册需要监听的事件 | ||
qqbot.listen_events(t_token, False, qqbot_handler) | ||
|
||
# 定义注册事件回调执行函数,如 `_message_handler` | ||
def _message_handler(event, message: Message): | ||
msg_api = qqbot.MessageAPI(t_token, False) | ||
# 打印返回信息 | ||
qqbot.logger.info("event %s" % event + ",receive message %s" % message.content) | ||
# 构造消息发送请求数据对象 | ||
send = qqbot.MessageSendRequest("<@%s>谢谢你,加油" % message.author.id, message.id) | ||
# 通过api发送回复消息 | ||
msg_api.post_message(message.channel_id, send) | ||
``` | ||
|
||
注:当前支持事件及回调事件包括: | ||
|
||
``` py | ||
class HandlerType(Enum): | ||
PLAIN_EVENT_HANDLER = 0 #透传事件 | ||
GUILD_EVENT_HANDLER = 1 #频道事件 | ||
GUILD_MEMBER_EVENT_HANDLER = 2 #频道成员事件 | ||
CHANNEL_EVENT_HANDLER = 3 #子频道事件 | ||
MESSAGE_EVENT_HANDLER = 4 #消息事件 | ||
AT_MESSAGE_EVENT_HANDLER = 5 #At消息事件 | ||
# DIRECT_MESSAGE_EVENT_HANDLER = 6 #私信消息事件 | ||
# AUDIO_EVENT_HANDLER = 7 #音频事件 | ||
``` | ||
|
||
事件回调函数的参数 1 为事件名称,参数 2 返回具体的数据对象。 | ||
|
||
``` py | ||
#透传事件(无具体的数据对象,根据后台返回Json对象) | ||
def _plain_handler(event, data): | ||
#频道事件 | ||
def _guild_handler(event, guild:Guild): | ||
#频道成员事件 | ||
def _guild_member_handler(event, guild_member: GuildMember): | ||
#子频道事件 | ||
def _channel_handler(event, channel: Channel): | ||
#消息事件 #At消息事件 | ||
def _message_handler(event, message: Message): | ||
``` | ||
|
||
## 日志打印 | ||
|
||
基于自带的 logging 模块封装的日志模块,提供了日志写入以及美化了打印格式,并支持通过设置 `QQBOT_LOG_LEVEL` 环境变量来调整日志打印级别(默认打印级别为 `INFO`)。 | ||
|
||
### 使用方法 | ||
|
||
``` py | ||
# 引用模块 | ||
from core.util import logging | ||
|
||
# 获取 `logger` 实例 | ||
logger = logging.getLogger(__name__) | ||
|
||
# 打印日志 | ||
logger.info("hello world!") | ||
``` | ||
|
||
### 设置日志级别 | ||
|
||
通过 `export` 命令添加 `QQBOT_LOG_LEVEL` 环境变量可以设置日志级别。例如: | ||
|
||
``` bash | ||
export QQBOT_LOG_LEVEL=10 # 10表示DEBUG级别 | ||
``` | ||
|
||
几个可选取值(参考了[logging模块的取值](https://docs.python.org/3/library/logging.html#levels)): | ||
|
||
| Level | 取值 | | ||
| ----- | ------------- | | ||
| CRITICAL | 50 | | ||
| ERROR | 40 | | ||
| WARNING | 30 | | ||
| INFO | 20 | | ||
| DEBUG | 10 | | ||
| NOTSET | 0 | | ||
|
||
### 禁用日志文件输出 | ||
|
||
默认情况下 qqbot 会在当前执行目录下生成格式为 `qqbot.log.*` 的日志文件。如果想禁用这些日志文件,可以通过设置 `QQBOT_DISABLE_LOG` 环境变量为 1 来关闭。 | ||
|
||
``` bash | ||
export QQBOT_DISABLE_LOG=1 # 1表示禁用日志 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# 获取频道详情 | ||
|
||
获取频道详情信息。 | ||
|
||
## 使用示例 | ||
|
||
```javascript | ||
async function demo() { | ||
let { data } = await client.guildApi.guild(guildId); | ||
} | ||
``` | ||
|
||
## 参数说明 | ||
|
||
| 字段名 | 必填 | 类型 | 描述 | | ||
| ------- | ---- | ------ | ------- | | ||
| guildId | 是 | string | 频道 ID | | ||
|
||
## 返回说明 | ||
|
||
返回 [Guild](#guild) 对象。 | ||
|
||
### Guild | ||
|
||
| 字段名 | 类型 | 描述 | | ||
| ------------ | ------- | ------------------ | | ||
| id | string | 频道 ID | | ||
| name | string | 频道名称 | | ||
| owner_id | string | 创建人用户 ID | | ||
| owner | boolean | 当前人是否是创建人 | | ||
| member_count | number | 成员数 | | ||
| max_members | number | 最大成员数 | | ||
| description | string | 描述 | | ||
|
||
## 返回示例 | ||
|
||
`data`: | ||
|
||
```python | ||
{ | ||
"id":"guildId", | ||
"name":"频道名称", | ||
"owner_id":"owner_id", | ||
"owner":false, | ||
"member_count":8, | ||
"max_members":300, | ||
"description":"" | ||
} | ||
``` |
51 changes: 51 additions & 0 deletions
51
docs/develop/pythonsdk/guild/guild_role/create_guild_role.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# 创建频道身份组 | ||
|
||
创建一个频道身份组。 | ||
|
||
## 使用示例 | ||
|
||
```python | ||
import qqbot | ||
|
||
token = qqbot.Token({appid}, {token}) | ||
|
||
api = qqbot.GuildRoleAPI(token, False) | ||
result = api.create_guild_role(guildId, roleInfo) | ||
``` | ||
|
||
::: warning 注意 | ||
需要使用的 token 对应的用户具备创建身份组权限。如果是机器人,要求被添加为管理员。 | ||
::: | ||
|
||
## 参数说明 | ||
|
||
| 字段名 | 必填 | 类型 | 描述 | | ||
| -------- | ---- | --------------------- | -------------- | | ||
| guildId | 是 | string | 频道 ID | | ||
| roleInfo | 是 | [RoleInfo](#roleinfo) | 频道身份组参数 | | ||
|
||
### RoleInfo | ||
|
||
| 字段名 | 必填 | 类型 | 描述 | | ||
| ------ | ---- | ------ | -------------------------------------------------------------- | | ||
| name | 是 | string | 名称 | | ||
| color | 否 | number | ARGB 的 HEX 十六进制颜色值转换后的十进制数值(例:4294927682) | | ||
| hoist | 否 | number | 在成员列表中单独展示: 0-否, 1-是 | | ||
|
||
## 返回说明 | ||
|
||
| 字段名 | 类型 | 描述 | | ||
| ------- | ----------------------- | -------------- | | ||
| role_id | string | 频道身份组 ID | | ||
| role | [GuildRole](#guildrole) | 频道身份组对象 | | ||
|
||
### GuildRole | ||
|
||
| 字段名 | 类型 | 描述 | | ||
| ------------ | ------ | -------------------------------------------------------------- | | ||
| id | string | 身份组 ID | | ||
| name | string | 名称 | | ||
| color | number | ARGB 的 HEX 十六进制颜色值转换后的十进制数值(例:4294927682) | | ||
| hoist | number | 是否在成员列表中单独展示: 0-否, 1-是 | | ||
| number | number | 人数 | | ||
| member_limit | number | 成员上限 | |
48 changes: 48 additions & 0 deletions
48
docs/develop/pythonsdk/guild/guild_role/create_guild_role_member.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 创建频道身份组成员 | ||
|
||
创建频道身份组成员。 | ||
|
||
## 使用示例 | ||
|
||
```python | ||
import qqbot | ||
|
||
token = qqbot.Token({appid}, {token}) | ||
|
||
api = qqbot.GuildRoleAPI(token, False) | ||
result = api.create_guild_role_member(guildId, roleId, userId, channel) | ||
``` | ||
|
||
::: warning 注意 | ||
|
||
- 需要使用的 `token` 对应的用户具备增加身份组成员权限,如果是机器人,要求被添加为管理员。 | ||
- 如果要增加的身份组 `ID` 是子频道管理员,需要增加 `Channel` 对象来指定具体是哪个子频道。 | ||
|
||
::: | ||
|
||
## 参数说明 | ||
|
||
| 字段名 | 必填 | 类型 | 描述 | | ||
| ------- | ---- | ------------------- | ------------------------------------ | | ||
| guildId | 是 | string | 频道 ID | | ||
| roleId | 是 | string | 身份组 ID | | ||
| userId | 是 | string | 用户 ID | | ||
| channel | 是 | [Channel](#channel) | 接收一个只填充了子频道 ID 字段的对象 | | ||
|
||
### Channel | ||
|
||
| 字段名 | 类型 | 描述 | | ||
| ------ | ------ | --------- | | ||
| id | string | 子频道 ID | | ||
|
||
## 返回说明 | ||
|
||
添加是否成功 | ||
|
||
## 返回示例 | ||
|
||
`data`: | ||
|
||
```python | ||
True | ||
``` |
31 changes: 31 additions & 0 deletions
31
docs/develop/pythonsdk/guild/guild_role/delete_guild_role.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 删除频道身份组 | ||
|
||
删除频道身份组。 | ||
|
||
```python | ||
import qqbot | ||
|
||
token = qqbot.Token({appid}, {token}) | ||
|
||
api = qqbot.GuildRoleAPI(token, False) | ||
result = api.delete_guild_role(guildId, roleId) | ||
``` | ||
|
||
### 参数说明 | ||
|
||
| 参数 | 必填 | 类型 | 说明 | | ||
| ------- | ---- | ------ | --------- | | ||
| guildId | 是 | string | 频道 ID | | ||
| roleId | 是 | string | 身份组 ID | | ||
|
||
### 返回说明 | ||
|
||
是否删除成功 | ||
|
||
### 返回示例 | ||
|
||
`data`: | ||
|
||
```python | ||
True | ||
``` |
47 changes: 47 additions & 0 deletions
47
docs/develop/pythonsdk/guild/guild_role/delete_guild_role_member.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# 删除频道身份组成员 | ||
|
||
删除频道身份组成员。 | ||
|
||
## 使用示例 | ||
|
||
```python | ||
import qqbot | ||
|
||
token = qqbot.Token({appid}, {token}) | ||
|
||
api = qqbot.GuildRoleAPI(token, False) | ||
result = api.delete_guild_role_member(guildId, roleId, userId, channel) | ||
``` | ||
|
||
::: warning 注意 | ||
|
||
- 需要使用的 token 对应的用户具备删除身份组成员权限。如果是机器人,要求被添加为管理员。 | ||
- 如果要删除的身份组 ID 是`5-子频道管理员`,需要增加 channel 对象来指定具体是哪个子频道 | ||
::: | ||
|
||
## 参数说明 | ||
|
||
| 字段名 | 必填 | 类型 | 描述 | | ||
| ------- | ---- | ------------------- | ------------------------------------ | | ||
| guildId | 是 | string | 频道 ID | | ||
| roleId | 是 | string | 身份组 ID | | ||
| userId | 是 | string | 用户 ID | | ||
| channel | 是 | [Channel](#channel) | 接收一个只填充了子频道 ID 字段的对象 | | ||
|
||
### Channel | ||
|
||
| 字段名 | 类型 | 描述 | | ||
| ------ | ------ | --------- | | ||
| id | string | 子频道 ID | | ||
|
||
## 返回说明 | ||
|
||
返回删除是否成功 | ||
|
||
## 返回示例 | ||
|
||
`data`: | ||
|
||
```python | ||
True | ||
``` |
Oops, something went wrong.