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

本文介绍了如何使用Python和nonebot框架搭建酷Q机器人,并详细阐述了从安装nonebot到配置酷Q HTTP API,以及通过编写bot.py文件实现接收和响应QQ消息的过程。通过这个教程,读者可以成功建立一个能读取和回复QQ消息的机器人。
摘要由CSDN通过智能技术生成

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:

[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("欢迎新人入群")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值