找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 3172|回复: 2

QQ机器人mirai之初试

[复制链接]

307

主题

228

回帖

7343

积分

用户组: 真·技术宅

UID
2
精华
76
威望
291 点
宅币
5593 个
贡献
253 次
宅之契约
0 份
在线时间
948 小时
注册时间
2014-1-25
发表于 2021-8-3 18:38:14 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
本帖最后由 元始天尊 于 2021-8-3 20:48 编辑

mirai简介

  mirai是用Android QQ协议实现的开源机器人库。基于Java环境因此支持全平台。可以用来做很多自动化的操作。项目地址在https://github.com/mamoe/mirai,论坛https://mirai.mamoe.net。这软件开发的还挺好的  

mirai安装

  从https://github.com/iTXTech/mcl-installer/releases选择合适版本,这里笔者开发用的MacOS环境,因此以MacOS为例。首先确保自己的Java SDK版本为16及以上。

chmod +x mcl-installer-1.0.3-macos-amd64 && ./mcl-installer-1.0.3-macos-amd64
# 这一步笔者执行失败,因为没连vpn。不过发现错误提示中也是下载文件失败,因此直接下载这个解压放到当前目录<https://github.com/iTXTech/mirai-console-loader/releases>
# 此时目录下有mcl.jar mcl.cmd mcl scripts
chmod +x mcl && ./mcl
# mcl首次初始化下载一堆jar,然后提示成功
2021-08-03 16:14:51 I/main: Starting mirai-console...
2021-08-03 16:14:51 I/main: Backend: version 2.7-M2, built on 2021-07-06 21:43:31.
2021-08-03 16:14:51 I/main: Frontend Terminal: version 2.7-M2, provided by Mamoe Technologies
2021-08-03 16:14:51 I/main: Welcome to visit https://mirai.mamoe.net/
2021-08-03 16:14:51 I/plugin: Successfully loaded plugin MCL Addon
2021-08-03 16:14:51 I/main: Prepared built-in commands: autoLogin, help, login, permission, status, stop
2021-08-03 16:14:51 I/MCL Addon: iTXTech MCL Version: 1.2.2-60c67fb
2021-08-03 16:14:51 I/main: 1 plugin(s) enabled.
2021-08-03 16:14:51 I/main: mirai-console started successfully.

测试

  在mirai提示符下尝试登陆,/login QQ号 密码,于是就登陆成功了,并且可以看到mirai已经在持续不断的接收消息了。(因为笔者Mac用的网络和手机登陆QQ的网络是同一个,因此不存在网络引发的登陆验证问题,直接就可以登录)  

  16:14:46 [INFO] iTXTech Mirai Console Loader version 1.2.2-60c67fb
  16:14:46 [INFO] https://github.com/iTXTech/mirai-console-loader
  16:14:46 [INFO] This program is licensed under GNU AGPL v3
  16:14:46 [INFO] Verifying "org.bouncycastle:bcprov-jdk15on" v1.64
  16:14:48 [INFO] Verifying "net.mamoe:mirai-console" v2.7-M2
  16:14:49 [INFO] Verifying "net.mamoe:mirai-console-terminal" v2.7-M2
  16:14:49 [INFO] Verifying "net.mamoe:mirai-core-all" v2.7-M2
  16:14:50 [INFO] Verifying "org.itxtech:mcl-addon" v1.2.2
  16:14:50 [INFO] Fetching Mirai Console Loader Announcement...
  16:14:50 [INFO] Mirai Console Loader Announcement:
