How to develop QQ robot with Python

Preface

Although this article ultimately achieves the goal of developing mirai robots in python, the starting tutorial, especially the environment configuration, still has a lot of the same operations, and there are still lessons for other programming languages.

It is assumed that you have installed Java, Python and other necessary environments for running the mirai ecosystem.

mirai ecology

  • mirai official ecological document

  • To use mirai to develop QQ robots, you must first have a certain understanding of its ecology, because it is too complicated and has many pitfalls, so if you know more about it, you will be able to solve problems faster when you encounter them.

  • mirai ecological summary chart

picture

  • Simply put, the core of the mirai ecosystem is the Mirai framework, which includes mirai-core and mirai-core-api.

  • Among them, the former is responsible for protocol-related content, while the latter is responsible for providing external interfaces to operate the former. Therefore, it is mirai-core-api that directly deals with programmers, and mirai-core is invisible to us.

  • QQ robots can already be developed using mirai-core-api, but it is still too difficult for newcomers, so the official mirai development team wrote a QQ robot program, mirai-console, which uses the basic functions provided by the mirai framework. Based on this, it is encapsulated and further provides a more convenient open interface.

  • With mirai-console, we no longer need to directly develop mirai's QQ robot, but develop mirai-console plug-ins, such as the following model:

picture

  • The embarrassing thing is that to develop the mirai-console plug-in, you need to use java or kotlin. If you are like me and are not familiar with them, then another official plug-in, mirai-api-http, can solve this problem.

  • So using mirai-api-http to develop QQ robot, it became the following model:

picture

  • As you can see, when we use mirai-api-http, we have more development language choices. Here I choose python.

Start

Start mirai-console using mirai-console-loader

According to the above introduction, to develop a mirai QQ robot, we first need to run mirai-console, and it is difficult to achieve this step. For example, you need to prepare mirai-core, mirai-console and mirai-console-terminal, and then you need to start it through a long series of instructions.

