Skip to main content

A customizable, adapter-independent, event-based scheduled broadcaster aiming for all nonebot2 adapters.

Project description

Nonebot Plugin Scheduled Broadcast

✨ 一款可配置的, 不依赖具体适配器的, 基于事件的定时广播插件. ✨

特性

  • 基于一般事件 nonebot.adapters.Event 构造, 从而支持全部协议的信息发送
  • 定时广播由文件配置, 基于 apscheduler 提供类 crontab 的配置参数

安装

使用 nb-cli 安装

  • nonebot2 项目的根目录下打开命令行, 输入以下指令即可安装:
    nb-cli plugin install nonebot-plugin-scheduled-broadcast

使用包管理器安装

  • 使用 pdm 安装:
    pdm add nonebot-plugin-scheduled-broadcast
  • 使用 poetry 安装:
    poetry add nonebot-plugin-scheduled-broadcast
  • 使用 pip 安装: (虽然不太推荐但大概率没事)
    pip install nonebot-plugin-scheduled-broadcast

使用

启动广播

  • 保证发送消息的 id 位于 SUPERUSER 用户组中
  • 在需要启动广播的地方发送:
    启动广播/enablebc 广播ID
  • 如果是第一次使用, 机器人在执行上述命令后, 在 nonebot2 项目的根目录下生成一个名为 broadcast_policy.json 的配置文件
  • 如果该机器人下已经存在相同的 广播ID, 配置文件将会保留, 同时广播作业将被 恢复, 如果不需要修改 config 中的内容, 不需要重启

配置文件的填写

  • 一般而言, 配置文件的结构如下
{
    "self_id": {
        "broadcast_id": {
            "config": {
                "example": {"second": "*/30"}
            },
            "data": "event data (b64encode)",
            "hash": "event hash (sha256)",
            "enable": true
        },
        "another broadcast_id": {
            // ...
        }
    },
    "another self_id": {
        // ...
    }
}
  • config 的可用配置与 apscheduler 一致, 以下给出一个示例, 该示例表示, example 命令会在每天 10:24, 每隔 10 秒被触发:
{
    "config": {
        "example": {
            "hour": 10,
            "minute": 24,
            "second": "*/10"
        }
    }
}
  • 在准备好了配置文件后, 就可以准备编写待触发的指令了.

待触发指令的编写

  • 由于本插件提供了一个装饰器, 可以按照如下方式编写待触发指令:
from nonebot import require
from nonebot.adapters import Event
from nonebot.log import logger

require("nonebot_plugin_scheduled_broadcast")

from nonebot_plugin_scheduled_broadcast.core import broadcast

@broadcast('example')
async def _(self_id: str, event: Event):
    """Scheduled example broadcast."""
    message = generate_your_message()
    try:
        bot = nonebot.get_bots()[self_id]  # select the target bot
        await bot.send(event=event, message=message)  # send message
    except Exception:
        logger.error(traceback.format_exc())  # print logs
  • 触发指令编写完成以后, 重新启动机器人即可.

关闭广播

  • 保证发送消息的 id 位于 SUPERUSER 用户组中
  • 在需要关闭广播的地方发送:
    关闭广播/disablebc 广播ID
  • 停止广播之后, 对应 广播ID 中的 enable 将被置为 false, 为保持可复用性, 其余部分将不会改变, 同时广播作业将被 暂停, 如果不需要修改 config 中的内容, 不需要重启

配置

  • broadcast_policy_location: 代表配置文件的存放位置, 默认值为 ./broadcast_policy.json

注意事项

  • 由于每一个 Event 几乎不会相同, 建议不要在同一个地方多次执行 启动广播 命令, 可能会刷屏的
  • 由于用到了 pickle 的序列化和反序列化功能, 而该功能具有潜在的安全风险, 请谨慎对待来源不明的配置文件. 如果不确定配置文件的安全性, 建议重新执行 广播ID, 然后替换配置文件中的 config 键里面的内容.
  • 由于配置文件的生成限制, 当修改了 config 中的内容时, 必须需要重启机器人使得新的配置生效, 其它情况下均不需要重启机器人.

协议

  • 本项目使用 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

nonebot_plugin_scheduled_broadcast-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file nonebot_plugin_scheduled_broadcast-0.1.0.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_scheduled_broadcast-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3b6c6e12fb0fb585edc565df6a10a31b826ab0b1a78f16d947e37142b11e210a
MD5 10d3203905df1f864181699a75f30fe9
BLAKE2b-256 7c093268e456fd75b1490f0b0dd8dfdccb8e2026ea69005f687203c7724bcc15

See more details on using hashes here.

File details

Details for the file nonebot_plugin_scheduled_broadcast-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_scheduled_broadcast-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5010c00aecd54ee124926a2ec883527fd1a71258747c78be5730369535a7cd11
MD5 379ea6c173efbc950048c9e230793860
BLAKE2b-256 e8ebdaef784c62004edcfb26b297ad69abeafbf4090cb0bf590386d40386782f

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