GithubHelp home page GithubHelp logo

nonebot_plugin_broker's Introduction

nonebot_plugin_broker

本插件旨在提供简单易用的发布/订阅接口,以便迅速地向nonebot插件中加入推送功能,实现订阅的集中管理,避免重复代码

此外也是一个简单的、插件之间信息交互的发布/订阅模式实现

安装

可以使用pip安装

pip install nonebot_plugin_broker

如果使用脚手架nb-cli,也许可以:

nb plugin install nonebot_plugin_broker

请注意:本插件依赖nonebot官方的apscheduler插件和onebot适配器

简单使用

本插件提供三个对外函数,register/subscribe/remove 通过nonebot的require功能获得:

register = require("broker").register
subscribe = require("broker").subscribe
remove = require("broker").remove

发布

使用register函数即可注册一个服务,并获得一个发布函数

publish = register(title="test", hour=8)

其中title参数是该服务的标题,也是该服务的唯一标识符,使用时请避免重名

hour参数为该服务的推送时间,hour=8代表会在每天早上八点进行推送

hour参数可以省略,如果省略,则会使用.env文件中的broker_default_time变量,如果没有该变量,则默认为8,即早上八点。

这两个参数还可以在保持位置关系的条件下省略参数名,故上述语句可以简写为:

publish = register("test")

也是同样的功能

在注册完服务,得到publish函数后,便可在插件中向broker发布内容了:

def func(arg):
    do_something
    ...
    publish("a test info")
    ...
    do_something

如上,发布完成后,broker会在指定的时间向订阅者推送消息,发布者无需对订阅和推送进行管理

当然,publish不仅可以发布字符串消息,也支持发送数字(会被转换成字符串)和bytes类型的图片,或是包含上述内容的列表list,如果仍未满足需求,可以自行组装MessageSegment发送


订阅

对于想要订阅已注册服务的用户,可以向bot发送/订阅 <服务标题1> <服务标题2> ...进行订阅,指令和参数间、参数和参数间需要有空格分割,如果是一个群需要订阅,则需要群主/管理员 @bot 发送如上指令:@bot /订阅 <服务标题1> <服务标题2> ...

退订流程同上,将订阅换成退订即可

对于想要订阅已注册服务的开发者,可以使用subscribe函数:

def func(arg):
    do_something

subscribe(title="test", subscriber=func)

如上,即可使func成为test服务的订阅者,broker会将消息作为参数传入该函数并调用之

退订方法也同上,只需将subscribe换成remove

请注意:送入该函数通常是Message对象而非原始数据,您可能需要自行进行解析


获取服务清单

向bot发送/清单即可获得所有可见服务的清单


其他

插件同时还自带了每个小时的报时服务、每月第一天早上八点的报时服务 及每周一早上八点的报时服务,这些服务在被函数订阅时将不会送入Message对象:

服务 标题(title) 参数
x点报时服务 clock{x} {x}
月初报时服务 月初通知 first_day
周一报时服务 周一通知 mon

进阶使用

register函数细节

publish函数细节

subscribe与remove

管理员指令

配置参数

示例

简单的使用方法可以参考我的leetcode_everyday插件

TODO

以下为近期更新目标

  • 完善基础功能
  • 加入管理功能,支持管理员向bot发送信息进行管理 √已完成
  • 向bot请求服务清单的功能 √已完成
  • 支持mirai适配器
  • 含参数订阅功能(具体实现研究中)
  • 运行周期回调功能
  • 写文档

未来做什么

以下内容在考虑中

  • 支持更多适配器,在本插件上抹平适配器间的差异(主要是文字和图片)
  • 其他不知道什么鬼功能

如果您有什么想法也可以告诉我

CHANGELOG

2022/03/30

  • 加入获取清单功能
  • 加入管理员指令
  • 完善文档

LICENSE

MIT License

nonebot_plugin_broker's People

Contributors

mwbimh avatar

Stargazers

 avatar Cateon Huo avatar  avatar  avatar sansiny avatar MeguriM avatar Cyc1ops avatar

Watchers

 avatar AInoriex avatar

nonebot_plugin_broker's Issues

希望能在python3.9用

遇到的问题是这个:
ERROR: Ignored the following versions that require a different python version: 0.1.0 Requires-Python >=3.10,<4.0; 0.2.0 Requires-Python >=3.10,<4.0; 0.3.0 Requires-Python >=3.10,<4.0; 0.4.0 Requires-Python >=3.10,<4.0
ERROR: Could not find a version that satisfies the requirement nonebot-plugin-broker (from versions: none)
ERROR: No matching distribution found for nonebot-plugin-broker

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.