Do1e

Do1e

github
email

Build Your Own QQ Bot

This article is synchronized and updated to xLog by Mix Space
For the best browsing experience, it is recommended to visit the original link
https://www.do1e.cn/posts/code/building-your-own-qq-bot


Introduction#

Recently, I found that the WebUI of NapCatQQ has been improved, making it quite suitable for beginners. Therefore, I wrote this article.

This article only covers the basic usage methods and may lack some advanced features. However, it is also necessary to understand the following basic knowledge: command line, HTTP protocol, Docker, Python, and it is not completely zero-based. (If you only want to use Webhook for notifications or use existing NoneBot plugins, no Python programming is required.)

The tools used are NapCatQQ and NoneBot. The former runs QQ and connects to protocols like OneBot11, while the latter is a framework implemented based on Python calling protocols. You can refer to the official documentation if you encounter any issues. (If you only want to use Webhook for notifications, you can set up NapCatQQ.)

Preparations#

  1. A server, either internal or external network.
  2. A QQ number to serve as the QQ bot; it is not recommended to use your main account.

QQ can detect whether plugins are being used, and your account may encounter the following issues: forced logout, login restrictions, freezing.
Please evaluate and decide whether to continue attempting to use it based on your actual situation; the author is not responsible for any consequences of building a bot based on this article.

NapCatQQ#

Installation#

There are many installation methods for NapCatQQ, but since it is a service, I still recommend using Docker to install the Shell version: https://napneko.github.io/guide/boot/Shell#napcat-docker-linux-containerized-deployment
The Docker container comes with the NTQQ body, so there is no need to install the corresponding version of QQ separately.

docker run -d \
    -v /data/napcat/QQdata:/app/.config/QQ \      # QQ data directory
    -v /data/napcat/logs:/app/napcat/logs \       # Log directory
    -v /data/napcat/config:/app/napcat/config \   # Configuration directory
    -v /etc/localtime:/etc/localtime:ro \
    --net=host \    # It is recommended to use the host network to connect to the subsequent NoneBot
    --name napcat \
    --restart=always \
    mlikiowa/napcat-docker:latest

At this point, you can access http://[IP]:6099/webui to see the NTQQ web page, where you can log in and set up. The login password can be found in the log file (default: napcat), and it is recommended to change it after logging in.
Alternatively, you can also use docker logs napcat to view the logs and scan the QR code to log in to QQ.
When scanning the QR code, it is recommended to check the option to avoid mobile verification later.

Enable HTTP Protocol#

In the WebUI, find Network Configuration -> New -> HTTP Server, and you can name it, set the Host, and port as you like. It is recommended to choose Array for the message format; actual testing shows it is compatible with String, and you can send mixed text and image messages. It is strongly recommended to fill in the Token to prevent malicious requests.

After enabling, you can test it in API Debugging -> HTTP or any other API testing tool you are comfortable with. You can refer to the API here: https://napcat.apifox.cn/

There are two ways to use the Token:

  1. Add Authorization: [Token] in the request header.
curl -X GET 'http://[IP]:3000/get_friend_list' -H 'Authorization: [Token]'
  1. Add ?access_token=[Token] in the URL.
curl -X GET 'http://[IP]:3000/get_friend_list?access_token=[Token]'

Use Webhook for Notifications#

The most important interfaces here are sending private messages and sending group messages, which can be found in API Debugging -> HTTP. There is also a function for constructing messages, which is quite simple.
Here is an example of sending a private message:

curl -X POST 'http://[IP]:3000/send_private_msg' -H 'Content-Type: application/json' -H 'Authorization: [Token]' -d '{
  "user_id": "[TargetQQ]",
  "message": [
    {
      "type": "text",
      "data": {
        "text": "Hello World!"
      }
    },
    {
      "type": "face",
      "data": {
        "id": "63"
      }
    },
    {
      "type": "image",
      "data": {
        "file": "https://napneko.github.io/assets/newlogo.png"
      }
    }
  ]
}'

image

Some applications may use shoutrrr as the implementation of Webhook, and you can refer to the following configuration:

generic://[IP]:3000/send_private_msg?access_token=[Token]&template=json&titleKey=user_id&messageKey=message&@Content-Type=application/json&$user_id=[TargetQQ]

Some applications may already support the OneBot11 protocol; in this case, just fill in http://[IP]:3000, the Token, and the target QQ number.

nginx Reverse Proxy#

If you have a domain name, you can refer to the following NapCatQQ nginx configuration example:

server {
    listen 80;
    listen [::]:80;
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name qq.example.cn;
    access_log /var/log/nginx/access.qq.log;
    error_log /var/log/nginx/error.qq.log;
    location /webui {
        proxy_pass http://127.0.0.1:6099$request_uri;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /api {
        proxy_pass http://127.0.0.1:6099$request_uri;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location /files {
        proxy_pass http://127.0.0.1:6099$request_uri;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    location / {
        if ($request_method = POST) {
            access_log /var/log/nginx/post.qq.log postdata;
        }
        proxy_pass http://127.0.0.1:3000$request_uri;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

NoneBot#

To be continued, you can refer to the documentation

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.