Giter Site home page Giter Site logo

pylibob's Introduction

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

pylibob's People

Contributors

mingxuangame avatar

Stargazers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.