跨平台Python异步聊天机器人框架,支持QQ、飞书、钉钉等渠道

《开源精选》是我们分享Github、Gitee等开源社区中优质项目的栏目,包括技术、学习、实用与各种有趣的内容。本期推荐的是一个跨平台 Python 异步聊天机器人框架——nonebot2。

NoneBot2 是一个现代、跨平台、可扩展的 Python 聊天机器人框架,它基于 Python 的类型注解和异步特性,能够为你的需求实现提供便捷灵活的支持。


特性

  • 开箱即用:使用 NB-CLI 快速构建属于你的机器人

  • 插件系统:插件化开发,模块化管理

  • 跨平台支持:支持多种平台,以及多样的事件响应方式

  • 异步开发:异步优先式开发,提高运行效率

  • 依赖注入:简单清晰的依赖注入系统,内置依赖函数减少用户代码

应用案例

  • milkice233/efb-qq-slave:基于 ehForwarderBot 框架的 QQ 从端
https://github.com/milkice233/efb-qq-slave
  • projectriri/bot-gateway:提供跨聊天平台的通用机器人 API 的机器人消息网关
https://projectriri.github.io/bot-gateway/
  • jqqqqqqqqqq/UnifiedMessageRelay:QQ <-> Telegram Bot Framework & Forwarder
https://github.com/jqqqqqqqqqq/UnifiedMessageRelay
  • Mother-Ship/cabbageWeb:基于 Java Web 的 osu! 游戏数据查询机器人
https://github.com/Mother-Ship/cabbageWeb
  • spacemeowx2/splatoon2-qqbot:宇宙第一的 Splatoon2 的地图机器人
https://github.com/spacemeowx2/splatoon2-qqbot
  • mrthanlon/SICNUBOT:专为四川师范大学设计用于审核发布消息用的 QQ 机器人
https://github.com/mrthanlon/SICNUBOT
  • Bluefissure/FFXIVBOT:基于 Django Channels 的最终幻想 14 游戏数据查询机器人
https://github.com/Bluefissure/OtterBot
  • duan602728596/qqtools:基于 Nwjs 的 QQ 群工具(摩点、口袋 48、微博提醒、入群欢迎、定时喊话、自定义命令和回复信息等)
https://github.com/duan602728596/qqtools
  • UltraSoundX/SDFMU-Library:山东第一医科大图书馆预约机器人
https://github.com/UltraSoundX/SDFMU-Library
  • Ninzore/Wecab:网络内容聚合机器人,支持微博、B站、Twitter 等
https://github.com/Ninzore/Wecab
  • Kyomotoi/ATRI: 为即时聊天工具中复现一只优秀的功能性机器人是本项目的目标
https://github.com/Kyomotoi/ATRI
  • KimigaiiWuyi/GenshinUID: 基于 HoshinoBot/NoneBot2/QQ 官方频道 Bot (Python SDK) 的原神 Uid 查询/原神 Wiki/米社签到/树脂提醒插件
https://github.com/KimigaiiWuyi/GenshinUID


插件示例

命令式问答示例:

from nonebot import on_command
from nonebot.rule import to_me
from nonebot.matcher import Matcher
from nonebot.adapters import Message
from nonebot.params import Arg, CommandArg, ArgPlainText

weather = on_command("weather", rule=to_me(), aliases={"天气", "天气预报"}, priority=5)

@weather.handle()
async def handle_first_receive(matcher: Matcher, args: Message = CommandArg()):
    plain_text = args.extract_plain_text()  # 首次发送命令时跟随的参数,例:/天气 上海,则args为上海
    if plain_text:
        matcher.set_arg("city", args)  # 如果用户发送了参数则直接赋值

@weather.got("city", prompt="你想查询哪个城市的天气呢?")
async def handle_city(city: Message = Arg(), city_name: str = ArgPlainText("city")):
    if city_name not in ["北京", "上海"]:  # 如果参数不符合要求,则提示用户重新输入
        # 可以使用平台的 Message 类直接构造模板消息
        await weather.reject(city.template("你想查询的城市 {city} 暂不支持,请重新输入!"))

    city_weather = await get_weather(city_name)
    await weather.finish(city_weather)

# 在这里编写获取天气信息的函数
async def get_weather(city: str) -> str:
    return f"{city}的天气是..."


—END—

开源协议:MIT

开源地址:https://github.com/nonebot/nonebot2

开发教程:https://nb2.baka.icu/docs/tutorial/create-project

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章