2021-08-03 16:14:51 I/main: Starting mirai-console...
2021-08-03 16:14:51 I/main: Backend: version 2.7-M2, built on 2021-07-06 21:43:31.
2021-08-03 16:14:51 I/main: Frontend Terminal: version 2.7-M2, provided by Mamoe Technologies
2021-08-03 16:14:51 I/main: Welcome to visit https://mirai.mamoe.net/
2021-08-03 16:14:51 I/plugin: Successfully loaded plugin MCL Addon
2021-08-03 16:14:51 I/main: Prepared built-in commands: autoLogin, help, login, permission, status, stop
2021-08-03 16:14:51 I/MCL Addon: iTXTech MCL Version: 1.2.2-60c67fb
2021-08-03 16:14:51 I/main: 1 plugin(s) enabled.
2021-08-03 16:14:51 I/main: mirai-console started successfully.
> /login 258711658 密码保密
2021-08-03 16:18:01 W/stderr:
2021-08-03 16:18:01 I/Mirai: Mirai 正在使用桌面环境. 如遇到验证码将会弹出对话框. 可添加 JVM 属性 `mirai.no-desktop` 以关闭.
2021-08-03 16:18:01 I/Mirai: Mirai is using desktop. Captcha will be thrown by window popup. You can add `mirai.no-desktop` to JVM properties (-Dmirai.no-desktop) to disable it.
2021-08-03 16:18:01 W/stderr: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
2021-08-03 16:18:01 W/stderr: SLF4J: Defaulting to no-operation (NOP) logger implementation
2021-08-03 16:18:01 W/stderr: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2021-08-03 16:18:02 I/Bot.258711658: [AccountSecretsManager] Loaded account secrets from local cache.
2021-08-03 16:18:03 I/Bot.258711658: [AccountSecretsManager] Saved account secrets to local cache for fast login.
2021-08-03 16:18:03 I/Bot.258711658: Login successful.
2021-08-03 16:18:04 V/Bot.258711658: Event: MemberJoinEvent.Active(member=1360972041)
2021-08-03 16:18:04 V/Bot.258711658: Event: BotOnlineEvent(bot=Bot(258711658))
2021-08-03 16:18:04 I/Bot.258711658: Bot login successful.
无聊的人 (258711658) Login successful
2021-08-03 16:19:24 V/Bot.258711658: Event: FriendInputStatusChangedEvent(friend=Friend(571652571), inputting=true)
2021-08-03 16:19:25 V/Bot.258711658: 夜华(571652571) -> aaa
2021-08-03 16:19:25 V/Bot.258711658: Event: FriendInputStatusChangedEvent(friend=Friend(571652571), inputting=false)
2021-08-03 16:19:26 V/Bot.258711658: Event: FriendInputStatusChangedEvent(friend=Friend(571652571), inputting=false)
2021-08-03 16:20:43 V/Bot.258711658: [大生意(72679285)] 夜华(571652571) -> hi

  下面来设置自动登录,重启mirai生效  

> /autologin
2021-08-03 16:25:22 W/console: 参数不匹配, 你是否想执行:
/autoLogin add <account> <password> [passwordKind]    (参数不足)
/autoLogin clear    (参数不足)
/autoLogin list    (参数不足)
/autoLogin remove <account>    (参数不足)
/autoLogin removeConfig <account> <configKey>    (参数不足)
/autoLogin setConfig <account> <configKey> <value>    (参数不足)
> /exit

部署http接口

  mirai常用的插件有  

  • chat-command 官方插件,允许在聊天环境通过以 "/" 起始的消息执行指令
  • mirai-api-http 官方插件,提供 HTTP 支持,允许使用其他编程语言的插件,接口文档位于
# 下载插件<https://github.com/project-mirai/mirai-api-http/releases>,安装目录./plugin
# 执行mcl后可得到文件`config/net.mamoe.mirai-api-http/setting.yml`

  http接口文档   

# 安装插件
./mcl --update-package net.mamoe:mirai-api-http --type plugin --channel stable
# 此时plugins下面存在mirai-api-http-v2.1.0.mirai.jar  mirai-api-http-v2.1.0.mirai.sha1
# 此时config下存在net.mamoe.mirai-api-http/setting.yml,可以修改配置,笔者的文件如下:
adapters:
  - http
