最新版Nonebot2合并发送消息


所谓合并发送消息就是将多个类型的消息组合到一块发送出去,适用于群管理机器人,例如入群欢迎、退群欢送等等。其中消息的格式可以参考go-cqhttp的CQ码 传输门。今天的两个插件是通过Nonebot2调用go-cqhttp的api实现消息的组合发送。
例:发送qq表情

{
    "type": "face",
    "data": {
        "id": "123"
    }
}

前置知识

首先说一下Nonebot2调用go-cqhttp api的函数函数名称为call_api(api名称,参数…),该方法位于机器人身上,引入方式如像这样:from nonebot.adapters.onebot.v11 import GroupDecreaseNoticeEvent,Bot
引入适配器中的Bot并调用的方式像这样:

#声明
async def handle_first_receive(bot: Bot, event: GroupDecreaseNoticeEvent, state: T_State):
#调用
await bot.call_api("send_group_msg", group_id=event.group_id, message=rely)

还有许多函数在Nonebot2官网上传送门。可以根据自己的需要挑选。除了发送消息这个api,当然还有许多其他优秀的api,可以直接去go-cqhttp官网查看传送门,例如咱们今天用到的api send_group_msg,我们只需研究其参数作用及参数类型,然后在Nonebot2机器人中调用即可。

实战操作

合并消息

这个是合并之后的消息,实际上是一个消息列表,每一个对象就是一条消息,在发送的时候,将所有消息统一进行发送。type是消息类型,data是必有项,代表消息的消息体,开头提到过,这个格式可以参考go-cqhttp的CQ码。就不过多赘述了。

rely = [
    {
        "type": "text",
        "data": {
            "text": "欢迎"
        }
    },
    {
        "type": "at",
        "data": {
            "qq": f"{event.user_id}"
        }
    },
    {
        "type": "text",
        "data": {
            "text": "进群"
        }
    },
    {
        "type": "image",
        "data": {
            "file": f"https://q4.qlogo.cn/headimg_dl?dst_uin={event.user_id}&spec=640"
        }
    },
    {
        "type": "text",
        "data": {
            "text": "欢迎新人!大家努力学习呀!"
        }
    },
    {
        "type": "face",
        "data": {
            "id": "13"
        }
    }
]

入群欢迎插件

这个插件可以检测到群中人数增加,并发送欢迎语句。效果如下图:
在这里插入图片描述

from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupIncreaseNoticeEvent, Bot
from nonebot import on_notice, on_command
GroupIncrease = on_notice()

# 检测群成员增加
@GroupIncrease.handle()
async def handle_first_receive(bot: Bot, event: GroupIncreaseNoticeEvent, state: T_State):
    rely = [
        {
            "type": "text",
            "data": {
                "text": "欢迎"
            }
        },
        {
            "type": "at",
            "data": {
                "qq": f"{event.user_id}"
            }
        },
        {
            "type": "text",
            "data": {
                "text": "进群"
            }
        },
        {
            "type": "image",
            "data": {
                "file": f"https://q4.qlogo.cn/headimg_dl?dst_uin={event.user_id}&spec=640"
            }
        },
        {
            "type": "text",
            "data": {
                "text": "欢迎新人!大家努力学习呀!"
            }
        },
        {
            "type": "face",
            "data": {
                "id": "13"
            }
        }
    ]
    await bot.call_api("send_group_msg", group_id=event.group_id, message=rely)

退群欢送插件

这个插件可以检测到群中人数减少,并发送欢送语句。效果如下图:
在这里插入图片描述

from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupDecreaseNoticeEvent,Bot
from nonebot import on_notice, on_command
import warnings,requests
# from nonebot.permission import *
warnings.filterwarnings("ignore")
# 检测离开群成员
group_decrease = on_notice()


@group_decrease.handle()
async def handle_first_receive(bot: Bot, event: GroupDecreaseNoticeEvent, state: T_State):
    rely = [
        {
            "type": "text",
            "data": {
                "text": "这位小可爱"
            }
        },
        {
            "type": "at",
            "data": {
                "qq": f"{event.user_id}"
            }
        },
        {
            "type": "text",
            "data": {
                "text": "退群咯啦!"
            }
        },
        {
            "type": "image",
            "data": {
                "file": f"https://q4.qlogo.cn/headimg_dl?dst_uin={event.user_id}&spec=640"
            }
        },
        {
            "type": "text",
            "data": {
                "text": "一路走好,小心脚滑!"
            }
        },
        {
            "type": "face",
            "data": {
                "id": "13"
            }
        }
    ]
    await bot.call_api("send_group_msg", group_id=event.group_id, message=rely)
    # await bot.send(event=event,message=rely)

至此分享结束!
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酷尔。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值