From 7700bd7b26ae6435a04bf79a84b29e8bcbafed68 Mon Sep 17 00:00:00 2001 From: Syping Date: Sat, 20 Jan 2024 22:39:23 +0100 Subject: [PATCH] added message splitting --- src/message_queue.cpp | 2 +- src/submit_queue.cpp | 2 +- src/webhook_push.cpp | 73 +++++++++++++++++++++++++++++++++++-------- src/webhook_push.h | 8 +++-- 4 files changed, 68 insertions(+), 17 deletions(-) diff --git a/src/message_queue.cpp b/src/message_queue.cpp index ccb3ec5..4501698 100644 --- a/src/message_queue.cpp +++ b/src/message_queue.cpp @@ -66,7 +66,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu }; if (!tr_apiKey.empty()) - json_body.emplace("apiKey", tr_apiKey); + json_body["apiKey"] = tr_apiKey; const dpp::http_headers http_headers = { {"Content-Type", "application/json"} diff --git a/src/submit_queue.cpp b/src/submit_queue.cpp index 4416445..5a78013 100644 --- a/src/submit_queue.cpp +++ b/src/submit_queue.cpp @@ -38,7 +38,7 @@ void bot::submit_queue::run(dpp::cluster *bot) m_queue.erase(m_queue.begin()); m_mutex.unlock(); - webhook_push::run(message.webhook, message, bot); + webhook_push::run(message, bot); std::this_thread::yield(); } diff --git a/src/webhook_push.cpp b/src/webhook_push.cpp index 11a80ed..ccdc247 100644 --- a/src/webhook_push.cpp +++ b/src/webhook_push.cpp @@ -16,26 +16,73 @@ * responsible for anything with use of the software, you are self responsible. *****************************************************************************/ +#include +#include #include "webhook_push.h" -void bot::webhook_push::run(const dpp::webhook &webhook, const bot::translated_message &message, dpp::cluster *bot) +void bot::webhook_push::run(const bot::translated_message &message, dpp::cluster *bot) { - const dpp::json json_body = { - {"content", message.message}, + dpp::json json_body = { {"username", message.author}, {"avatar_url", message.avatar} }; - try { - bot->post_rest(API_PATH "/webhooks", std::to_string(webhook.id), dpp::utility::url_encode(webhook.token), dpp::m_post, json_body.dump(), [bot](dpp::json &json, const dpp::http_request_completion_t &event) { - if (event.status != 204) - std::cerr << "Webhook push returned unexpected code " << event.status << " with response: " << event.body << std::endl; - }); + // We will split too long messages into multiple messages + if (message.message.length() > 2000) { + std::string_view message_v = message.message; + while (!message_v.empty()) { + const std::string_view message_eov = message_v.substr(1333, 666); + const std::string_view::size_type pos = message_eov.rfind('\n'); + if (pos != std::string_view::npos) { + json_body["content"] = message_v.substr(0, 1333 + pos); + push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot); + message_v = message_v.substr(1333 + pos); + } + else { + std::cmatch match; + const std::regex eos_regex("^.*(\\.|\\?|\\!)\\s.*$"); + const std::regex eop_regex("^.*(\\,)\\s.*$"); + const std::regex eow_regex("^.*()\\s.*$"); + if (std::regex_match(message_eov.begin(), message_eov.end(), match, eos_regex)) { + json_body["content"] = message_v.substr(0, 1334 + match.position(1)); + message_v = message_v.substr(1334 + match.position(1)); + } + else if (std::regex_match(message_eov.begin(), message_eov.end(), match, eop_regex)) { + json_body["content"] = message_v.substr(0, 1334 + match.position(1)); + message_v = message_v.substr(1334 + match.position(1)); + } + else if (std::regex_match(message_eov.begin(), message_eov.end(), match, eow_regex)) { + json_body["content"] = message_v.substr(0, 1334 + match.position(1)); + message_v = message_v.substr(1334 + match.position(1)); + } + else { + json_body["content"] = message_v.substr(0, 1333); + message_v = message_v.substr(1333); + } + push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot); + } + if (message_v.length() <= 2000) { + json_body["content"] = message_v; + push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot); + message_v = std::string_view(); + } + } } - catch (const std::exception &exception) { - std::cerr << "Exception thrown while Webhook push: " << exception.what() << std::endl; - } - catch (...) { - std::cerr << "Exception thrown while Webhook push: unknown" << std::endl; + else { + json_body["content"] = message.message; + push_request(message.webhook.id, message.webhook.token, json_body.dump(), bot); } } + +void bot::webhook_push::push_request(dpp::snowflake webhook_id, const std::string &webhook_token, const std::string &json, dpp::cluster *bot) +{ + + std::promise _p; + std::future _f = _p.get_future(); + bot->post_rest(API_PATH "/webhooks", std::to_string(webhook_id), dpp::utility::url_encode(webhook_token), dpp::m_post, json, [bot, &_p](dpp::json &json, const dpp::http_request_completion_t &event) { + if (event.status != 204) + std::cerr << "Webhook push returned unexpected code " << event.status << " with response: " << event.body << std::endl; + _p.set_value(event); + }); + _f.wait(); +} diff --git a/src/webhook_push.h b/src/webhook_push.h index 6293d50..643b20d 100644 --- a/src/webhook_push.h +++ b/src/webhook_push.h @@ -19,13 +19,17 @@ #ifndef WEBHOOK_PUSH_H #define WEBHOOK_PUSH_H -#include +#include +#include #include "submit_queue.h" namespace bot { class webhook_push { public: - static void run(const dpp::webhook &webhook, const bot::translated_message &message, dpp::cluster *bot); + static void run(const bot::translated_message &message, dpp::cluster *bot); + + private: + static void push_request(dpp::snowflake webhook_id, const std::string &webhook_token, const std::string &json, dpp::cluster *bot); }; }