{"id":15047913,"url":"https://github.com/cyanray/mirai-cpp","last_synced_at":"2025-10-04T07:31:00.409Z","repository":{"id":37454264,"uuid":"242362888","full_name":"cyanray/mirai-cpp","owner":"cyanray","description":"本项目为 mirai-api-http 的 C++ 封装，方便使用 C++ 开发基于 mirai-api-http 插件。","archived":true,"fork":false,"pushed_at":"2024-07-16T07:05:59.000Z","size":4080,"stargazers_count":147,"open_issues_count":8,"forks_count":37,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-23T04:31:33.359Z","etag":null,"topics":["cplusplus-11","mirai","mirai-api-http","mirai-bot","mirai-cpp","qqbot"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cyanray.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-22T15:17:46.000Z","updated_at":"2024-12-25T05:18:09.000Z","dependencies_parsed_at":"2025-01-23T04:31:03.747Z","dependency_job_id":"e1966982-1bdc-4ec6-a702-7bdce0364150","html_url":"https://github.com/cyanray/mirai-cpp","commit_stats":null,"previous_names":[],"tags_count":36,"template":false,"template_full_name":null,"purl":"pkg:github/cyanray/mirai-cpp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyanray%2Fmirai-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyanray%2Fmirai-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyanray%2Fmirai-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyanray%2Fmirai-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cyanray","download_url":"https://codeload.github.com/cyanray/mirai-cpp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyanray%2Fmirai-cpp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278283485,"owners_count":25961309,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-04T02:00:05.491Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cplusplus-11","mirai","mirai-api-http","mirai-bot","mirai-cpp","qqbot"],"created_at":"2024-09-24T21:05:59.687Z","updated_at":"2025-10-04T07:31:00.047Z","avatar_url":"https://github.com/cyanray.png","language":"C++","readme":"# mirai-cpp\n\n![Build](https://github.com/cyanray/mirai-cpp/workflows/Build/badge.svg)\n![](https://img.shields.io/github/license/cyanray/mirai-cpp.svg)\n![](https://img.shields.io/badge/c++-17/20-green.svg)\n## 0x01 项目简介\n\n[mirai](https://github.com/mamoe/mirai) 是全开源 QQ 机器人 / QQ 协议支持库。(使用Kotlin编写)\n\n[mirai-api-http](https://github.com/project-mirai/mirai-api-http) 提供一系列 HTTP API 以便其他语言使用 mirai。\n\n本项目为 mirai-api-http 的 C++ 封装，方便使用 C++ 开发基于 mirai-api-http 的插件。\n\n**在使用 mirai-cpp 和基于 mirai-cpp 的插件之前，你需要配置好 [mirai-console](https://github.com/mamoe/mirai-console) 并且安装 [mirai-api-http](https://github.com/project-mirai/mirai-api-http) 插件。**\n\n## 0x02 特性\n\n* C++17;\n* 内置所有依赖库;\n* 容易使用，丰富的示例；\n* 与 mirai-api-http 同步更新(使用相同版本号，保证兼容性);\n* 对 mirai-api-http 进行完全地封装(支持其所有功能、事件);\n\n## 0x03 快速入门\n\n使用 mirai-cpp 需要你熟悉 C++ 最基本的用法。\n\nmirai-cpp 的工作依赖 mirai-api-http 的 http 适配器和 ws 适配器。mirai-cpp 通过 http 进行发送消息等操作，通过 ws 接收事件和消息。\n\n下面的代码实现了一个最简单的”复读机“机器人，机器人账号收到任何好友私聊、群组消息或者临时消息，都会把同样的消息内容发送回去。\n\n```c++\n#include \u003ciostream\u003e\n// 静态链接 mirai-cpp 要在引入头文件前定义这个宏\n#define MIRAICPP_STATICLIB\n#include \u003cmirai.h\u003e\nusing namespace std;\nusing namespace Cyan;\nint main(int argc, char* argv[])\n{\n    MiraiBot bot;\n    SessionOptions opts = SessionOptions::FromCommandLine(argc, argv);\n    bot.Connect(opts);\n    cout \u003c\u003c \"Bot working...\" \u003c\u003c endl;\n    bot.On\u003cMessage\u003e(\n        [](Message m)\n        {\n            m.Reply(m.MessageChain);\n        });\n    char ch;\n    while ((ch = getchar()) != 'q');\n    bot.Disconnect();\n}\n```\n\n你需要重点关注 `MiraiBot` 类以及它的 `On\u003cT\u003e` 函数。\n\n[MiraiBot](https://github.com/cyanray/mirai-cpp/blob/master/include/mirai/mirai_bot.hpp) 类提供了诸如发送消息、获取好友列表等函数，这个函数列表可以在 [mirai-api-http](https://github.com/project-mirai/mirai-api-http/blob/master/docs/api/API.md) 的文档里查到。\n\n因为其中的函数数量比较多，而且作者精力有限，因此不提供详细的使用文档，具体如何使用请查看 [MiraiBot](https://github.com/cyanray/mirai-cpp/blob/master/include/mirai/mirai_bot.hpp) 类的注释以及 [examples](https://github.com/cyanray/mirai-cpp/tree/master/examples) 目录里的示例。\n\n你可能想了解：\n* [详细示例(简易复读机Bot)](https://github.com/cyanray/mirai-cpp/blob/master/examples/RepeatMessage.cpp)\n* [详细文档](doc/Documentation.md)\n* [如何与mirai-api-http建立连接](doc/Documentation.md#如何与mirai-api-http建立连接)\n* [如何接收、处理事件](doc/Documentation.md#如何接收、处理事件)\n* [如何发送消息、引用回复、撤回消息](doc/Documentation.md#如何发送消息、引用回复、撤回消息)\n* [如何处理失去连接的情况](doc/Documentation.md#处理失去连接的情况)\n\n## 0x04 如何编译、调试 mirai-cpp\n\n### 1. 快速尝试 (运行 examples、参与开发)\n\u003cdetails\u003e\n\n#### (1) 使用 Visual Studio\n\n1. 完整克隆/下载本仓库。\n\n2. 如图所示，使用 Visual Studio 2019/2022 直接打开这个文件夹。\n\n![使用 VS 直接打开 mirai-cpp 文件夹](./doc/pic/vs_1.png)\n\n3. 如果要尝试 examples 请确保 MIRAI_CPP_BUILD_EXAMPLES 被勾上。\n\n![项目配置](./doc/pic/vs_3.png)\n\n![勾上 MIRAI_CPP_BUILD_EXAMPLES](./doc/pic/vs_4.png)\n\n4. 由于所有的 examples 都从命令行读取配置，在调试之前需要编辑一下调试命令行。\n\n![找到设置调试配置的菜单](./doc/pic/vs_5.png)\n\n5. 根据你的 mirai-api-http 设置，填入以下配置。\n\n```json\n\"args\": [\n  \"--hostname=localhost\",\n  \"--port=8080\",\n  \"--bot-qq=123456789\",\n  \"--verify-key=VerifyKey\"\n]\n```\n\n![编辑调试命令行](./doc/pic/vs_6.png)\n\n6. 如果一切顺利，你可以直接运行我写好的示例，或者进行修改编写自己的机器人。\n\n![开始运行 examples](./doc/pic/vs_2.png)\n\n\u003c/details\u003e\n\n\n### **2. 创建自己的机器人项目**\n\n#### (1) 使用 CMake Project 模板\n\n具体参考 [mirai-cpp-template](https://github.com/cyanray/mirai-cpp-template) 的说明。\n\n\n### 3. 其他使用方式\n\n#### (1) 将程序移植、部署到 Linux 上\n\n\u003cdetails\u003e\n\n(以下内容基于 “快速尝试”，请先完成“快速尝试”。)\n\n上面的内容介绍了如何在 Windows 上开发使用 mirai-cpp 的程序，下面来介绍如何将你的程序移植到 Linux 平台，以便将程序部署到 Linux 服务器上。\n\n为了易于讲解与操作，以下内容在 **WSL** (**W**indows **S**ubsystem for **L**inux) 上进行。这里不对如何安装 WSL 进行说明，关于如何安装 WSL 还请自行查阅资料。\n\n打开在 “快速尝试” 中用到的项目。按照如图所示步骤，创建一个针对 WSL 平台的配置。因为我的 WSL 安装了 GCC 编译器，所以这里选择 **WSL-GCC-Releas**。\n\n![创建WSL-GCC平台配置1](./doc/pic/vs_3.png)\n\n![创建WSL-GCC平台配置2](./doc/pic/vs_configure_linux_project.png)\n\n如果一切顺利，等待 CMake 缓存生成成功后，即可编译出 Linux 平台的可执行文件。\n\n\u003c/details\u003e\n\n#### (2) 使用 vcpkg 安装 mirai-cpp\n\n\u003e 不推荐，因精力有限 vcpkg 的更新可能不及时\n\n要使用 vcpkg 管理 mirai-cpp，你需要将 [https://github.com/cyanray/mirai-cpp-vcpkg-port](https://github.com/cyanray/mirai-cpp-vcpkg-port) 中的 **mirai-cpp** 文件夹复制到 vcpkg 安装目录下的 **ports** 文件夹中。\n\n然后就可以使用 `./vcpkg install mirai-cpp:x86-windows mirai-cpp:x64-windows` 安装 mirai-cpp。\n\n\n## 0x05 项目依赖\n\n\u003e 本项目所有依赖都为 Header-Only 库，已经内置到本项目的 mirai/third-party 文件夹中。\n\n1. [**yhirose/cpp-httplib**](https://github.com/yhirose/cpp-httplib) A C++ header-only HTTP/HTTPS server and client library.\n\n2. [**nlohmann/json**](https://github.com/nlohmann/json) JSON for Modern C++.\n\n3. [**progschj/ThreadPool**](https://github.com/progschj/ThreadPool) A simple C++11 Thread Pool implementation.\n\n4. [**cyanray/LightWebSocketClient**](https://github.com/cyanray/LightWebSocketClient) A simple cross-platform WebSocket client.\n\n5. [**Neargye/magic_enum**](https://github.com/Neargye/magic_enum) Static reflection for enums (to string, from string, iteration) for modern C++, work with any enum type without any macro or boilerplate code.\n\n## 0x06 参与开发\n\nmirai-cpp 目前还有许多不足，欢迎提出 issues 或 pull requests。\n\n## 0x07 以下项目正在使用 mirai-cpp\n\n\u003e 如果你愿意分享你开发的程序，欢迎提出 issues 或 pull requests 在下方分享你的程序\n\n1. [MocliaParseBot](https://github.com/Moclia-Developer-Team/MocliaParseBot): 基于Mirai-Api-Http和Mirai-cpp的全平台网络服务群QQ解析项目\n2. [LGTBot-Mirai](https://github.com/Slontia/lgtbot-mirai): 基于mirai-cpp的游戏裁判机器人，可以在聊天室中开展游戏\n3. [MisakaBot](https://github.com/ChinaMisakaNetwork/MisakaBot): A QQ Bot based on mirai-cpp.\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyanray%2Fmirai-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyanray%2Fmirai-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyanray%2Fmirai-cpp/lists"}