Skip to main content

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

使用

在 handler 中回复消息的情况:

@matcher.handle()
async def handle(event: MessageEvent):
    # 直接调用 MessageFactory.send() 在 handler 中回复消息
    await MessageFactory("你好").send(reply=True, at_sender=True)
    await MessageFactory("需要回复的内容").send()
    await matcher.finish()

主动发送的情况:

from nonebot_plugin_saa import TargetQQGroup

# 发送目标为群号 114514 的群聊
target = TargetQQGroup(group_id=114514)
await MessageFactory("早上好").send_to(target)

从消息事件中提取发送目标:

from nonebot_plugin_saa import extract_target, get_target, SaaTarget

@matcher.handle()
async def handle(event: MessageEvent, bot: Bot):
    # 只有混入了 Specifier 的 PlatformTarget(例如 OpenID 版 QQ)需要传入 bot
    target = extract_target(event, bot)

@matcher.handle()
async def handle(target: SaaTarget):
    ...

发送目标的序列化与反序列化:

from nonebot_plugin_saa import PlatformTarget, TargetQQPrivate

target = TargetQQPrivate(user_id=112233)
serialized_target = target.json()
deserialized_target = PlatformTarget.deserialize(serialized_target)
assert deserialized_target == target

支持情况

✅:支持 ✖️:支持不了 🚧:等待适配

支持的 adapter

OneBot v11 OneBot v12 Kaiheila Telegram Feishu Red DoDo Satori QQ 官方 Discord
✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅

支持的消息类型

OneBot v11 OneBot v12 开黑啦 Telegram Feishu Red DoDo Satori QQ 官方 Discord
文字 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
图片 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
提及 ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅ ✅
提及全体 ✅ ✅ ✅ 🚧 ✅ ✅ 🚧 ✅ ✅ ✅
回复 ✅ ✅ ✅ ✅ ✅ 🚧 ✅ ✅ ✅ ✅

[!NOTE] 提及全体在 Telegram 中不存在,在 DoDo 中只能接收,不能发送

支持的发送目标

OneBot v11 OneBot v12 Kaiheila Telegram Feishu Red DoDo Satori QQ 官方 Discord
QQ 群 ✅ ✅ ✅ ✅
QQ 私聊 ✅ ✅ ✅ ✅
QQ 频道子频道消息 ✅ ✅
QQ 频道私聊 ✅ ✅
开黑啦私聊/频道 ✅ ✅
Telegram 普通对话/频道 ✅ ✅(仅普通对话)
飞书私聊/群聊 ✅ ✅
DoDo 私聊/群聊 ✅
QQ OpenId 版私聊/群聊 ✅
Discord 频道 ✅

[!NOTE]

  • 对于使用 Onebot v12,但是没有专门适配的发送目标,使用了 TargetOB12Unknow 来保证其可以正常使用
  • 对于使用 Satori,但是没有专门适配对应 Platform 的发送目标,使用了 TargetSatoriUnKnown 来保证其可以正常使用
  • QQ OpenId 版私聊/群聊指的是使用官方的 QQ Open Id 作为用户标识的私聊和群聊
  • Telegram 普通对话 所指为其私聊和群聊

问题与例子

因为在现在的 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("这是你要的图片")
    await msg_builder.send()
    await pic_matcher.finish()

类似项目

License

MIT

Metadata

Release files for nonebot-plugin-send-anything-anywhere 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nonebot-plugin-send-anything-anywhere 0.7.1
File Size Uploaded Provenance Actions
nonebot_plugin_send_anything_anywhere-0.7.1.tar.gz 34.3 kB Without provenance Details for nonebot_plugin_send_anything_anywhere-0.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nonebot-plugin-send-anything-anywhere 0.7.1
File Interpreter ABI Platform Actions
nonebot_plugin_send_anything_anywhere-0.7.1-py3-none-any.whl Python 3 none any Details for nonebot_plugin_send_anything_anywhere-0.7.1-py3-none-any.whl

Total release size: 81.7 kB

Release files / nonebot_plugin_send_anything_anywhere-0.7.1.tar.gz

Download URL nonebot_plugin_send_anything_anywhere-0.7.1.tar.gz
Size 34.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ec9c7ba59c824238b812950146a894acc88ca8da98f500de15217feebcd5e868
BLAKE2b-256 checksum
How to use checksums
6773821d35051fd98f7ccf499dec85c4b8a3e8d916aeef989371b8d793167abd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / nonebot_plugin_send_anything_anywhere-0.7.1-py3-none-any.whl

Download URL nonebot_plugin_send_anything_anywhere-0.7.1-py3-none-any.whl
Size 47.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b52044272be9a7bc77bd7a53ef700481c071fd4e889ae21a0411d0df22d13c16
BLAKE2b-256 checksum
How to use checksums
bdde39c3a1810c2b25239fe43ea68bced88a9ccadc693b132d5a42d6e25c9134
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.0 CPython/3.12.5

Security for nonebot-plugin-send-anything-anywhere 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page