一、安装框架 1. 安装iTXTech MCL Installer 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ sudo yum install cargo $ sudo yum install upx $ git clone https://github.com/iTXTech/mcl-installer.git $ cd mcl-installer $ cargo build --features native-tls --release $ cd target/release $ strip mcl-installer $ upx --best --lzma mcl-installer
2. 安装iTXTech MCL 1 2 3 4 5 6 7 8 9 10 11 12 $ cd PATH_TO_INSTALL_MCL $ PATH_TO_INSTALLER/mcl-installer Would you like to install Java? (Y/N, default: Y) Java version (8-15, default: 11): JRE or JDK (1: JRE, 2: JDK, default: JRE): Binary Architecture (default: x64): The latest stable version of iTXTech MCL is x.x.x Would you like to download it? (Y/N, default: Y)
1 2 $ cd PATH_TO_INSTALL_MCL $ ./mcl --update-package net.mamoe:mirai-api-http --channel stable --type plugin
1 2 $ ./mcl --update-package net.mamoe:mirai-login-solver-selenium --channel nightly --type plugin
参考文档 https://graia-document.vercel.app/
本篇仅作最直接的过程,更多细节和解释可以查看原文档。
1. 安装sdk插件 1 $ pip install graia-application-mirai
2. 添加配置 运行过一次mcl(通过命令 ./mcl
)后,会生成 ./config/net.mnmoe.mirai-api-http/setting.yml
文件,修改其中的以下配置
1 2 3 4 5 6 7 8 9 authKey: graia-mirai-api-http-authkey cacheSize: 4096 enableWebsocket: true host: '0.0.0.0' port: 8080
3. 编写自己的插件 将以下代码保存到文件 bot.py
内, 确保该文件位于你的工作区内:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 from graia.broadcast import Broadcastfrom graia.application import GraiaMiraiApplication, Sessionfrom graia.application.message.chain import MessageChainimport asynciofrom graia.application.message.elements.internal import Plainfrom graia.application.friend import Friendloop = 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=1234567890 , websocket=True ) ) @bcc.receiver("FriendMessage") async def friend_message_listener (app: GraiaMiraiApplication, friend: Friend, message: MessageChain) : await app.sendFriendMessage(friend, MessageChain.create([ Plain(f"Hello { friend.nickname } , you said '{ message.asDisplay() } '!" ) ])) app.launch_blocking()
4. 运行自己的插件 首先保存 mcl 处于正常运行状态,出现类似如下信息
1 2 3 4 5 $ ./mcl ...... 2021-04-03 01:55:26 I/Bot.1234567890: Login successful 2021-04-03 01:55:26 I/main: mirai-console started successfully. 2021-04-03 01:55:28 I/Bot.1234567890: Server list: 36.155.229.203:8080, 111.30.178.34:8080, 120.232.18.180:8080, 111.30.169.27:80, 36.155.229.140:14000, 120.232.18.55:443, 111.30.181.202:80, 36.155.229.228:80, msfwifi.3g.qq.com:8080, 111.30.169.93:80.
运行插件:
接下来用另一个QQ向机器人发送消息即可(“Unknown Error”是我的QQ昵称)
同时,Mirai Console 得到以下日志:
1 2 3 4 2021-04-03 02:24:49 V/Bot.2690919164: Unknown Error(1361050885) -> 123 2021-04-03 02:24:49 V/Bot.2690919164: Friend(1361050885) <- Hello Unknown Error, you said '123'! 2021-04-03 02:25:03 V/Bot.2690919164: Unknown Error(1361050885) -> 456 2021-04-03 02:25:03 V/Bot.2690919164: Friend(1361050885) <- Hello Unknown Error, you said '456'!
bot.py 得到以下日志:
1 2 3 4 [2021-04-03 02:24:49,152][INFO]: 2690919164: [Unknown Error(1361050885)] -> '[mirai:source:11960,1617387889]123' [2021-04-03 02:24:49,366][INFO]: [BOT 2690919164] Friend(1361050885) <- "Hello Unknown Error, you said '123'!" [2021-04-03 02:25:03,234][INFO]: 2690919164: [Unknown Error(1361050885)] -> '[mirai:source:11961,1617387903]456' [2021-04-03 02:25:03,432][INFO]: [BOT 2690919164] Friend(1361050885) <- "Hello Unknown Error, you said '456'!"
至此,QQ机器人已经可以正常运行,至于机器人可以做哪些事情,就取决于你的插件有多么丰富了!
5. 更丰富的插件 在 https://graiaproject.github.io/Application/ 中发现更多有用的api来使你的插件更加丰富。
使用 saya 管理插件 1 2 3 pip install graia-saya pip install image git clone https://github.com/Roc136/saya_plugins_collection.git
涉及到的项目仓库地址:
Mirai:https://github.com/mamoe/mirai
Mirai Console: https://github.com/mamoe/mirai-console
mirai-api-http: https://github.com/project-mirai/mirai-api-http
Mirai Console Loader: https://github.com/iTXTech/mirai-console-loader
iTXTech MCL Installer: https://github.com/iTXTech/mcl-installer
mirai-login-solver-selenium: https://github.com/project-mirai/mirai-login-solver-selenium
Graia Framework: https://github.com/GraiaProject/Application