admin管理员组

文章数量:1558996

0.安装nonebot,酷q

pip3 install nonebot

在酷q九游会真人第一品牌官网下载:https://cqp/t/23253 air图灵版
安装coolq-http-api
网页地址:https://richardchien.gitee.io/coolq-http-api/docs/4.12/#/
并将其放在酷qair的app文件夹中
运行cqa.exe

在悬浮窗里点击应用管理启用http api
在data/app/io.github.richardchien.coolqhttpapi/config/下可以看到一个以你登录qq号命名
的.json文件
打开该文件然后找到下面三个配置项
ws_reverse_api_url
ws_reverse_event_url
use_ws_reverse

改为

{
    "host": "[::]",
    "port": 5700,
    "use_http": true,
    "ws_host": "[::]",
    "ws_port": 6700,
    "use_ws": false,
    "ws_reverse_url": "",
    "ws_reverse_api_url": "ws://127.0.0.1:8080/ws/api/",
    "ws_reverse_event_url": "ws://127.0.0.1:8080/ws/event/",
    "ws_reverse_reconnect_interval": 3000,
    "ws_reverse_reconnect_on_code_1000": true,
    "use_ws_reverse": true,
    "post_url": "",
    "access_token": "",
    "secret": "",
    "post_message_format": "string",
    "serve_data_files": false,
    "update_source": "china",
    "update_channel": "stable",
    "auto_check_update": false,
    "auto_perform_update": false,
    "show_log_console": true,
    "log_level": "info"
}

1.尝试

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

运行该程序,我们可以在控制台看到如下日志:

ujson module not found, using json
msgpack not installed, msgpackserializer unavailable
[2020-02-16 20:57:45,401 nonebot] info: succeeded to import "nonebot.plugins.base"
[2020-02-16 20:57:45,401 nonebot] info: running on 127.0.0.1:8080
running on http://127.0.0.1:8080 (ctrl  c to quit)
[2020-02-16 20:57:45,457] running on 127.0.0.1:8080 over http (ctrl  c to quit)
[2020-02-16 20:57:47,067] 127.0.0.1:2990 get /ws/api/ 1.1 101 - 17916
[2020-02-16 20:57:47,101] 127.0.0.1:2991 get /ws/event/ 1.1 101 - 26923
[2020-02-16 20:57:47,124] info in __init__: received event: meta_event.lifecycle.connect
[2020-02-16 20:57:56,424] info in __init__: received event: message.private.friend

可以看到现在程序运行在了本地的 8080 端口,而且本地的 6568 和 6569 端口也连接到了本服务,就是我们在 http api 插件的配置文件中做的配置
试下发送:/echo 你好

控制台:

[2020-02-16 21:01:28,975 nonebot] info: self: 371631503, message 8 from 296853751: /echo 你好`
[2020-02-16 21:01:28,975 nonebot] debug: parsing command: /echo 你好`
[2020-02-16 21:01:28,976 nonebot] debug: matched command start: /
[2020-02-16 21:01:28,977 nonebot] debug: split command name: ('echo',)
[2020-02-16 21:01:28,981 nonebot] debug: command ('echo',) found, function: <function echo at 0x000001b219ca68c8>
[2020-02-16 21:01:28,983 nonebot] debug: new session of command ('echo',) created
[2020-02-16 21:01:28,990 nonebot] debug: running command ('echo',)
[2020-02-16 21:01:29,152 nonebot] debug: session of command ('echo',) finished
[2020-02-16 21:01:29,152 nonebot] info: message 8 is handled as a command

并且 机器人回复我们你好,这时候我们可以确定成功了

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

from nonebot.default_config import *
superusers = {12345}

创建一个mybot文件夹,在里面创建一个plugins文件夹
在里面创建一个bot.py文件

结构如下:

├── mybot
│ └── plugins
│ └── bot.py
├── main.py
└── config.py

修改main.py

import nonebot
import config
from os import path
if __name__ == "__main__":
	nonebot.init(config)
	nonebot.load_plugins(
		path.join(path.dirname(__file__),"mybot","plugins"),
		"mybot.plugins"
		)
	nonebot.run(host='127.0.0.1', port=8080)
ujson module not found, using json
msgpack not installed, msgpackserializer unavailable
[2020-02-16 21:15:48,202 nonebot] info: succeeded to import "mybot.plugins.bot"
[2020-02-16 21:15:48,202 nonebot] info: running on 127.0.0.1:8080
running on http://127.0.0.1:8080 (ctrl  c to quit)
[2020-02-16 21:15:48,239] running on 127.0.0.1:8080 over http (ctrl  c to quit)
[2020-02-16 21:15:48,550] 127.0.0.1:3426 get /ws/event/ 1.1 101 - 14952
[2020-02-16 21:15:48,572] 127.0.0.1:3427 get /ws/api/ 1.1 101 - 12883
[2020-02-16 21:15:48,576] info in __init__: received event: meta_event.lifecycle.connect

命令行提示import成功

修改bot.py

import nonebot
from nonebot import on_request,requestsession
from nonebot import on_notice,noticesession
bot = nonebot.get_bot()
@bot.on_message("private")
async def _(ctx):
	print(ctx)
@bot.on_message("group")
async def _(ctx):
	print(ctx)
@on_request("group")
async def _(session:requestsession):
	if session.ctx["comment"] =="python"
		await session.approve()
		return
	await session.reject("请说暗号")
@on_notice('group_increase')
async def _(session:noticesession):
	print("欢迎新人入群")

本文标签: 机器人消息qqpython