qbotsdk 0.6.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- qbotsdk-0.6.0/CHANGELOG.md +27 -0
- qbotsdk-0.6.0/LICENSE +201 -0
- qbotsdk-0.6.0/PKG-INFO +250 -0
- qbotsdk-0.6.0/README.md +214 -0
- qbotsdk-0.6.0/pyproject.toml +130 -0
- qbotsdk-0.6.0/src/qbotsdk/__init__.py +76 -0
- qbotsdk-0.6.0/src/qbotsdk/adapter/__init__.py +9 -0
- qbotsdk-0.6.0/src/qbotsdk/adapter/base.py +35 -0
- qbotsdk-0.6.0/src/qbotsdk/adapter/webhook.py +78 -0
- qbotsdk-0.6.0/src/qbotsdk/adapter/websocket.py +65 -0
- qbotsdk-0.6.0/src/qbotsdk/api/__init__.py +46 -0
- qbotsdk-0.6.0/src/qbotsdk/api/_parse.py +29 -0
- qbotsdk-0.6.0/src/qbotsdk/api/channel.py +136 -0
- qbotsdk-0.6.0/src/qbotsdk/api/group.py +184 -0
- qbotsdk-0.6.0/src/qbotsdk/api/guild.py +73 -0
- qbotsdk-0.6.0/src/qbotsdk/api/interaction.py +34 -0
- qbotsdk-0.6.0/src/qbotsdk/api/message.py +55 -0
- qbotsdk-0.6.0/src/qbotsdk/api/reaction.py +125 -0
- qbotsdk-0.6.0/src/qbotsdk/api/user.py +57 -0
- qbotsdk-0.6.0/src/qbotsdk/api/user_message.py +115 -0
- qbotsdk-0.6.0/src/qbotsdk/bot.py +136 -0
- qbotsdk-0.6.0/src/qbotsdk/constant.py +10 -0
- qbotsdk-0.6.0/src/qbotsdk/exceptions.py +195 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/__init__.py +61 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/connection.py +316 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/dispatcher.py +142 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/handler.py +81 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/payload.py +198 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/session.py +59 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/webhook.py +66 -0
- qbotsdk-0.6.0/src/qbotsdk/gateway/webhook_server.py +171 -0
- qbotsdk-0.6.0/src/qbotsdk/model/__init__.py +145 -0
- qbotsdk-0.6.0/src/qbotsdk/model/base.py +9 -0
- qbotsdk-0.6.0/src/qbotsdk/model/channel.py +74 -0
- qbotsdk-0.6.0/src/qbotsdk/model/draft.py +234 -0
- qbotsdk-0.6.0/src/qbotsdk/model/event.py +410 -0
- qbotsdk-0.6.0/src/qbotsdk/model/group.py +119 -0
- qbotsdk-0.6.0/src/qbotsdk/model/guild.py +29 -0
- qbotsdk-0.6.0/src/qbotsdk/model/keyboard.py +112 -0
- qbotsdk-0.6.0/src/qbotsdk/model/media.py +43 -0
- qbotsdk-0.6.0/src/qbotsdk/model/member.py +27 -0
- qbotsdk-0.6.0/src/qbotsdk/model/message.py +79 -0
- qbotsdk-0.6.0/src/qbotsdk/model/reaction.py +65 -0
- qbotsdk-0.6.0/src/qbotsdk/model/role.py +31 -0
- qbotsdk-0.6.0/src/qbotsdk/model/scene.py +98 -0
- qbotsdk-0.6.0/src/qbotsdk/model/token.py +9 -0
- qbotsdk-0.6.0/src/qbotsdk/model/user.py +63 -0
- qbotsdk-0.6.0/src/qbotsdk/module/__init__.py +1 -0
- qbotsdk-0.6.0/src/qbotsdk/module/http_client.py +372 -0
- qbotsdk-0.6.0/src/qbotsdk/module/pagination.py +74 -0
- qbotsdk-0.6.0/src/qbotsdk/module/rate_limit.py +101 -0
- qbotsdk-0.6.0/src/qbotsdk/module/token_manager.py +151 -0
- qbotsdk-0.6.0/src/qbotsdk/options.py +43 -0
- qbotsdk-0.6.0/src/qbotsdk/py.typed +1 -0
- qbotsdk-0.6.0/src/qbotsdk/storage/__init__.py +28 -0
- qbotsdk-0.6.0/src/qbotsdk/storage/base.py +42 -0
- qbotsdk-0.6.0/src/qbotsdk/storage/file.py +142 -0
- qbotsdk-0.6.0/src/qbotsdk/storage/memory.py +25 -0
- qbotsdk-0.6.0/src/qbotsdk/storage/redis.py +219 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# 更新日志
|
|
2
|
+
|
|
3
|
+
## [0.6.0] - 2026-10-28
|
|
4
|
+
|
|
5
|
+
首个发布到 PyPI 的版本。
|
|
6
|
+
|
|
7
|
+
### 新增
|
|
8
|
+
|
|
9
|
+
- 事件接入改为适配器模式,默认提供`WebSocketAdapter`与`WebhookAdapter`,默认使用 `WebSocketAdapter()`
|
|
10
|
+
- 会话存储抽象 `SessionStorage`
|
|
11
|
+
- 新增 `build_webhook_handler(dispatcher, app_secret)`
|
|
12
|
+
- 群聊与单聊消息带 `keyboard` 时自动升成 Markdown 消息
|
|
13
|
+
- 新增 `qbotsdk.__version__`
|
|
14
|
+
- 新增六个示例以及`examples/README.md`。
|
|
15
|
+
|
|
16
|
+
### 变更
|
|
17
|
+
|
|
18
|
+
- 会话存储从 `qbotsdk.gateway.storage` 移到顶层的 `qbotsdk.storage`。
|
|
19
|
+
- Redis 支持抽成可选依赖 `qbotsdk[redis]`
|
|
20
|
+
- 全仓导入统一为绝对导入,由 `ruff` 的 `TID` 规则约束。
|
|
21
|
+
|
|
22
|
+
### 破坏性变更
|
|
23
|
+
|
|
24
|
+
- `Bot` 默认使用 `WebSocketAdapter()`;`Bot` 的 `intents` 与 `shard` 参数移到 `WebSocketAdapter`。
|
|
25
|
+
- `WebhookServer` 的第一个参数从 `Bot` 改成 `Dispatcher`。
|
|
26
|
+
|
|
27
|
+
[0.6.0]: https://github.com/half-nothing/qbotsdk/releases/tag/v0.6.0
|
qbotsdk-0.6.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
qbotsdk-0.6.0/PKG-INFO
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: qbotsdk
|
|
3
|
+
Version: 0.6.0
|
|
4
|
+
Summary: Async SDK for the QQ bot open platform
|
|
5
|
+
Keywords: qq,qqbot,bot,sdk,websocket,webhook,asyncio
|
|
6
|
+
Author-Email: Half_nothing <Half_nothing@163.com>
|
|
7
|
+
Maintainer-Email: Half_nothing <Half_nothing@163.com>
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Internet
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Project-URL: Homepage, https://github.com/half-nothing/qbotsdk
|
|
23
|
+
Project-URL: Repository, https://github.com/half-nothing/qbotsdk
|
|
24
|
+
Project-URL: Issues, https://github.com/half-nothing/qbotsdk/issues
|
|
25
|
+
Project-URL: Changelog, https://github.com/half-nothing/qbotsdk/blob/main/CHANGELOG.md
|
|
26
|
+
Requires-Python: >=3.12
|
|
27
|
+
Requires-Dist: aiohttp>=3.14.3
|
|
28
|
+
Requires-Dist: pydantic>=2.13.4
|
|
29
|
+
Requires-Dist: yarl>=1.24.5
|
|
30
|
+
Requires-Dist: loguru>=0.7.3
|
|
31
|
+
Requires-Dist: cryptography>=50.0.2
|
|
32
|
+
Requires-Dist: async-event-bus>=1.0.0
|
|
33
|
+
Provides-Extra: redis
|
|
34
|
+
Requires-Dist: redis>=8.1.0; extra == "redis"
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# qbotsdk
|
|
38
|
+
|
|
39
|
+
非官方 QQ 机器人 SDK
|
|
40
|
+
|
|
41
|
+
[](https://www.python.org/)
|
|
42
|
+
[](./LICENSE)
|
|
43
|
+
[](https://pypi.org/project/qbotsdk/)
|
|
44
|
+
|
|
45
|
+
## 特性
|
|
46
|
+
|
|
47
|
+
- **全异步**:基于 `asyncio` 与 `aiohttp`,所有接口均为异步接口。
|
|
48
|
+
- **登录态自动维护**:`access_token` 自动换取、缓存、到期前刷新。
|
|
49
|
+
- **连接与重连**:WebSocket 适配器自动心跳、断线重连与 Resume。
|
|
50
|
+
- **接入方式可插拔**:默认走 WebSocket 长连接,也可以换成 Webhook 回调,或自己实现 `Adapter`。
|
|
51
|
+
- **消息即对象**:收到的事件可以直接 `await event.reply(...)`。
|
|
52
|
+
- **容错内置**:429 按 `Retry-After` 重试,幂等请求遇 5xx 与网络异常退避重试,本地令牌桶限流,错误统一成 `QQBotError` 体系。
|
|
53
|
+
- **类型完整**:pydantic v2 模型、全量类型标注、随包分发 `py.typed`。
|
|
54
|
+
|
|
55
|
+
## 安装
|
|
56
|
+
|
|
57
|
+
包名与导入名均为 `qbotsdk`,要求 Python 版本 >= 3.12。
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install qbotsdk
|
|
61
|
+
uv add qbotsdk # uv 项目里加依赖
|
|
62
|
+
uv pip install qbotsdk # 或直接装进当前环境
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
要从源码安装
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
git clone https://github.com/half-nothing/qbotsdk.git
|
|
69
|
+
cd qbotsdk
|
|
70
|
+
|
|
71
|
+
pip install -e . # 可编辑模式,改完立刻生效
|
|
72
|
+
uv pip install -e . # uv 同理
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 快速开始
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
import asyncio
|
|
79
|
+
import os
|
|
80
|
+
|
|
81
|
+
from qbotsdk import Bot
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
async def main() -> None:
|
|
85
|
+
# https://q.qq.com/qqbot/dashboard/manage
|
|
86
|
+
# 进入上方网站管理机器人,在开发设置中找到“AppID 接入凭证”卡片
|
|
87
|
+
# 将环境变量QQ_BOT_APP_ID设为获取到的AppID
|
|
88
|
+
# 将环境变量QQ_BOT_APP_SECRET设为获取到的AppSecret
|
|
89
|
+
# 如果您选择直接替换对应内容,请不要将携带敏感信息的文件提交到仓库
|
|
90
|
+
async with Bot(
|
|
91
|
+
app_id=os.environ["QQ_BOT_APP_ID"],
|
|
92
|
+
app_secret=os.environ["QQ_BOT_APP_SECRET"],
|
|
93
|
+
) as bot:
|
|
94
|
+
me = await bot.api.user.me()
|
|
95
|
+
print(f"登录成功:{me.username}")
|
|
96
|
+
|
|
97
|
+
async for guild in bot.api.user.iter_guilds():
|
|
98
|
+
channels = await bot.api.guild.channels(guild.id)
|
|
99
|
+
print(f"{guild.name}:{len(channels)} 个子频道")
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
asyncio.run(main())
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## 接收事件
|
|
106
|
+
|
|
107
|
+
默认走 WebSocket 长连接
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
import asyncio
|
|
111
|
+
|
|
112
|
+
from async_event_bus import EventContext
|
|
113
|
+
|
|
114
|
+
from qbotsdk import Bot
|
|
115
|
+
from qbotsdk.model.event import C2CMessageCreate, GroupAtMessageCreate, InteractionCreate
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
async def main() -> None:
|
|
119
|
+
# 认证信息的具体获取方式请查看“快速开始”章节
|
|
120
|
+
async with Bot(app_id=..., app_secret=...) as bot:
|
|
121
|
+
|
|
122
|
+
@bot.on(GroupAtMessageCreate)
|
|
123
|
+
async def on_group_at(ctx: EventContext[GroupAtMessageCreate]) -> None:
|
|
124
|
+
await ctx.event.reply("收到: " + ctx.event.content)
|
|
125
|
+
|
|
126
|
+
@bot.on(C2CMessageCreate)
|
|
127
|
+
async def on_c2c(ctx: EventContext[C2CMessageCreate]) -> None:
|
|
128
|
+
await ctx.event.reply("收到: " + ctx.event.content)
|
|
129
|
+
|
|
130
|
+
@bot.on(InteractionCreate)
|
|
131
|
+
async def on_interaction(ctx: EventContext[InteractionCreate]) -> None:
|
|
132
|
+
if ctx.event.needs_response:
|
|
133
|
+
await ctx.event.respond() # 不回应客户端会一直 loading
|
|
134
|
+
|
|
135
|
+
await bot.start()
|
|
136
|
+
await asyncio.sleep(3600)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
asyncio.run(main())
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
bot的`adapter`参数默认为`WebSocketAdapter`,可以显式指定为`WebhookAdapter`
|
|
143
|
+
也可以自行实现`Adapter`从而扩展更多接入方式,自行实现的`Adapter`需要继承`qbotsdk.adapter.Adapter`并实现两个抽象方法
|
|
144
|
+
|
|
145
|
+
`WebSocketAdapter`的会话状态默认存在内存里
|
|
146
|
+
如果想关闭后自动续连,可以在构造`WebSocketAdapter`的时候传递参数`session_storage=`
|
|
147
|
+
可选值为`MemorySessionStorage`, `FileSessionStorage`, `RedisSessionStorage`, 默认为`MemorySessionStorage`
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from qbotsdk import WebSocketAdapter
|
|
151
|
+
from qbotsdk.storage import FileSessionStorage
|
|
152
|
+
|
|
153
|
+
# 若不传存储路径,默认路径为软件运行目录下的.session.json文件
|
|
154
|
+
WebSocketAdapter(session_storage=FileSessionStorage())
|
|
155
|
+
|
|
156
|
+
# Redis 在可选依赖里, 使用前需要先安装qbotsdk[redis]
|
|
157
|
+
from qbotsdk.storage import RedisSessionStorage
|
|
158
|
+
|
|
159
|
+
# 给完整地址
|
|
160
|
+
WebSocketAdapter(session_storage=RedisSessionStorage.from_url("redis://:pwd@10.0.0.5:6379/2"))
|
|
161
|
+
|
|
162
|
+
# 或者只给零件, 地址由SDK拼
|
|
163
|
+
storage = RedisSessionStorage.from_url(host="10.0.0.5", port=6379, password="pwd", db=2)
|
|
164
|
+
WebSocketAdapter(session_storage=storage)
|
|
165
|
+
|
|
166
|
+
# Unix domain socket, 以及指定RESP协议版本
|
|
167
|
+
RedisSessionStorage.from_url(unix_socket="/var/run/redis/redis.sock", db=2, protocol=3)
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
也可以直接把现成的Redis客户端交给SDK
|
|
171
|
+
|
|
172
|
+
```python
|
|
173
|
+
# 特别注意,SDK需要的Redis实例全限定名是redis.asyncio.Redis
|
|
174
|
+
from redis.asyncio import Redis
|
|
175
|
+
|
|
176
|
+
from qbotsdk.storage import RedisSessionStorage
|
|
177
|
+
|
|
178
|
+
storage = RedisSessionStorage(Redis(host="10.0.0.5", port=6379))
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
### Webhook 接入
|
|
183
|
+
|
|
184
|
+
在[QQ机器人管理后台](https://q.qq.com/qqbot/dashboard/manage)填好回调地址并勾选要监听的事件后,
|
|
185
|
+
事件就改走 HTTP 推送,代码层面仅需把适配器换成 `WebhookAdapter`:
|
|
186
|
+
|
|
187
|
+
```python
|
|
188
|
+
from qbotsdk import Bot, WebhookAdapter
|
|
189
|
+
|
|
190
|
+
bot = Bot(
|
|
191
|
+
# 认证信息的具体获取方式请查看“快速开始”章节
|
|
192
|
+
app_id=...,
|
|
193
|
+
app_secret=...,
|
|
194
|
+
adapter=WebhookAdapter(
|
|
195
|
+
"同app_secret",
|
|
196
|
+
port=8443,
|
|
197
|
+
path="/qbotsdk",
|
|
198
|
+
certfile="certs/fullchain.pem",
|
|
199
|
+
keyfile="certs/privkey.pem",
|
|
200
|
+
),
|
|
201
|
+
)
|
|
202
|
+
await bot.start()
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
没有公网地址时用反向代理或内网穿透把请求转到本机,务必原样透传 body 和两个签名头,任何改写都会导致验签失败。
|
|
206
|
+
|
|
207
|
+
如果事件要接进自己已有的aiohttp应用,不用起整套服务器,挂一个处理函数就行:
|
|
208
|
+
|
|
209
|
+
```python
|
|
210
|
+
from aiohttp import web
|
|
211
|
+
|
|
212
|
+
from qbotsdk import Bot, build_webhook_handler
|
|
213
|
+
|
|
214
|
+
bot = Bot(app_id=..., app_secret=...)
|
|
215
|
+
app = web.Application()
|
|
216
|
+
app.router.add_post("/qbotsdk", build_webhook_handler(bot.dispatcher, "机器人密钥"))
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
如果要使用flask等其他http框架,仿照该处理函数写一遍处理函数即可。
|
|
220
|
+
|
|
221
|
+
## 示例
|
|
222
|
+
|
|
223
|
+
具体示例请见[examples文件夹](examples/README.md)。
|
|
224
|
+
|
|
225
|
+
## 第三方依赖
|
|
226
|
+
|
|
227
|
+
| 依赖 | 用途 | 许可 |
|
|
228
|
+
|--------------------------------------------------------------------|-------------------------|----------------------------|
|
|
229
|
+
| [aiohttp](https://github.com/aio-libs/aiohttp) | HTTP 与 WebSocket 客户端 | Apache-2.0 AND MIT |
|
|
230
|
+
| [pydantic](https://github.com/pydantic/pydantic) | 数据模型与校验 | MIT |
|
|
231
|
+
| [loguru](https://github.com/Delgan/loguru) | 日志 | MIT |
|
|
232
|
+
| [cryptography](https://github.com/pyca/cryptography) | Webhook 的 ed25519 签名与验签 | Apache-2.0 OR BSD-3-Clause |
|
|
233
|
+
| [async-event-bus](https://github.com/half-nothing/async-event-bus) | 事件总线 | MIT |
|
|
234
|
+
| [yarl](https://github.com/aio-libs/yarl) | URL 拼接 | Apache-2.0 |
|
|
235
|
+
|
|
236
|
+
## 致谢
|
|
237
|
+
|
|
238
|
+
- [QQ 机器人官方文档](https://bot.q.qq.com/wiki/develop/api-v2):字段、错误码与协议细节都以它为准。
|
|
239
|
+
- [async-event-bus](https://github.com/half-nothing/async-event-bus):事件总线。
|
|
240
|
+
- [aiohttp](https://github.com/aio-libs/aiohttp):HTTP 与 WebSocket 客户端。
|
|
241
|
+
- [pydantic](https://github.com/pydantic/pydantic):数据模型与校验。
|
|
242
|
+
- [loguru](https://github.com/Delgan/loguru):日志。
|
|
243
|
+
- [cryptography](https://github.com/pyca/cryptography):ed25519 签名与验签。
|
|
244
|
+
- [yarl](https://github.com/aio-libs/yarl):URL 拼接。
|
|
245
|
+
- 腾讯官方 Python SDK 文档与社区实现:心跳、Resume、关闭码语义对照过。
|
|
246
|
+
- 所有反馈过的使用者。
|
|
247
|
+
|
|
248
|
+
## 许可证
|
|
249
|
+
|
|
250
|
+
Apache-2.0,见 [LICENSE](LICENSE)。
|