NoneBot+酷Q,打造QQ机器人

    NoneBot 是一个基于 酷Q 的 Python 异步 QQ 机器人框架,它会对 QQ 机器人收到的消息进行解析和处理,并以插件化的形式,分发给消息所对应的命令处理器和自然语言处理器,来完成具体的功能。

一 、软件安装

  • 安装 NoneBot 库,NoneBot github地址为:https://github.com/richardchien/nonebot
1 pip install nonebot
  • 安装酷Q 软件

     酷Q 软件可以直接到官网下载,https://cqp.cc/t/23253,本教程使用的是 酷Q Air 小i版

  • 安装 HTTP API 插件

    HTTP API 插件下载地址 https://github.com/richardchien/coolq-http-api/releases

    首先将下载好的 HTTP API 插件放到 app 目录下,然后双击 CQA.exe 文件,输入机器人对应的 QQ 号和密码,登陆之后,在应用管理中,启动 HTTP API 插件。

二、配置 HTTP API 插件
    进入酷Q 的 data/app/io.github.richardchien.coolqhttpapi/config/ 目录,有一个.json 的文件,user-id 为刚刚登陆的 QQ 号。修改这个文件的如下配置

 

三、构建应用程序

    nonebot 的官方文档地址:https://none.rclab.tk/guide/getting-started.html,
    下面我们先跑一下官网上的基础例子:

import nonebot

if __name__ == "__main__":
    nonebot.init()
    nonebot.load_builtin_plugins()
    nonebot.run(host='127.0.0.1', port=8080)

   运行该程序(在普通的IDLE下即可,某些集成环境反而可能报错),我们可以在控制台看到如下日志:

    可以看到现在程序运行在了本地的 8080 端口,而且本地的 4081和 4080 端口也连接到了本服务,就是我们在 HTTP API 插件的配置文件中做的配置。

 "ws_reverse_api_url": "ws://127.0.0.1:8080/ws/api/",
  "ws_reverse_event_url": "ws://127.0.0.1:8080/ws/event/",

# 增强机器人功能之配置文件

增加 config.py 文件,输入内容如下:

from nonebot.default_config import *

SUPERUSERS = {QQ号}#1
COMMAND_START = {'', '/', '!', '', ''}
HOST = '0.0.0.0'
PORT = 8080

    SUPERUSERS:是配置一个超级 QQ 用户,我们可以为这个超级用户配置一些特殊的操作;
    COMMAND_START:是配置命令起始字符,我们增加了空字符串,所以不需要任何起始字符也能调用命令;
    另外就是配置了 host 和 端口 port。

编写自己的插件

创建一个 plugins 文件夹,在里面创建 daily.py 文件,编写如下代码:

 1 from nonebot import on_command, CommandSession
 2  2from utils import getdata
 3  
 4  
 5  @on_command('daily', aliases=('每日一句',))
 6  async def daily(session: CommandSession):
 7      daily_send = await get_daily()
 8      await session.send(daily_send[0])
 9      await session.send(daily_send[1])
10 
11 
12 async def get_daily():
13    daily_sentence = getdata.get_content()
14    return daily_sentence

完整代码

bot.py

1 import nonebot
2 import config
3 from  os import path
4 if __name__ == '__main__':
5     nonebot.init(config)
6     nonebot.load_plugins(path.join(path.dirname(__file__), 'plugins'), 'plugins')
7 
8     nonebot.run()
9     #nonebot.run(host='127.0.0.1', port=8080)

config.py

1 #!/usr/bin/env python
2 # encoding: utf-8
3 from nonebot.default_config import *
4 
5 SUPERUSERS = {317xxxxx67}#qq号
6 COMMAND_START = {'', '/', '!', '', ''}
7 HOST = '0.0.0.0'
8 PORT = 8080

/plugins/daily

 1 #!/usr/bin/env python
 2 # encoding: utf-8
 3 
 4 from nonebot import on_command, CommandSession
 5 
 6 import  requests
 7 @on_command('daily', aliases=('每日一句',))
 8 async def daily(session: CommandSession):
 9     daily_send = await get_daily()
10     await session.send(daily_send[0])
11     await session.send(daily_send[1])
12 
13 
14 async def get_daily():
15     daily_sentence = get_content()
16     return daily_sentence
17 
18 def get_content():
19     url = 'http://open.iciba.com/dsapi/'
20     res = requests.get(url)
21     content_e = res.json()['content']
22     content_c = res.json()['note']
23     return [content_c, content_e]

猜你喜欢

转载自www.cnblogs.com/sxinfo/p/12321090.html