Skip to main content

An adaptor for nonebot2 adaptors

Project description

logo征集中,假装有图片

Nonebot Plugin
Send Anything Anywhere

你只管业务实现,把发送交给我们

GitHub Workflow Status Codecov PyPI - Python Version PyPI GitHub

这个插件可以做什么

  • 为常见的消息类型提供抽象类,自适应转换成对应 adapter 的消息
  • 提供一套统一的,符合直觉的发送接口(规划中)
  • 为复杂的消息提供易用的生成接口(规划中)

本插件通过传入 bot 的类型来自适应生成对应 bot adapter 所使用的 Message

安装

  • 使用 nb-cli 安装
    nb plugin install nonebot-plugin-send-anything-anywhere
  • 使用 poetry 安装
    poetry add nonebot-plugin-send-anything-anywhere
  • 使用 pip 安装
    pip install nonebot-plugin-send-anything-anywhere

支持的 adapter

  • OneBot v11
  • OneBot v12
  • QQ Guild

支持的消息类型

  • 文字(全平台)
  • 图片(全平台)
  • at(全平台)
  • 回复(除QQ频道)

问题与例子

因为在现在的 Nonebot 插件开发中,消息的构建和发送是和 adapter 高度耦合的,这导致一个插件要适配不同的 adapter 是困难的

before:

from nonebot.adapters.onebot.v11.event import MessageEvent as V11MessageEvent
from nonebot.adapters.onebot.v11.message import MessageSegment as V11MessageSegment
from nonebot.adapters.onebot.v12.event import MessageEvent as V12MessageEvent
from nonebot.adapters.onebot.v12.message import MessageSegment as V12MessageSegment
from nonebot.adapters.onebot.v12.bot import Bot as V12Bot

pic_matcher = nonebot.on_command('发送图片')

pic_matcher.handle()
async def _handle_v11(event: V11MessageEvent):
    pic_content = ...
    msg = V11MessageSegment.image(pic_content) + V11MessageSegment.text("这是你要的图片")
    await pic_matcher.finish(msg)

pic_matcher.handle()
async def _handle_v12(bot: V12Bot, event: V12MessageEvent):
    pic_content = ...
    pic_file = await bot.upload_file(type='data', name='image', data=pic_content)
    msg = V12MessageSegment.image(pic_file['file_id']) + V12MessageSegment.text("这是你要的图片")
    await pic_matcher.finish(msg)

现在只需要:

from nonebot.adapters.onebot.v11.event import MessageEvent as V11MessageEvent
from nonebot.adapters.onebot.v12.event import MessageEvent as V12MessageEvent
from nonebot.internal.adapter.bot import Bot
from nonebot_plugin_saa import Image, Text, MessageFactory

pic_matcher = nonebot.on_command('发送图片')

pic_matcher.handle()
async def _handle_v12(bot: Bot, event: Union[V12MessageEvent, V11MessageEvent]):
    pic_content = ...
    msg_builder = MessageFactory([
        Image(pic_content), Text("这是你要的图片")
    ])
    # or msg_builder = Image(pic_content) + Text("这是你要的图片")
    msg = await msg_builder.build(bot)
    await pic_matcher.finish(msg)

类似项目

License

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

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_send_anything_anywhere-0.1.1.tar.gz.

File metadata

File hashes

Hashes for nonebot_plugin_send_anything_anywhere-0.1.1.tar.gz
Algorithm Hash digest
SHA256 683eca767b8ad6254143677da6815af83257ced064ea1655a1fcd423c2ad9a05
MD5 c6e61d0359bcbf2b41f533c7911ba6b5
BLAKE2b-256 72abfc7f34965a9b749380757f3c0ba24417a57a4c5c21e13ef45b58d6bc4447

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nonebot_plugin_send_anything_anywhere-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 353803527c29237a460a1966a9419f051af9db6cf603c9a8e1d0b9dc158f6e7c
MD5 40cabf251679b110255afaae7f73474a
BLAKE2b-256 17c20fe32e94ba30fd0cba0d77a1f0b41af75e5765174e69d5ea0cbe9b3ee4d3

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