The official has obviously taken this into consideration. In order to save the newcomers who were dissuaded by this difficult operation, the official has launched mirai-console-loader (mcl for short) - the official one-click launcher of mirai-console. So you just need to download it (don't worry about 0.0 as mentioned in the first step). Github warehouse location: mirai-console-loader[2]

After downloading mcl, unzip it, open cmd, switch to the directory where mcl is located, and run mcl. As shown below:

picture

If nothing else, mirai-console was successfully started, as shown below:

picture

However, the project we downloaded from the official website encountered an error of 0.0 when running (if you did not make any errors, ignore this step). The reason is an error in the configuration file (too bad - -).

The modification method is as follows:

picture

Then run mcl again. As expected, it can run successfully. The first big hurdle has been passed...and then there is another big hurdle

Handle slider verification secondary login using mirai-login-solver-selenium

In the successfully started mcl window, running the command to log in to qq: login account password should cause an error, because mirai-console cannot handle slider verification when logging in:

picture

So we need another project of mirai, mirai-login-solver-selenium[3] to assist in login.

mirai-login-solver-selenium installation steps (need to install Chrome browser first) 

First end the previously running mirai-console, then run the following command on the command line to add the package

mcl --update-package net.mamoe:mirai-login-solver-selenium --channel nightly --type plugin

Then re-run mcl, so that mcl will try to download mirai-login-solver-selenium.

However, I also had problems with this step (if you don’t have problems, please skip it). Because it uses selenium, it needs to use chromedriver. However, the download of chromedriver always fails, so in this step, you need to manually download chromedriver and then replace it to the corresponding directory. Proceed as follows:

1. Check the cmd window and find out what version of chromedriver mcl is downloading.

picture

2. Then go to another mirror source of chromedriver to download, recommended: chromedriver[4] 

3. Just find one with a similar version number. For example, I will download 86.0.4240.22

picture

4. Unzip the downloaded file and rename it to chromedriver-86.0.4240.198.exe, which is the file name we just viewed in the command line window. It must be consistent with the name of the file you want to download.

5. End the mcl command line program that was run before, and then replace the prepared chromedriver-86.0.4240.198.exe to the following directory

picture

6. Rerun the mcl program. If everything goes well, you can continue with the previous steps and enter the command: login account password and try to log in. Next, a browser window will pop up, and you only need to complete the login verification in a fool-like manner. If the login is successful, you should not need to re-verify every time you log in in the future.

Crossed another hurdle...then came another hurdle

Use mirai-api-http to increase language extensibility (to be able to develop in other languages)

I have been talking about mirai-api-http, but so far, we have not used it. The previous work did two things

  1. Run mirai-console using mcl

  2. Use mirai-login-solver-selenium to assist in passing the slider verification code to complete the login

Then start using mirai-api-http. First, download mirai-api-http[5] at the mirai-api-http project address.

Then place the downloaded jar package in the plugin folder, as shown in the figure

picture

Then restart mcl and log in again. In this way, the preparation work is completed, but I have some errors. It seems that it is a signature verification problem. The errors are as follows:

picture

After consulting around, I learned that it is a problem with oracle JDK, so I only need to replace orcaleJDK with open JDK. The steps are as follows:

1. Download open JDK[6]. For example, I downloaded the version shown in the picture:

picture

2. Unzip open JDK and place it in a location you think is appropriate. For example, I placed it as shown in the figure below:

picture

3. Add the path to the jdk to the environment variable: This Computer ->Right-click Properties -> Advanced System Settings -> Advanced -> Environment Variables, and then follow the steps below

picture

picture

Develop mirai robot using python via graia-application-mirai

The previous operations, up to this step, are basically common to all programmers using languages ​​other than Java/Kotlin. The following operations are only written for programmers using python

graia-application-mirai official document[7]

First configure mirai-api-http, as shown in the figure:

picture

The following is a reference, just make your own choice.

# file: mcl-1.0.3/config/net.mamoe.mirai.api.http/setting.yml
authKey: graia-mirai-api-http-authkey # 你可以自己设定, 这里作为示范

# 可选,缓存大小,默认4096.缓存过小会导致引用回复与撤回消息失败
cacheSize: 4096

enableWebsocket: true # 是否启用 websocket 方式, 若使用 websocket 方式交互会得到更好的性能
host: '0.0.0.0' # httpapi 服务监听的地址, 错误的设置会造成 Graia Application 无法与其交互
port: 8080 # httpapi 服务监听的端口, 错误的设置会造成 Graia Application 无法与其交互

Restart mcl and update configuration

Next, install the module for python to operate the mirai-api-http interface: graia-application-mirai

pip install graia-application-mirai

Copy the following code to bot.py, follow the comment prompts, and modify it slightly based on the configuration of mirai-api-http just now. then run

from graia.broadcast import Broadcast
from graia.application import GraiaMiraiApplication, Session
from graia.application.message.chain import MessageChain
import asyncio

from graia.application.message.elements.internal import Plain
from graia.application.friend import Friend

loop = asyncio.get_event_loop()

bcc = Broadcast(loop=loop)
app = GraiaMiraiApplication(
    broadcast=bcc,
    connect_info=Session(
        host="http://localhost:8080", # 填入 httpapi 服务运行的地址
        authKey="graia-mirai-api-http-authkey", # 填入 authKey
        account=5234120587, # 你的机器人的 qq 号
        websocket=True # Graia 已经可以根据所配置的消息接收的方式来保证消息接收部分的正常运作.
    )
)

@bcc.receiver("FriendMessage")
async def friend_message_listener(app: GraiaMiraiApplication, friend: Friend):
    await app.sendFriendMessage(friend, MessageChain.create([
        Plain("Hello, World!")
    ]))

app.launch_blocking()

Then send a random message to your QQ robot. If it replies Hello, World!, it means the operation is successful.

Success looks like this:

picture

Conclusion

All the above operations are just a starting tutorial for developing QQ robots using mirai. If you need to know more, you should read the official documents to learn more APIs.

For more exciting tutorials, please search "Qianfeng Education" on Station B 

Qianfeng Education's full set of Python video tutorials, easily mastering Excel, Word, PPT, email, crawlers, and office automation (lectured by Song Runing)

 

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/134726778