nonebot2 installation and use
Write in front
This article is suitable for beginners who have just come into contact with bot
Welcome friends who like fate 2 and are interested in qq group bot to join us
[fate 2 linkage bot development group]: 924026546
This article is also posted on personal blog: here
Advance preparation
- A computer (nonsense)
- One server (optional)
- python3.8 or above
- Hand (?)?
Environmental installation
Official documents: NoneBot
If upgrading from 1 to 2, uninstall the previous version first:
pip uninstall nonebot
Scaffold installation (official recommendation)
pip install nb-cli
If the installation fails, please use the following method:
Direct installation (alternative)
pip install nonebot2 or poetry add nonebot2
If installation fails
- Check whether the default version of python does not match (3.8 or above is required)
- Check whether the pip version is installed or whether the corresponding python version is normal (pip3 command is usually used. If pip3 uses python 3.5 by default, it needs to be changed)
- If there are other errors, please copy the error information and Baidu
Start creating a bot
If you have installed NB cli, you can use the (lan) lead (ren) lead installation method:
nb
d8b db .d88b. d8b db d88888b d8888b. .d88b. d888888b 888o 88 .8P Y8. 888o 88 88' 88 `8D .8P Y8. `~~88~~' 88V8o 88 88 88 88V8o 88 88ooooo 88oooY' 88 88 88 88 V8o88 88 88 88 V8o88 88~~~~~ 88~~~b. 88 88 88 88 V888 `8b d8' 88 V888 88. 88 8D `8b d8' 88 VP V8P `Y88P' VP V8P Y88888P Y8888P' `Y88P' YP Welcome to NoneBot CLI! ? What do you want to do? (Use arrow keys) ❯ Show Logo Create a New Project Run the Bot in Current Folder Create a New NoneBot Plugin List All Published Plugins Search for Published Plugin Install a Published Plugin Update a Published Plugin Remove an Installed Plugin Create a Custom Adapter List All Published Adapters Search for Published Adapters Build Docker Image for the Bot Deploy the Bot to Docker Stop the Bot Container in Docker
Select the second item Create a New Project to create a bot project
In sequence:
- Enter project name
- Select the first item to create a new folder
- Load the nonebot built-in plug-in
- Because we use cqhttp, we just choose cqhttp
Then a folder named after the bot project will be created in the directory
use
cd Project name
Enter the directory and enter
nb run
A string of ip addresses indicates success:
03-30 10:18:02 [INFO] uvicorn | Started server process [8480] 03-30 10:18:02 [INFO] uvicorn | Waiting for application startup. 03-30 10:18:02 [INFO] uvicorn | Application startup complete. 03-30 10:18:02 [INFO] uvicorn | Uvicorn running on http://127.0.0.1:8080 (Press CTRL+C to qui t)
At this time, your bot is created successfully, but to interact with QQ, you also need a protocol to send and receive QQ information
cqhttp protocol usage
Cqhttp is essentially a headless QQ client, which can be used to receive and send QQ information. The function of bot can be realized by interacting with cqhttp
Simply running a NoneBot instance will not produce any effect, because at the moment, QQ doesn't know the existence of NoneBot, so it can't send messages to it. Therefore, now we need to use a headless QQ to report messages and other events to NoneBot.
Example of QQ protocol end:
OICQ-http-api (based on OICQ)
Use of go cqgttp
Go cqhttp is relatively easy to use. This article will explain it with go cqhttp
Download go cqhttp file
Download address: download
windows:
go-cqhttp_windows_amd64.zip
Linux:
go-cqhttp_linux_amd64.tar.gz
Unzip the file and move it to the file directory
Start go cqhttp for the first time
use
./go-cqhttp
start-up
The first startup will automatically exit and require parameter modification
reference resources:
{ uin: robot QQ number password: Robot password encrypt_password: false password_encrypted: "" enable_db: true access_token: "" relogin: { enabled: true relogin_delay: 3 max_relogin_times: 0 } _rate_limit: { enabled: false frequency: 1 bucket_size: 1 } ignore_invalid_cqcode: false force_fragmented: false heartbeat_interval: 0 http_config: { enabled: false host: "0.0.0.0" port: 5700 timeout: 0 post_urls: {} } ws_config: { enabled: false host: "0.0.0.0" port: 6700 } ws_reverse_servers: [ { enabled: true reverse_url: ws://127.0.0.1:8080/cqhttp/ws reverse_api_url: ws://you_websocket_api.server reverse_event_url: ws://you_websocket_event.server reverse_reconnect_interval: 3000 } ] post_message_format: array use_sso_address: false debug: false log_level: "" web_ui: { enabled: false host: 127.0.0.1 web_ui_port: 9999 web_input: false } }
You need to modify uin, password and WS_ reverse_ enabled and reverse in servers_ url,post_message_format these items
uin: robot QQ number password: Robot password enabled: true reverse_url: ws://127.0.0.1:8080/cqhttp/ws post_message_format: array
After modification, restart go cqhttp and log in to qq as prompted
If nonebot2 is turned on at the same time, two prompts for connecting to cqhttp will be displayed on the log of nb2:
09-14 21:31:16 [INFO] uvicorn | ('127.0.0.1', 12345) - "WebSocket /cqhttp/ws" [accepted] 09-14 21:31:16 [INFO] nonebot | WebSocket Connection from CQHTTP Bot Yours QQ number Accepted!