Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
$<INSTALL_INTERFACE:include>
)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/third-party>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/mirai-third-party>
$<INSTALL_INTERFACE:third-party>
)

Expand Down Expand Up @@ -63,7 +63,7 @@ if(MIRAI_CPP_INSTALL)
)

install(
DIRECTORY third-party
DIRECTORY mirai-third-party
DESTINATION ${CMAKE_INSTALL_PREFIX}
)

Expand Down
57 changes: 57 additions & 0 deletions examples/FetchEventsViahHTTP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
// 使用静态库必须要在引入 mirai.h 前定义这个宏
#define MIRAICPP_STATICLIB
#include <mirai.h>

int main()
{
using namespace std;
using namespace Cyan;
system("chcp 65001");
MiraiBot bot("127.0.0.1", 539);
while (true)
{
try
{
bot.Auth("INITKEY7A3O1a9v", 1589588851_qq);
break;
}
catch (const std::exception& ex)
{
cout << ex.what() << endl;
}
MiraiBot::SleepSeconds(1);
}
cout << "成功登录 bot。" << endl;

bot.OnEventReceived<GroupMessage>(
[&](GroupMessage gm)
{
gm.QuoteReply(gm.MessageChain);
});

bot.OnEventReceived<FriendMessage>(
[&](FriendMessage fm)
{
fm.Reply("你好呀, " + fm.MessageChain);
});

bot.OnEventReceived<TempMessage>(
[&](TempMessage tm)
{
tm.Reply(tm.MessageChain);
});

// 默认使用 WebSocket 拉取事件、消息
// 如果要使用 HTTP 可以在 EventLoop 前执行 UseHTTP
// 记录轮询事件时的错误
bot.UseHTTP().EventLoop([](const char* errMsg)
{
cout << "轮询事件时出错: " << errMsg << endl;
});

// 默认参数是在 cerr 输出错误
// bot.EventLoop();

return 0;
}
43 changes: 39 additions & 4 deletions include/mirai_bot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,22 @@ namespace Cyan
class EXPORTED MiraiBot
{
public:
MiraiBot() :qq_(0), pool_(4), http_client_("localhost", 8080) {}
MiraiBot(const string& host, int port) : qq_(0), pool_(4), http_client_(host, port) {}
MiraiBot() :
qq_(0),
pool_(4),
http_client_("localhost", 8080),
host_("localhost"),
port_(8080),
cacheSize_(4096),
ws_enabled_(true) {}
MiraiBot(const string& host, int port) :
qq_(0),
pool_(4),
http_client_(host, port),
host_(host),
port_(port),
cacheSize_(4096),
ws_enabled_(true) {}
~MiraiBot()
{
Release();
Expand Down Expand Up @@ -112,12 +126,29 @@ namespace Cyan
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}

MiraiBot& UseWebSocket()
{
this->ws_enabled_ = true;
SessionConfigure(cacheSize_, ws_enabled_);
return *this;
}

MiraiBot& UseHTTP()
{
this->ws_enabled_ = false;
SessionConfigure(cacheSize_, ws_enabled_);
return *this;
}

void EventLoop(function<void(const char*)> errLogger = nullptr);

private:
bool SessionVerify();
bool SessionRelease();
unsigned int FetchMessagesAndEvents(unsigned int count = 10);
bool SessionConfigure(int cacheSize, bool enableWebsocket);
unsigned int FetchEvents_HTTP(unsigned int count = 10);
void FetchEvents_WS();
void ProcessEvents(const nlohmann::json& ele);
template<typename T>
inline WeakEvent MakeWeakEvent(const json& json_)
{
Expand All @@ -139,7 +170,7 @@ namespace Cyan
}

}

// 因为 httplib 使用 string 来保存文件内容,这里适配一下
inline string ReadFile(const string& filename)
{
Expand All @@ -156,6 +187,10 @@ namespace Cyan
string authKey_;
QQ_t qq_;
string sessionKey_;
string host_;
int port_;
int cacheSize_;
bool ws_enabled_;
httplib::Client http_client_;
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
ThreadPool pool_;
Expand Down
File renamed without changes.
Loading