Skip to main content

另一个 LibOneBot Python 库,旨在帮助开发者实现 OneBot 12 标准。

Project description

pylibob

另一个 LibOneBot Python 库,旨在帮助开发者实现 OneBot 12 标准。

这是什么?

这是一个 Python 的 LibOneBot,旨在帮助开发者快速实现 OneBot 12 标准。

LibOneBot 对 OneBot Connect 和动作、事件、消息段进行了包装,便于开发者使用。

下文涉及的 OneBot 概念请参考 OneBot 术语表

安装

pip install pylibob

快速上手

from __future__ import annotations

# from typing_extensions import Annotated  # python<3.9
from typing import Annotated  # python>=3.9

from pylibob import (
    HTTP,
    Bot,
    Event,
    HTTPWebhook,
    OneBotImpl,
    WebSocket,
    WebSocketReverse,
)

impl = OneBotImpl(
    "test",  # 实现名称
    "1.0.0",  # 实现版本
    [
        HTTP(
            host="0.0.0.0",  # HTTP 服务器监听 IP
            port=8080,  # HTTP 服务器监听端口
            event_enabled=True,  # 是否启用 get_latest_events 元动作
            event_buffer_size=20,  # 事件缓冲区大小
            access_token="access_token",  # 访问令牌
        ),
        HTTPWebhook(
            url="http://127.0.0.1:8080/onebot/v12/http/",  # Webhook 上报地址
        ),
        # WebSocket 均存在 enable_heartbeat 和 heartbeat_interval
        WebSocket(
            enable_heartbeat=True,  # 启用心跳
            heartbeat_interval=5000,  # 心跳间隔
        ),
        WebSocketReverse(url="ws://127.0.0.1:8081/onebot/v12/ws/"),
    ],
    Bot(platform="qq", user_id="1", online=True),  # 任意个数 Bot 实例
)


@impl.action("hello")
async def _(
    # 采用类型注解的方式声明参数及类型
    a: str,
    # 扩展参数使用 Annotated,第一个 metadata 会被视为参数名
    b: Annotated[int, "extra.param"],
    # 注解为 Bot 的参数不计入动作需要的参数,会内部处理传入 Bot 实例
    c: Bot,
    # 允许默认值
    d: int = 5,
):
    # 此动作 `hello` 需要必须参数:
    #     a (string)
    #     extra.param (int)
    # 可选参数:
    #     d (int) (default = 5)

    # 向应用推送事件
    await impl.emit(Event(...))

    return a, b, c, d  # 返回的内容会传入到响应的 data

# 机器人准备好后(一般指机器人登录完成)
# 实现良好状态默认为 False,准备好需手动调整为 True
# impl.is_good = True
# 还要调用此方法去更新状态
# await impl.update_status()

impl.run()  # 运行

许可证

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylibob-0.1.1.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylibob-0.1.1-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file pylibob-0.1.1.tar.gz.

File metadata

  • Download URL: pylibob-0.1.1.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.9.3 CPython/3.10.12

File hashes

Hashes for pylibob-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5695ed256f3a89356adc9dc6fc4cab63f8506bb9151008a1a302f196fae04fd9
MD5 d7edec5b3721cc3df84a68de2de28f53
BLAKE2b-256 9e7326219d80fd44e17d5b3823a6bf10f2bba2879d6d5baf131d3f172bc96951

See more details on using hashes here.

File details

Details for the file pylibob-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pylibob-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.9.3 CPython/3.10.12

File hashes

Hashes for pylibob-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2113c3087fa4074af105b834bc9e639bd3c931b9dbe4086c9739c9c823fde2b
MD5 2a23bbc34e55cba2a9ecda7aefeb38a1
BLAKE2b-256 26809970922debdce5beef6481657864c2762d58c03ae7bcc4ad42ec16c85c7f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page