imbot-sdk-python 0.1.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.
- imbot_sdk_python-0.1.0/CHANGELOG.md +27 -0
- imbot_sdk_python-0.1.0/LICENSE +201 -0
- imbot_sdk_python-0.1.0/MANIFEST.in +17 -0
- imbot_sdk_python-0.1.0/PKG-INFO +296 -0
- imbot_sdk_python-0.1.0/README.md +262 -0
- imbot_sdk_python-0.1.0/docs/API.md +164 -0
- imbot_sdk_python-0.1.0/docs/PROTOCOL.md +76 -0
- imbot_sdk_python-0.1.0/examples/.env.example +15 -0
- imbot_sdk_python-0.1.0/examples/echo_bot.py +128 -0
- imbot_sdk_python-0.1.0/examples/quickstart.py +45 -0
- imbot_sdk_python-0.1.0/examples/run.sh +22 -0
- imbot_sdk_python-0.1.0/imbot_sdk/__init__.py +57 -0
- imbot_sdk_python-0.1.0/imbot_sdk/client.py +1155 -0
- imbot_sdk_python-0.1.0/imbot_sdk/consts.py +36 -0
- imbot_sdk_python-0.1.0/imbot_sdk/data_accessor.py +35 -0
- imbot_sdk_python-0.1.0/imbot_sdk/listeners.py +67 -0
- imbot_sdk_python-0.1.0/imbot_sdk/messages/__init__.py +93 -0
- imbot_sdk_python-0.1.0/imbot_sdk/messages/base.py +54 -0
- imbot_sdk_python-0.1.0/imbot_sdk/messages/contents.py +463 -0
- imbot_sdk_python-0.1.0/imbot_sdk/models.py +99 -0
- imbot_sdk_python-0.1.0/imbot_sdk/pb/__init__.py +0 -0
- imbot_sdk_python-0.1.0/imbot_sdk/pb/appmessages_pb2.py +433 -0
- imbot_sdk_python-0.1.0/imbot_sdk/pb/chatroom_pb2.py +98 -0
- imbot_sdk_python-0.1.0/imbot_sdk/pb/connect_pb2.py +55 -0
- imbot_sdk_python-0.1.0/imbot_sdk/pb/rtcroom_pb2.py +82 -0
- imbot_sdk_python-0.1.0/imbot_sdk/py.typed +0 -0
- imbot_sdk_python-0.1.0/imbot_sdk_python.egg-info/PKG-INFO +296 -0
- imbot_sdk_python-0.1.0/imbot_sdk_python.egg-info/SOURCES.txt +37 -0
- imbot_sdk_python-0.1.0/imbot_sdk_python.egg-info/dependency_links.txt +1 -0
- imbot_sdk_python-0.1.0/imbot_sdk_python.egg-info/requires.txt +6 -0
- imbot_sdk_python-0.1.0/imbot_sdk_python.egg-info/top_level.txt +1 -0
- imbot_sdk_python-0.1.0/proto/appmessages.proto +1246 -0
- imbot_sdk_python-0.1.0/proto/chatroom.proto +171 -0
- imbot_sdk_python-0.1.0/proto/connect.proto +87 -0
- imbot_sdk_python-0.1.0/proto/gen.sh +32 -0
- imbot_sdk_python-0.1.0/proto/rtcroom.proto +164 -0
- imbot_sdk_python-0.1.0/pyproject.toml +48 -0
- imbot_sdk_python-0.1.0/requirements.txt +2 -0
- imbot_sdk_python-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented here. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/), and the project adheres to
|
|
5
|
+
[Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-06-25
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- Initial release of `imbot-sdk-python`.
|
|
12
|
+
- Connection management: `connect` / `reconnect` / `disconnect` / `logout`,
|
|
13
|
+
heartbeat keep-alive, and automatic reconnect with backoff.
|
|
14
|
+
- Message send/receive for private, group, chatroom and public-channel
|
|
15
|
+
conversations, with high-level listeners and raw protobuf callbacks.
|
|
16
|
+
- Message management: history query, recall, modify, mark-read, search and
|
|
17
|
+
top-message APIs.
|
|
18
|
+
- Conversation management: list, unread count, top, mute and tags.
|
|
19
|
+
- User / friend / group info and online-status subscription.
|
|
20
|
+
- Chatroom join/quit, messaging and attribute sync.
|
|
21
|
+
- RTC room create / join / query / quit / invite.
|
|
22
|
+
- File credential query.
|
|
23
|
+
- Built-in message content models (text, image, file, video, voice, stream
|
|
24
|
+
text, recall info, merge, thumbnail-packed image, snapshot-packed video) with
|
|
25
|
+
an `UnknownMessage` fallback.
|
|
26
|
+
|
|
27
|
+
[0.1.0]: https://github.com/juggleim/imbot-sdk-python/releases/tag/v0.1.0
|
|
@@ -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.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
include README.md
|
|
2
|
+
include LICENSE
|
|
3
|
+
include requirements.txt
|
|
4
|
+
include CHANGELOG.md
|
|
5
|
+
include imbot_sdk/py.typed
|
|
6
|
+
|
|
7
|
+
# Source .proto definitions (for regenerating the protobuf modules)
|
|
8
|
+
recursive-include proto *.proto *.sh
|
|
9
|
+
|
|
10
|
+
# Docs and runnable examples
|
|
11
|
+
recursive-include docs *.md
|
|
12
|
+
recursive-include examples *.py *.sh
|
|
13
|
+
include examples/.env.example
|
|
14
|
+
|
|
15
|
+
# Keep build artifacts out of the sdist
|
|
16
|
+
global-exclude __pycache__/*
|
|
17
|
+
global-exclude *.py[cod]
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: imbot-sdk-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: JuggleIM Python Bot SDK — WebSocket long-connection IM client for bots and server-side agents
|
|
5
|
+
Author-email: Tyler <tyler@juggle.im>
|
|
6
|
+
Maintainer-email: Tyler <tyler@juggle.im>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
Project-URL: Homepage, https://github.com/juggleim/imbot-sdk-python
|
|
9
|
+
Project-URL: Repository, https://github.com/juggleim/imbot-sdk-python
|
|
10
|
+
Project-URL: Issues, https://github.com/juggleim/imbot-sdk-python/issues
|
|
11
|
+
Project-URL: Documentation, https://github.com/juggleim/imbot-sdk-python#readme
|
|
12
|
+
Keywords: juggleim,im,instant-messaging,chat,bot,websocket,sdk
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Communications :: Chat
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.8
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Requires-Dist: protobuf>=5.0
|
|
29
|
+
Requires-Dist: websocket-client>=1.6
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: build; extra == "dev"
|
|
32
|
+
Requires-Dist: twine; extra == "dev"
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
|
|
35
|
+
# imbot-sdk-python
|
|
36
|
+
|
|
37
|
+
[](https://pypi.org/project/imbot-sdk-python/)
|
|
38
|
+
[](https://pypi.org/project/imbot-sdk-python/)
|
|
39
|
+
[](https://github.com/juggleim/imbot-sdk-python/blob/main/LICENSE)
|
|
40
|
+
|
|
41
|
+
`imbot-sdk-python` 是 JuggleIM 的 Python SDK,基于 WebSocket 与 IM 服务建立长连接,适合 Bot、服务端消息代理或需要主动收发消息的 Python 程序。覆盖连接管理、消息收发、会话查询、历史消息、聊天室、用户状态、RTC 房间和文件凭证查询等常用能力。
|
|
42
|
+
|
|
43
|
+
- 源码:<https://github.com/juggleim/imbot-sdk-python>
|
|
44
|
+
- 包名:`imbot-sdk-python`;导入名:`import imbot_sdk`
|
|
45
|
+
|
|
46
|
+
## 安装
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install imbot-sdk-python
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
依赖(`protobuf>=5.0`、`websocket-client>=1.6`)会自动安装。Python 3.8+。
|
|
53
|
+
|
|
54
|
+
从源码安装:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
git clone https://github.com/juggleim/imbot-sdk-python.git
|
|
58
|
+
cd imbot-sdk-python
|
|
59
|
+
pip install .
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## 测试环境
|
|
63
|
+
|
|
64
|
+
| 项目 | 值 |
|
|
65
|
+
| --- | --- |
|
|
66
|
+
| Server API | `http://127.0.0.1` |
|
|
67
|
+
| WebSocket | `wss://127.0.0.1` |
|
|
68
|
+
| AppKey | `AppKey` |
|
|
69
|
+
|
|
70
|
+
> Token 由你的 JuggleIM 应用服务端 / 控制台用 AppKey + AppSecret 通过 Server API 颁发。SDK 本身只负责建立长连接与收发消息。
|
|
71
|
+
|
|
72
|
+
## 能力概览
|
|
73
|
+
|
|
74
|
+
- 连接管理:`connect`、`disconnect`、`logout`、心跳保活、断线自动重连
|
|
75
|
+
- 消息收发:单聊、群聊、聊天室、公共频道消息发送与接收
|
|
76
|
+
- 消息管理:历史消息查询、撤回、修改、已读标记、搜索、置顶消息
|
|
77
|
+
- 会话管理:会话列表、未读数、置顶、免打扰、标签
|
|
78
|
+
- 用户与群信息:用户资料、好友资料、群信息、在线状态订阅
|
|
79
|
+
- 聊天室:加入/退出聊天室、聊天室消息、聊天室属性同步
|
|
80
|
+
- RTC:创建/加入/查询/退出 RTC 房间
|
|
81
|
+
- 文件:文件凭证查询 `get_file_cred`
|
|
82
|
+
|
|
83
|
+
## 快速说明
|
|
84
|
+
|
|
85
|
+
### 1. 创建客户端
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from imbot_sdk import ImBotClient
|
|
89
|
+
|
|
90
|
+
client = ImBotClient("wss://127.0.0.1", "your-appkey")
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
说明:
|
|
94
|
+
|
|
95
|
+
- `address` 传基础地址即可,SDK 会自动拼接为 `ws://host/imbot` 或 `wss://host/imbot`
|
|
96
|
+
- `client.platform` 默认是 `"Bot"`
|
|
97
|
+
- `client.auto_reconnect` 默认是 `True`
|
|
98
|
+
|
|
99
|
+
### 2. 建立连接
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
code, ack = client.connect("your-token")
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
说明:
|
|
106
|
+
|
|
107
|
+
- 连接成功时 `code == ClientErrorCode.SUCCESS`
|
|
108
|
+
- 成功后 `ack.userId` 会写入 `client.user_id`
|
|
109
|
+
- `connect` 只能在断开状态下调用;重复连接会返回 `ClientErrorCode.CONNECT_EXISTED`
|
|
110
|
+
- 主动调用 `disconnect()` 或 `logout()` 后,不会继续自动重连
|
|
111
|
+
|
|
112
|
+
### 3. 接收消息的两种方式
|
|
113
|
+
|
|
114
|
+
推荐优先使用高层监听(拿到的是已经解码后的 `Message`):
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from imbot_sdk import MessageListener, messages
|
|
118
|
+
|
|
119
|
+
class MyListener(MessageListener):
|
|
120
|
+
def on_message_receive(self, msg):
|
|
121
|
+
if isinstance(msg.msg_content, messages.TextMessage):
|
|
122
|
+
print("text:", msg.msg_content.content)
|
|
123
|
+
|
|
124
|
+
client.add_message_listener(MyListener())
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
如果你想拿到底层 protobuf 数据:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
client.on_message_callback = lambda down_msg: print(down_msg)
|
|
131
|
+
client.on_stream_msg_callback = lambda stream_msg: print(stream_msg)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 4. 发送消息
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from imbot_sdk import Conversation, messages
|
|
138
|
+
from imbot_sdk.pb import appmessages_pb2 as pb
|
|
139
|
+
|
|
140
|
+
text = messages.TextMessage("hello from imbot-sdk-python")
|
|
141
|
+
up = client.build_up_msg(text, client_uid="bot-1")
|
|
142
|
+
|
|
143
|
+
conv = Conversation(conversation_type=pb.Private, conversation="target-user-id")
|
|
144
|
+
code, ack = client.send_message(conv, up)
|
|
145
|
+
# ack.msgId / ack.msgSeqNo 为服务端确认结果
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`build_up_msg` 会把内容模型编码进 `UpMsg.msgType / msgContent / flags`;你也可以直接构造 `pb.UpMsg`。
|
|
149
|
+
|
|
150
|
+
## 完整示例:连接、监听、回声
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
import time
|
|
154
|
+
from imbot_sdk import ImBotClient, Conversation, MessageListener, ClientErrorCode, messages
|
|
155
|
+
from imbot_sdk.pb import appmessages_pb2 as pb
|
|
156
|
+
|
|
157
|
+
class Echo(MessageListener):
|
|
158
|
+
def __init__(self, client):
|
|
159
|
+
self.client = client
|
|
160
|
+
def on_message_receive(self, msg):
|
|
161
|
+
c = msg.msg_content
|
|
162
|
+
if isinstance(c, messages.TextMessage) and msg.sender_id != self.client.user_id:
|
|
163
|
+
up = self.client.build_up_msg(messages.TextMessage("echo: " + c.content),
|
|
164
|
+
client_uid="echo-%d" % time.time_ns())
|
|
165
|
+
self.client.send_message(msg.conversation, up)
|
|
166
|
+
|
|
167
|
+
client = ImBotClient("wss://127.0.0.1", "AppKey")
|
|
168
|
+
client.add_message_listener(Echo(client))
|
|
169
|
+
|
|
170
|
+
code, ack = client.connect("your-token")
|
|
171
|
+
assert code == ClientErrorCode.SUCCESS
|
|
172
|
+
print("connected:", ack.userId)
|
|
173
|
+
|
|
174
|
+
# 主动给某用户发一条单聊
|
|
175
|
+
up = client.build_up_msg(messages.TextMessage("hi"), client_uid="greet-1")
|
|
176
|
+
client.send_message(Conversation(conversation_type=pb.Private, conversation="target-user-id"), up)
|
|
177
|
+
|
|
178
|
+
try:
|
|
179
|
+
while True:
|
|
180
|
+
time.sleep(1)
|
|
181
|
+
except KeyboardInterrupt:
|
|
182
|
+
client.disconnect()
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
可运行的版本见 [`examples/echo_bot.py`](examples/echo_bot.py)。推荐用 `.env` + 启动脚本:
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
cp examples/.env.example examples/.env
|
|
189
|
+
# 编辑 examples/.env,至少填入 IMBOT_TOKEN(可选 IMBOT_TARGET)
|
|
190
|
+
bash examples/run.sh
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
`run.sh` 会自动加载 `examples/.env` 并启动 echo bot,多余参数会透传给脚本(如
|
|
194
|
+
`bash examples/run.sh --target some-user-id`)。也可以不用脚本,直接传参或导出环境变量:
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
python examples/echo_bot.py --token <your-token> --target <target-user-id>
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
配置项(环境变量 / `.env`):
|
|
201
|
+
|
|
202
|
+
| 变量 | 说明 | 必填 |
|
|
203
|
+
| --- | --- | --- |
|
|
204
|
+
| `IMBOT_TOKEN` | 用户 Token | 是 |
|
|
205
|
+
| `IMBOT_TARGET` | 启动时打招呼的目标用户 ID | 否 |
|
|
206
|
+
| `IMBOT_ADDRESS` | WebSocket 基础地址 | 否 |
|
|
207
|
+
| `IMBOT_APPKEY` | 应用 AppKey | 否 |
|
|
208
|
+
|
|
209
|
+
补充说明:
|
|
210
|
+
|
|
211
|
+
- 单聊发送使用 `pb.Private`,群聊使用 `pb.Group`,聊天室使用 `pb.Chatroom`,公共频道使用 `pb.PublicChannel`
|
|
212
|
+
- 聊天室消息也可以直接 `client.send_chatroom_msg(chatroom_id, up_msg)`
|
|
213
|
+
- 接收到的 `msg.msg_content` 已按消息类型解码,可直接 `isinstance` 判断
|
|
214
|
+
|
|
215
|
+
## 消息类型
|
|
216
|
+
|
|
217
|
+
内置的消息内容模型位于 `imbot_sdk.messages`:
|
|
218
|
+
|
|
219
|
+
| 类型 | 标识 | 类 |
|
|
220
|
+
| --- | --- | --- |
|
|
221
|
+
| 文本 | `jg:text` | `TextMessage` |
|
|
222
|
+
| 图片 | `jg:img` | `ImageMessage` |
|
|
223
|
+
| 文件 | `jg:file` | `FileMessage` |
|
|
224
|
+
| 视频 | `jg:video` | `VideoMessage` |
|
|
225
|
+
| 语音 | `jg:voice` | `VoiceMessage` |
|
|
226
|
+
| 流式文本 | `jg:streamtext` | `StreamTextMessage` |
|
|
227
|
+
| 撤回通知 | `jg:recallinfo` | `RecallInfoMessage` |
|
|
228
|
+
| 合并消息 | `jg:merge` | `MergeMessage` |
|
|
229
|
+
| 缩略图打包图片 | `jg:tpimg` | `ThumbnailPackedImageMessage` |
|
|
230
|
+
| 快照打包视频 | `jg:spvideo` | `SnapshotPackedVideoMessage` |
|
|
231
|
+
|
|
232
|
+
收到未内置支持的消息类型时,SDK 会回落为 `messages.UnknownMessage`。这些模型的 JSON 编解码字段保持稳定,因此消息可与其它 JuggleIM 客户端互通。
|
|
233
|
+
|
|
234
|
+
## 常用 API
|
|
235
|
+
|
|
236
|
+
> 完整方法清单见 [`docs/API.md`](docs/API.md)。命名采用 Python 风格(snake_case)。
|
|
237
|
+
|
|
238
|
+
### 连接与状态
|
|
239
|
+
|
|
240
|
+
`connect(token)`、`reconnect()`、`disconnect()`、`logout()`、`ping()`、`add_connection_status_change_listener(listener)`
|
|
241
|
+
|
|
242
|
+
### 消息
|
|
243
|
+
|
|
244
|
+
`send_message(conversation, up_msg)`、`qry_history_msgs(req)`、`recall_msg(req)`、`modify_msg(req)`、`mark_read_msg(req)`、`msg_search(req)`、`msg_global_search(req)`、`set_top_msg(req)`、`del_top_msg(req)`
|
|
245
|
+
|
|
246
|
+
### 会话
|
|
247
|
+
|
|
248
|
+
`get_conversation(req)`、`get_conversations(req)`、`sync_conversations(req)`、`clear_unread_count(req)`、`set_conversation_top(req)`、`set_mute(req)`、`delete_conversations(req)`
|
|
249
|
+
|
|
250
|
+
### 用户、群组、状态
|
|
251
|
+
|
|
252
|
+
`fetch_user_info(user_id)`、`fetch_group_info(group_id)`、`fetch_friend_info(friend_user_id)`、`get_user_status(req)`、`subscribe_user_status(req)`、`unsubscribe_user_status(req)`
|
|
253
|
+
|
|
254
|
+
### 聊天室
|
|
255
|
+
|
|
256
|
+
`join_chatroom(chatroom_id)`、`quit_chatroom(chatroom_id)`、`send_chatroom_msg(chatroom_id, up_msg)`、`set_attributes(chatroom_id, attributes)`、`remove_attributes(chatroom_id, keys)`
|
|
257
|
+
|
|
258
|
+
### RTC
|
|
259
|
+
|
|
260
|
+
`create_rtc_room(req)`、`join_rtc_room(req)`、`qry_rtc_room(room_id)`、`quit_rtc_room(room_id)`、`rtc_invite(req)`
|
|
261
|
+
|
|
262
|
+
### 文件
|
|
263
|
+
|
|
264
|
+
`get_file_cred(req)`
|
|
265
|
+
|
|
266
|
+
> 当前 SDK 提供文件凭证查询能力,文件上传/下载流程需由业务侧结合存储服务自行处理。
|
|
267
|
+
|
|
268
|
+
## 使用注意事项
|
|
269
|
+
|
|
270
|
+
- `publish` 和 `query` 都要求当前连接状态为 `CONNECTED`,否则返回 `ClientErrorCode.CONNECT_CLOSED`
|
|
271
|
+
- 发送和查询默认等待 10 秒 ACK,超时分别返回 `ClientErrorCode.SEND_TIMEOUT`、`ClientErrorCode.QUERY_TIMEOUT`
|
|
272
|
+
- SDK 内部会自动处理心跳;连续超过两个心跳周期(约 20 秒)未收到下行数据,会断开并尝试自动重连
|
|
273
|
+
- 消息监听回调运行在内部线程上,请避免长时间阻塞操作,必要时投递到工作队列
|
|
274
|
+
- 图片、文件、语音、视频等消息体只负责内容编解码,不负责上传文件本身
|
|
275
|
+
|
|
276
|
+
## 重新生成 protobuf
|
|
277
|
+
|
|
278
|
+
`.proto` 源文件位于 [`proto/`](proto/)。修改后重新生成:
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
bash proto/gen.sh
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## 模块结构
|
|
285
|
+
|
|
286
|
+
| 模块 | 说明 |
|
|
287
|
+
| --- | --- |
|
|
288
|
+
| `imbot_sdk.ImBotClient` | 客户端主入口 |
|
|
289
|
+
| `imbot_sdk.ClientErrorCode` / `ConnectState` | 错误码与连接状态 |
|
|
290
|
+
| `imbot_sdk.models` | 领域模型(`Conversation` / `Message` / `UserInfo` …)|
|
|
291
|
+
| `imbot_sdk.messages` | 消息内容模型 |
|
|
292
|
+
| `imbot_sdk.pb` | protobuf 类型(`appmessages_pb2` / `connect_pb2` / `chatroom_pb2` / `rtcroom_pb2`)|
|
|
293
|
+
|
|
294
|
+
## 许可证
|
|
295
|
+
|
|
296
|
+
[LICENSE](LICENSE)
|