added message splitting

This commit is contained in:
Syping 2024-01-20 22:39:23 +01:00
parent 06dec819ff
commit 7700bd7b26
4 changed files with 68 additions and 17 deletions

View file

@ -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"}

View file

@ -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();
}

View file

@ -16,26 +16,73 @@
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <future>
#include <regex>
#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<dpp::http_request_completion_t> _p;
std::future<dpp::http_request_completion_t> _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();
}

View file

@ -19,13 +19,17 @@
#ifndef WEBHOOK_PUSH_H
#define WEBHOOK_PUSH_H
#include <dpp/dpp.h>
#include <dpp/cluster.h>
#include <dpp/snowflake.h>
#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);
};
}