Backend Development 10 min read

Step-by-Step Guide to Building a QQ Bot with Mirai, Python, and Graia Application

This tutorial walks you through the complete process of setting up the Mirai ecosystem, configuring mirai-console-loader, handling slider verification with mirai-login-solver-selenium, installing mirai-api-http, switching to OpenJDK, and finally writing and running a Python bot using graia-application-mirai.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Step-by-Step Guide to Building a QQ Bot with Mirai, Python, and Graia Application

Introduction – The article provides a practical tutorial for developers who want to create a QQ robot using the Mirai framework, focusing on a Python‑centric workflow while still covering necessary Java/Kotlin components.

Mirai ecosystem – Mirai consists of the core framework (mirai‑core) and an API layer (mirai‑core‑api). The official console (mirai‑console) wraps these components, and plugins such as mirai‑api‑http enable language‑agnostic development. Images illustrate the ecosystem diagram.

Using mirai‑console‑loader (mcl) – To avoid manual setup of mirai‑core, mirai‑console, and mirai‑console‑terminal, the one‑click starter mirai-console-loader is downloaded, extracted, and run via the command line. Screenshots show successful startup and a configuration error that requires editing a config file.

Handling slider verification – The default console cannot pass the QQ slider captcha. The mirai-login-solver-selenium plugin is added with the command:

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

Because the plugin depends on ChromeDriver, the tutorial explains how to locate the required version, download a matching ChromeDriver binary, rename it, and replace the file in the appropriate directory.

Configuring mirai‑api‑http – After placing the mirai-api-http JAR into the plugins folder, the user must adjust the setting.yml file. Example configuration:

# file: mcl-1.0.3/config/net.mamoe.mirai.api.http/setting.yml
authKey: graia-mirai-api-http-authkey
cacheSize: 4096
enableWebsocket: true
host: '0.0.0.0'
port: 8080

If a signature verification error occurs, the guide advises switching from Oracle JDK to OpenJDK, showing how to download, extract, and add the JDK path to system environment variables.

Installing the Python client – With the backend ready, install the Python library that talks to mirai‑api‑http:

pip install graia-application-mirai

Then create bot.py with the following minimal example (comments omitted for brevity):

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",
        authKey="graia-mirai-api-http-authkey",
        account=5234120587,
        websocket=True
    )
)

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

app.launch_blocking()

Running the script and sending any message to the QQ bot should result in a "Hello, World!" reply, confirming a successful setup. A screenshot of the successful interaction is included.

Conclusion – The guide emphasizes that these steps constitute only a starter tutorial; readers are encouraged to consult the official Mirai documentation for deeper API usage and advanced features.

backendPythontutorialmiraiQQ botgraia
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.