An adaptor for nonebot2 adaptors
Project description
logo征集中,假装有图片
Nonebot Plugin
Send Anything Anywhere
你只管业务实现,把发送交给我们
这个插件可以做什么
- 为常见的消息类型提供抽象类,自适应转换成对应 adapter 的消息
- 提供一套统一的,符合直觉的发送接口(规划中)
- 为复杂的消息提供易用的生成接口(规划中)
本插件通过传入 bot 的类型来自适应生成对应 bot adapter 所使用的 Message
安装
TODO
支持的 adapter
- OneBot v11
- OneBot v12
支持的消息类型
- 文字(全平台)
- 图片(全平台)
- at(全平台)
- 回复(全平台)
问题与例子
因为在现在的 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)
类似项目
- nonebot-plugin-all4one 解决了类似的问题,但是用了不同路径
- nonebot-plugin-params 通过 Rule 定制订阅的平台,与本插件联合使用也许会有奇效
License
TODO
Project details
Release history Release notifications | RSS feed
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
Close
Hashes for nonebot_plugin_send_anything_anywhere-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83757e02e7ef1ad4eb0696017b855513062ba1831a08a2b9bd2f56c9a6d81fb5 |
|
MD5 | 0c6d559112c8b31257828913399b5901 |
|
BLAKE2b-256 | 0a8103f2610ed57a1a7ccd85135e2b4acf60be9dd2cef70ff152ce0eddb0f253 |
Close
Hashes for nonebot_plugin_send_anything_anywhere-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73a972134594f81e4b60575bbd2edd4aec77824a6b2d5a8479d7d7a762d5077c |
|
MD5 | df450a43c3204d94cb58dccc8f8d0c50 |
|
BLAKE2b-256 | ed9d76fc4d50ad59a2193692717d26c7cae33e008f3319be83b6bb32841e6b5e |