这是为 go-cqhttp 开发的第三方 C++ 库,封装了 Bot 账号,好友信息等 API。目前在 Ubuntu22.04 下测试通过,其他平台目前未测试。
以CMake为例,假设项目目录结构如下:
project example
├── CMakeLists.txt
├── build
├── include
│ └── cqhttp-cpplib <--本项目
└── src
└── main.cpp
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.5)
project(example)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(SOURCES src/main.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE include)
# 添加cqhttp-cpplib
add_subdirectory(include/cqhttp-cpplib)
target_link_libraries(${PROJECT_NAME} PRIVATE cqhttp-cpplib)
src/main.cpp
:
#include "cqhttp-cpplib/cqhttp.hpp"
#include <iostream>
#define LISTEN_PORT 5701 // 监听端口,即go-cqhttp的Post端口
#define POST_PORT 5700 // Post端口,即go-cqhttp的监听端口
#define IP "127.0.0.1" // go-cqhttp所在的ip地址
int main() {
cqhttp::Bot myBot(IP, LISTEN_PORT, POST_PORT);
//获取登录账号的信息
auto resp = myBot.getLoginInfo();
// resp 类型为 Response*
if (resp->valid) {
std::cout << *resp << std::endl;
}
//接受消息
auto msg = myBot.receive(); // 阻塞,直到监听端口收到消息
// msg 类型为 ListenMsg*
// 消息内容
std::cout << msg->content << std::endl;
// 发送者昵称
std::cout << msg->senderName << std::endl;
return 0;
}
编译并运行得到输出:
example/build❯ ./example
[2023-09-18 16:08:24.857] [Bot] [info] Bot Starting Up
[2023-09-18 16:08:24.858] [Bot] [info] Post收到有效响应
{
"nickname": "Hadeon-qwen",
"user_id": 1234567890
}
[2023-09-18 16:08:49.013] [Bot] [info] 收到好友Hadeon的私聊消息:message receive example🤔
message receive example🤔
Hadeon
[2023-09-18 16:08:49.013] [Bot] [info] Bot Shutting Down
封装的函数有两种传参方式:
- 通过json格式的param传参:
cqhttp::param params = {
{"key1", "val1"},
{"key2", "val2"},
...
};
myBot.postFunc(params);
cqhttp::param
的本质是 nlohmann::json
,更多初始化和参数操作方式请参考项目nlohmann/json
- 通过封装好的重载传参:
cqhttp::value_t val1 = "value1";
cqhttp::value_t val2 = "value2";
...
myBot.postFunc(val1, val2, ...);
具体参数和返回参考go-cqhttp帮助文档
(该API没有参数)
auto resp = myBot.getLoginInfo();
auto resp =
myBot.setProfile("NickName", "company",
"hadeon@qq.com", "XJTU",
"a qq bot");
// 或者:
cqhttp::param params = {
{"nickname", "NickName"},
{"company", "company"},
{"email", "hadeon@qq.com"},
{"college", "XJTU"},
{"personal_note", "a qq bot"},
};
auto resp = myBot.setProfile(params);
(该API只有企点协议可用,无参数)
auto resp = myBot.qidianAccountInfo();
auto resp = myBot.getModel("string");
// 或者:
cqhttp::param params = {
{"model", "string"},
};
auto resp = myBot.getModel(params);
auto resp = myBot.setModel(
"string",
"string"
);
// 或者:
cqhttp::param params = {
{"model", "string"},
{"model_show", "string"},
};
auto resp = myBot.setModel(params);
auto resp = myBot.getOnlineClients("true");
// 或者:
cqhttp::param params = {
{"no_cache", "false"},
};
auto resp = myBot.getOnlineClient(params);
auto resp = myBot.getAccountInfo("1234567890", "true");
// 或者:
auto resp = myBot.getAccountInfo("1234567890");
// 或者:
cqhttp::param params = {
{"user_id", "1234567890"},
{"no_cache", "false"},
};
auto resp = myBot.getAccountInfo(params);
(该API没有参数)
auto resp = myBot.getFriendList();
(该API没有参数)
auto resp = myBot.getUnidrectionalList();
auto resp = myBot.deleteFriend("1234567890");
// 或者:
cqhttp::param params = {
{"user_id", "1234567890"},
};
auto resp = myBot.deleteFriend(params);
auto resp = myBot.deleteUnidrectional("1234567890");
// 或者:
cqhttp::param params = {
{"user_id", "1234567890"},
};
auto resp = myBot.deleteUnidrectional(params);
cqhttp::value_t id = "1234567890", message = "a message";
auto resp = myBot.sendPrivateMsg(id, message);
// 或者:
cqhttp::param params = {
{"user_id", "1234567890"},
{"message", "a message"},
};
auto resp = myBot.sendPrivateMsg(params);
//通过群聊发起临时会话:
cqhttp::value_t userId_str, groupId_str, message;
auto resp = myBot.sendPrivateMsg(userId_str, groupId_str, message);
cqhttp::value_t groupId = "1234567890", message = "a message";
auto resp = myBot.sendGroupMsg(groupId, message);
// 或者:
cqhttp::param params = {
{"group_id", "1234567890"},
{"message", "a message"},
};
auto resp = myBot.sendGroupMsg(params);
auto resp = myBot.sendMsg(PRIVATE_MSG, "1234567890", "I'm a bot");
// 或者
cqhttp::param params = {
{"user_id", "1234567890"},
{"message", "I'm a bot"},
};
resp = myBot.sendMsg(params);
auto resp = myBot.sendMsg(GROUP_MSG, "1234567890", "I'm a bot");
// 或者
cqhttp::param params = {
{"group_id", "1234567890"},
{"message", "I'm a bot"},
};
resp = myBot.sendMsg(params);
auto msg = myBot.receive(); // 接收消息
cqhttp::value_t msgId = msg->msgId_str; // 获取接受消息的msgId
auto resp = myBot.getMsgInfo(msgId);
// 或者:
cqhttp::param params = {
{"message_id", msgId},
};
resp = getMsgInfo(params);
auto resp = myBot.deleteMsg(msgId);
// 或者:
cqhttp::param params = {
{"message_id", msgId},
};
resp = deleteMsg(params);
auto resp = myBot.markMsgRead(msgId);
// 或者:
cqhttp::param params = {
{"message_id", msgId},
};
resp = markMsgRead(params);