enableVerify: true
verifyKey: 1234
debug: true
singleMode: true
cacheSize: 4096
adapterSettings:
  http:
    host: localhost
    port: 8080
    cors: 
  • ./mcl 2021-08-03 18:09:15 I/http adapter: >>> [http adapter] is listening at http://localhost:8080 2021-08-03 18:09:15 I/Mirai HTTP API: Http api server is running out of verify mode 2021-08-03 18:09:15 I/Mirai HTTP API: adaptors: [http] 2021-08-03 18:09:15 I/Mirai HTTP API: ******************************************************** 2021-08-03 18:09:15 I/main: 2 plugin(s) enabled. ......
  •   测试http成功后即可开发python代码

    curl 127.0.0.1:8080/about
    {"code":0,"msg":"","data":{"version":"2.1.0"}}

    开发python代码

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import requests
    import time
    
    def run_mybot():
        # 如果配置中指定enableVerify为false则跳过这一步
        jres = requests.post("http://127.0.0.1:8080/verify", json={
            "verifyKey": "1234"
        }).json()
        if jres["code"] != 0:
            print("verify fail", jres)
            return
        else:
            print("verify suc")
        session = jres["session"]
        # 因为singleMode指定为true因此不需要文档中bind这一步
        jres = requests.get("http://127.0.0.1:8080/countMessage?sessionKey=" + session).json()
        print("unread", jres["data"])
    
        while True: # 1秒一读
            jres = requests.get("http://127.0.0.1:8080/fetchMessage?sessionKey=" + session + "&count=1").json()
            if jres["code"] != 0:
                print("readmsg fail", jres)
                break
            else:
                if jres["data"]:
                    print("readmsg", jres)
            time.sleep(1)
    
    run_mybot() # 此机器人循环读取消息

      执行结果(用主号在群里发了个消息)

    verify suc
    unread 0
    readmsg {'code': 0, 'msg': '', 'data': [{'type': 'GroupMessage', 'messageChain': [{'type': 'Source', 'id': 2768, 'time': 1627986816}, {'type': 'Plain', 'text': 'aaa'}], 'sender': {'id': 571652571, 'memberName': '夜华', 'specialTitle': '', 'permission': 'OWNER', 'joinTimestamp': 1627972550, 'lastSpeakTimestamp': 1627986816, 'muteTimeRemaining': 0, 'group': {'id': 72679285, 'name': '大生意', 'permission': 'MEMBER'}}}]}

    将mirai部署到服务器

      由于笔者服务器在国外,而mirai肯定也是自己模拟出一套Android设备参数去做登陆(做协议必然),所以100%引起登陆风控的,下面尝试从服务器部署mirai登陆QQ。安装好mirai执行login时,遇到如下提示   

    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] 需要滑动验证码, 请按照以下链接的步骤完成滑动验证码, 然后输入获取到的 ticket
    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] Slider captcha required. Please solve the captcha with following link. Type ticket here after completion.
    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] @see https://github.com/project-mirai/mirai-login-solver-selenium
    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] @see https://docs.mirai.mamoe.net/mirai-login-solver-selenium/
    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] 或者输入 TxCaptchaHelper 来使用 TxCaptchaHelper 完成滑动验证码
    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] Or type `TxCaptchaHelper` to resolve slider captcha with TxCaptchaHelper.apk
    2021-08-03 12:31:21 I/Bot.2587116580: [SliderCaptcha] Captcha link: https://ssl.captcha.qq.com/template/wireless_mqq_captcha.html?style=simple&aid=16&uin=保密&sid=保密&cap_cd=保密**&clientype=1&apptype=2
    12:31:21 LOGIN>

      这里提示需要完成滑块验证,然后用抓包工具把response里的ticket参数拿到并输入mirai。这里具体就不操作了。滑块完成后从若干个请求中抓到这个请求:

    https://t.captcha.qq.com/cap_union_new_verify
    {"errorCode":"0","randstr":"@VUF","ticket":"保密","errMessage":"","sess":""}

      输入后,又出现这个提示.

    2021-08-03 12:34:36 I/Bot.2587116580: [SliderCaptcha] 正在提交中...
    2021-08-03 12:34:36 I/Bot.2587116580: [SliderCaptcha] Submitting...
    2021-08-03 12:34:37 I/Bot.2587116580: [UnsafeLogin] 当前登录环境不安全,服务器要求账户认证。请在 QQ 浏览器打开 https://ti.qq.com/safe/verify?_wv=2&_wwv=128&envfrom=double-check&uin=保密&sig=保密 并完成验证后输入任意字符。
    2021-08-03 12:34:37 I/Bot.2587116580: [UnsafeLogin] Account verification required by the server. Please open https://ti.qq.com/safe/verify?_wv=2&_wwv=128&envfrom=double-check&uin=保密&sig=保密 in QQ browser and complete challenge, then type anything here to submit.

      这里提示需要完成登陆环境授权,实际上用老设备授权新设备(也就是服务器模拟出来的Android设备参数),这里笔者并没有从QQ浏览器打开,而是直接将链接发到手机QQ登陆号,直接扫码然后授权给新设备(服务器),授权完成,在mirai输入任意文本,即可登陆成功。

    2021-08-03 12:38:54 I/Bot.2587116580: [UnsafeLogin] 正在提交中...
    2021-08-03 12:38:54 I/Bot.2587116580: [UnsafeLogin] Submitting...
    2021-08-03 12:38:54 I/Bot.2587116580: Login successful

    回复

    使用道具 举报

    QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

    GMT+8, 2024-4-27 09:37 , Processed in 0.039235 second(s), 31 queries , Gzip On.

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表