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)
Brief introduction
-
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)
-
"NoneBot" is like a translator, transforming all the plug-ins of "CoolQ HTTP API plug-in" into "Python methods".
start
#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
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
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
{ "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 }
cd ~/Document mkdir qqboot_test touch boot.py cd qqboot_test & mkdir plugin
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)
touch user.py
-
Noneboot receives the message (natural language) sent by the user, and executes the specified command function by matching the keyword of the message. -
Noneboot receives the command directly, and then specifies the command function code directly
What's the weather like today? weather Is the weather good Weather in Beijing
@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 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.
python3 boot.py