mirror of
https://github.com/Syping/dtranslatebot.git
synced 2024-11-22 13:50:22 +01:00
use dpp::cluster::post_rest to send Webhook request
This commit is contained in:
parent
ccd2736c63
commit
7ee916537c
5 changed files with 10 additions and 30 deletions
|
@ -21,7 +21,7 @@
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
inline bot::translated_message make_translated_message(const bot::message &message, const std::string &translated_message, const std::string &webhook)
|
inline bot::translated_message make_translated_message(const bot::message &message, const std::string &translated_message, const dpp::webhook &webhook)
|
||||||
{
|
{
|
||||||
bot::translated_message tr_message;
|
bot::translated_message tr_message;
|
||||||
tr_message.author = message.author;
|
tr_message.author = message.author;
|
||||||
|
@ -95,7 +95,7 @@ void bot::message_queue::run(bot::settings::settings *settings, bot::submit_queu
|
||||||
std::cerr << "Exception thrown while translating: unknown" << std::endl;
|
std::cerr << "Exception thrown while translating: unknown" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
submit_queue->add(make_translated_message(message, tr_message, target->webhook));
|
submit_queue->add(make_translated_message(message, tr_message, dpp::webhook(target->webhook)));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
|
|
|
@ -38,7 +38,7 @@ void bot::submit_queue::run(dpp::cluster *bot)
|
||||||
m_queue.erase(m_queue.begin());
|
m_queue.erase(m_queue.begin());
|
||||||
m_mutex.unlock();
|
m_mutex.unlock();
|
||||||
|
|
||||||
bot::webhook_push webhook_push(message.webhook, message, bot);
|
webhook_push::run(message.webhook, message, bot);
|
||||||
|
|
||||||
std::this_thread::yield();
|
std::this_thread::yield();
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace bot {
|
||||||
std::string author;
|
std::string author;
|
||||||
std::string avatar;
|
std::string avatar;
|
||||||
std::string message;
|
std::string message;
|
||||||
std::string webhook;
|
dpp::webhook webhook;
|
||||||
};
|
};
|
||||||
|
|
||||||
class submit_queue {
|
class submit_queue {
|
||||||
|
|
|
@ -18,8 +18,7 @@
|
||||||
|
|
||||||
#include "webhook_push.h"
|
#include "webhook_push.h"
|
||||||
|
|
||||||
bot::webhook_push::webhook_push(const std::string &webhook, const bot::translated_message &message, dpp::cluster *bot) :
|
void bot::webhook_push::run(const dpp::webhook &webhook, const bot::translated_message &message, dpp::cluster *bot)
|
||||||
m_message(message)
|
|
||||||
{
|
{
|
||||||
const dpp::json json_body = {
|
const dpp::json json_body = {
|
||||||
{"content", message.message},
|
{"content", message.message},
|
||||||
|
@ -28,12 +27,10 @@ bot::webhook_push::webhook_push(const std::string &webhook, const bot::translate
|
||||||
};
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
dpp::http_request webhook_request(webhook, nullptr, dpp::m_post, json_body.dump(), "application/json");
|
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) {
|
||||||
const dpp::http_request_completion_t result = webhook_request.run(bot);
|
if (event.status != 204)
|
||||||
if (result.status != 204)
|
std::cerr << "Webhook push returned unexpected code " << event.status << " with response: " << event.body << std::endl;
|
||||||
std::cerr << "Webhook push returned unexpected code " << result.status << " with response: " << result.body << std::endl;
|
});
|
||||||
m_content = result.body;
|
|
||||||
m_status = result.status;
|
|
||||||
}
|
}
|
||||||
catch (const std::exception &exception) {
|
catch (const std::exception &exception) {
|
||||||
std::cerr << "Exception thrown while Webhook push: " << exception.what() << std::endl;
|
std::cerr << "Exception thrown while Webhook push: " << exception.what() << std::endl;
|
||||||
|
@ -42,13 +39,3 @@ bot::webhook_push::webhook_push(const std::string &webhook, const bot::translate
|
||||||
std::cerr << "Exception thrown while Webhook push: unknown" << std::endl;
|
std::cerr << "Exception thrown while Webhook push: unknown" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string bot::webhook_push::get_content() const
|
|
||||||
{
|
|
||||||
return m_content;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint16_t bot::webhook_push::get_status() const
|
|
||||||
{
|
|
||||||
return m_status;
|
|
||||||
}
|
|
||||||
|
|
|
@ -25,14 +25,7 @@
|
||||||
namespace bot {
|
namespace bot {
|
||||||
class webhook_push {
|
class webhook_push {
|
||||||
public:
|
public:
|
||||||
explicit webhook_push(const std::string &webhook, const bot::translated_message &message, dpp::cluster *bot);
|
static void run(const dpp::webhook &webhook, const bot::translated_message &message, dpp::cluster *bot);
|
||||||
const std::string get_content() const;
|
|
||||||
uint16_t get_status() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string m_content;
|
|
||||||
uint16_t m_status;
|
|
||||||
bot::translated_message m_message;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue