python qq机器人开发 利用Python读取QQ消息

0.安装nonebot,酷q

pip3 install nonebot

在酷q官网下载:https://cqp.cc/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("欢迎新人入群")

你可能感兴趣的:(python,qq)