Production of QQ private letter and group chat robot based on cqhttp

Follow Sichuan boss:   @Sichuan rookie   Make your own robot together! Chuanchuan's video tutorial through train: Complete production process of python robot (1)_ Beep beep beep_ bilibili

LAN Duoduo's small warehouse practice record blog

1. Install the appropriate package using pip

1) To configure the mirror source, refer to:

Pychart configuring image source (long term validity)_ Lando's small warehouse - CSDN blog

2) In Terminal, enter:

pip install pydantic loguru pygtrie httpx python-dotenv uvicorn fastapi requests selenium aiocqhttp ffmpeg aiohttp nb-cli nonebot2 nonebot-plugin-apscheduler nonebot-adapter-cqhttp -i https://pypi.tuna.tsinghua.edu.cn/simple

3) Installation process example: the following code can be used for testing:

import nonebot
from nonebot.adapters.cqhttp import Bot as CQHTTPBot
nonebot.init()

Check whether the installation is successful. If successful, it is shown on the right of the figure below:

2. Enter the command nb to configure the corresponding project in the console

1) Enter nb and enter

2) Select Create a New NoneBot Plugin, press enter and fill in the corresponding project name

 

3) Select In a "src" folder and press enter

 

4) Select cqhttp and enter

 

5) Enter y and enter

 

3,go-cqhttp   File download and installation

1) Download   go-cqhttp   release file
go-cqhttp   Download address of release file:   https://github.com/Mrs4s/go-cqhttp/releases/

If there is a problem that github cannot access, please refer to: https://blog.csdn.net/qq_43554335/article/details/121570391

Link: https://pan.baidu.com/s/1JLpVh9nhTqAszjr9GItnKA 
Extraction code: haed

2) Unzip the downloaded compressed package and put it into the robot project directory (my lddrobot)

3) Enter go cqhttp_ windows_ AMD64 file, press and hold shift  +  Right click   Open the Powershell window and enter the command:

.\go-cqhttp.exe


  Select 12345 to generate a config.yml file:

4. Modify. env file

1) Modify the ENVIRONMENT of the. env file to prod

  2) Since PORT 8080 is easily occupied, modify the PORT of. env.dev to 8081

3) At the same time, modify the PROT of. env.prod to 8081

5. Modify config.yml file

1) Fill in QQ group robot QQ and password

2) Enable forward ws

3) Enable reverse ws

  4) Set the reverse ws Universal address (the port number here is 8081, ha, I didn't change it in the figure, please change it to 8081)

6. Modify the bot.py code

1) Import module

import nonebot
from nonebot.adapters.cqhttp import Bot as CQHTTPBot  #Import module

2) Add plug-in

nonebot.load_plugins("src/plugins") #Add plug-in

3) Register cq

driver.register_adapter("cqhttp",CQHTTPBot) #Register cq

4) Comment out nonebot.load_from_toml("pyproject.toml")

#nonebot.load_from_toml("pyproject.toml") #Comment out

5) Complete code

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import nonebot
from nonebot.adapters.cqhttp import Bot as CQHTTPBot  #Import module

# Custom your logger
# 
# from nonebot.log import logger, default_format
# logger.add("error.log",
#            rotation="00:00",
#            diagnose=False,
#            level="ERROR",
#            format=default_format)

# You can pass some keyword args config to init function
nonebot.init()
nonebot.load_plugins("src/plugins") #Add plug-in
app = nonebot.get_asgi()

driver = nonebot.get_driver()
driver.register_adapter("cqhttp",CQHTTPBot) #Register cq
nonebot.load_builtin_plugins()

# Please DO NOT modify this file unless you know what you are doing!
# As an alternative, you should use command `nb` or modify `pyproject.toml` to load plugins
#nonebot.load_from_toml("pyproject.toml") #Comment out

# Modify some config / config depends on loaded configs
# 
# config = driver.config
# do something...


if __name__ == "__main__":
    nonebot.logger.warning("Always use `nb run` to start the bot instead of manually running!")
    nonebot.run(app="__mp_main__:app")

7. Run and test

1) Run bot.py

2) Enter. \ go-cqhttp.exe in the Power shell and run go-cqhttp.exe (if you filled in QQ and password before, you will log in automatically. If you didn't fill in, you need to scan the code to log in). The effect is as follows:

 

2) Test results: (this is just the beginning of Xiaobai robot production, and more complex functions will be added later)

Tags: Python Pycharm qq

Posted on Tue, 30 Nov 2021 07:36:29 -0500 by PallaviDalvi