connection
本模块实现了 OneBot Connect 的 HTTP 和 HTTP Webhook 部分 以及连接基类的定义。
ClientConnection
Bases: Connection
客户端连接(HTTP Webhook/反向 WebSocket)基类。
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
url |
str
|
OneBot 应用的连接目标地址 |
ua |
str
|
连接时使用的 User-Agent |
Source code in src/pylibob/connection.py
Connection
连接基类。
init_connection 方法为初始化连接的方法,用于在 impl 确定后继续初始化。
顺序如下:
graph LR
A[__init__] --> B[确定 impl] --> C[init_connection]
run_action 用于以原始数据运行动作响应器。
emit_event 用于向应用端推送事件,需各连接自行实现。
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
impl |
OneBotImpl
|
OneBot 实现实例 |
Source code in src/pylibob/connection.py
impl: 'OneBotImpl'
property
OneBot 实现实例。`
__init__(*, access_token=None)
emit_event(event)
async
init_connection()
run_action(data)
async
以原始数据运行动作响应器。
- 未传入
action或params时返回 10001 Bad Request。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
dict[str, Any]
|
原始数据 |
required |
Source code in src/pylibob/connection.py
HTTP
Bases: ServerConnection
当启用 get_latest_events 元动作时,连接会创建一个大小为 event_buffer_size 的事件队列 event_queue。
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
host |
str
|
HTTP 服务器监听 IP |
port |
int
|
HTTP 服务器监听端口 |
event_queue |
Queue[Event] | None
|
事件队列 |
Source code in src/pylibob/connection.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
__init__(*, access_token=None, host='0.0.0.0', port=8080, event_enabled=True, event_buffer_size=20)
初始化 HTTP 连接。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
str | None
|
访问令牌 |
None
|
host |
str
|
HTTP 服务器监听 IP |
'0.0.0.0'
|
port |
int
|
HTTP 服务器监听端口 |
8080
|
event_enabled |
bool
|
是否启用 |
True
|
event_buffer_size |
int
|
事件缓冲区大小 |
20
|
Source code in src/pylibob/connection.py
action_get_latest_events(limit=0, timeout=0)
async
Source code in src/pylibob/connection.py
HTTPWebhook
Bases: ClientConnection
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
url |
str
|
Webhook 上报地址 |
timeout |
int
|
上报请求超时时间,单位: 毫秒,0 表示不超时 |
Source code in src/pylibob/connection.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
__init__(url, *, access_token=None, timeout=5000)
初始化 HTTP Webhook 连接。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
access_token |
str | None
|
访问令牌 |
None
|
url |
str
|
Webhook 上报地址 |
required |
timeout |
int
|
上报请求超时时间,单位: 毫秒,0 表示不超时 |
5000
|
Source code in src/pylibob/connection.py
ServerConnection
Bases: Connection
服务器连接(HTTP/正向 WebSocket)基类。
服务器连接会由 ServerRunner 运行(uvicorn)。
Attributes:
| Name | Type | Description |
|---|---|---|
access_token |
str | None
|
访问令牌 |
host |
str
|
服务器监听 IP |
port |
int
|
服务器监听端口 |