Environmental requirements

  • Mac OS/linux (similar to other operating systems)
  • Cooldocker+CoolQ HTTP API plug-in
  • NoneBot
  • docker
If you are a Windows server or a computer, you can also install Cool Q, while a Mac Linux/Linux server can only install Cool Q Docker (Docker installation is recommended)

You can also directly use the "CoolQ HTTP API Plug in" Docker image package This eliminates the need to configure the CoolQ HTTP API Plug in.

Brief introduction

Briefly introduce the principle.

  • Log in to your QQ account in KuQ, and "KuQ" will take over your message and send QQ's All message requests and sending It is encapsulated into some interfaces for the "plug-in" to take over and call further.
All plug-in suffixes of Cool Q are .cpk , which needs to be installed inside KuQ.
  • The "CoolQ HTTP API Plug in" is a plug-in dedicated to "http request and information processing". The language used is of course the same as that of CoolQ (Yi language seems to be)

There are few developers who can speak the language easily emmm

  • "NoneBot" is like a translator, transforming all the plug-ins of "CoolQ HTTP API plug-in" into "Python methods".

The CoolQ HTTP API plug-in has many translators in different languages, which is convenient Developers using different languages can directly process the information of KuQ

In this way, we can directly write Python files to receive or send QQ messages!

start

Start installation!

 #1. Install Docker brew install docker #2. Install the KuQ+http plug-in integration image docker pull richardchien/cqhttp:latest #3. Install nonebot pip3 install nonebot

We first enable Cool Q in Docker

 docker run -ti --rm --name cqhttp-test  -v $(pwd)/coolq:/home/user/coolq  -p 9001:9000  -p 5701:5700  -e COOLQ_ACCOUNT=123456   -e CQHTTP_POST_URL= http://example.com:8080   -e CQHTTP_SERVE_DATA_FILES=yes   richardchien/cqhttp:latest

If the above command needs to be changed, change it nine thousand and one and five thousand seven hundred and one It's OK. The others don't need to be modified.

Including:

  • nine thousand and one The port number is the port number for logging into KuQ
  • five thousand and one The port number is the open port of the HTTP API plug-in, and it doesn't matter. If multiple cool Qs need to be enabled, ensure the port number and container name cqhttp-test Not repeated

After startup, open 127.0.0.1:9001 If deployed to a Linux server IP address: 9001

After opening the page, you will be asked to enter a password. The default password is MAX8char After entering, there is a login interface similar to QQ. Enter the account password. (You may need to enter the verification code for the first login)

At this point, KuQ actually logs in to your QQ as an Android phone and takes over all your message processing. The CoolQ HTTP API plug-in is also opening the interface for other plug-ins to call.


Next, we need to use the nonebot module and Python code to take over the receiving and sending of QQ messages.
stay coolq/data/app/io.github.richardchien.coolqhttpapi/config/ There is a. json file<your login QQ number>. Open it and modify (add if not) the three variables to corresponding values

 { "ws_reverse_api_url": "ws://127.0.0.1:9999/ws/api/", "ws_reverse_event_url": "ws://127.0.0.1:9999/ws/event/", "use_ws_reverse": true }

!> If Docker is deployed on the computer 127.0.0.1 Change to host.docker.internal
!> If the docker is deployed on a Linux server 127.0.0.1 Change to 172.17.0.1

This operation is to let the CoolQ HTTP API Plug in lend the received and sent information to the noneboot module to take over.


So, let's start developing with Python through the nonboot module.

You can use pycharm ide, or you can directly create a folder and use the command line to enable it.

We directly use the command line to start.

 cd ~/Document mkdir qqboot_test touch boot.py cd qqboot_test & mkdir plugin

The first instruction cd Switch to the specified directory. You can follow the instructions exactly the same.

Open boot.py and copy and paste the following code:

 import nonebot from os import path if __name__ == '__main__': nonebot.init() nonebot.load_plugins( path.join(path.dirname(__file__),'plugins'), 'plugins' ) nonebot.run(host='127.0.0.1', port=9999)

We used to use ws://127.0.0.1:9999/ws/api/ Take over the message processing of the CoolQ HTTP API Plug in.

The above code starts the noneboot module and binds it to the specified port of the host.

!> If it is the boot.py program running on the local computer, the host should fill in 127.0.0.1 that will do
!> For the boot.py program deployed by the Linux server, change the host to 0.0.0.0

Above we are qqboot_test A new one has been created in the project plugins Folder, and in boot.py Indicates the location of the plug-in folder.


Now create a file in the plug-in folder.

 touch user.py

Before specific development, briefly introduce the processing logic of noneboot.

There are two ways:

  1. Noneboot receives the message (natural language) sent by the user, and executes the specified command function by matching the keyword of the message.
  2. Noneboot receives the command directly, and then specifies the command function code directly

User sent /Text The format of indicates sending written words Command named

We process the user's natural language information in the first way.

For example, when the user sends the following sentence

 What's the weather like today? weather Is the weather good Weather in Beijing

stay user.py Edit inside the file:

 @On_natural_language (keywords={'Weather '}) async def _(session: NLPSession): #Return the intention command. The first two parameters are required, representing confidence and intention command name respectively return IntentCommand(60.0, 'weather') @On_command ('weather ', aliases=('weather')) async def weather(session: CommandSession): #Get the city name from the session state (session. state). If it does not exist, ask the user City=session. get ('city ', prompt='Your city is') weather_report = await get_weather(city) #Send weather forecast to users await session.send(weather_report) async def get_weather(city: str) -> str: #Here we simply return a string Return city+"Nice weather"

The code is divided into three parts:

  • The first part is to parse natural language, as long as it contains keywords The keywords in it will be processed with corresponding commands weather
  • The second part is the command processing function, aliases It is an alias, which means through /[aliases] In this way, you can directly execute command functions
  • The third part is asynchronous Request to return the weather forecast, which is a feature of noneBoot.

Finally, start the project:

 python3 boot.py

After starting this command, you will see all the information output taken over by KuQ in the console output.


The above is the development and deployment of a simple QQ chat robot.

Deploy the robot to the docker on the server. If you want, you can have a lot of creativity. For example, if you receive any information at night, you will automatically reply "rest at night and deal with it after going online"

But the only possible problem is that after starting KuQ in Docker, You can't log in to the same QQ number again

Enjoy your use~

Last modification: November 27, 2019
Do you like my article?
Don't forget to praise or appreciate, let me know that you accompany me on the way of creation.