77 lines
3.6 KiB
C++
77 lines
3.6 KiB
C++
#include "commands.h"
|
|
#include "curl/curl.h"
|
|
#include "curl/easy.h"
|
|
#include "td/telegram/td_api.h"
|
|
#include <cstring>
|
|
#include <functional>
|
|
#include <spdlog/spdlog.h>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
void cmd::handle_regular_message(context *ctx, td_api::message &msg) {
|
|
if (msg.content_->get_id() == td_api::messageText::ID) {
|
|
std::string text = static_cast<td_api::messageText&>(*msg.content_).text_->text_;
|
|
if (std::strncmp(text.c_str(), "/shorten ", 9) == 0) {
|
|
std::string_view param(text.begin() + 9, text.end());
|
|
size_t nextSpace = param.find(' ');
|
|
if (nextSpace != std::string_view::npos)
|
|
param.remove_suffix(param.size() - nextSpace);
|
|
spdlog::info("Command /shorten received with parameter '{}'", param);
|
|
|
|
// TODO check URL validity
|
|
|
|
bool result = shorten_link(std::string(param), ctx, [tg = ctx->tg, chat_id = msg.chat_id_, thread_id = msg.message_thread_id_](std::string url){
|
|
tg->send_query(td_api::make_object<td_api::sendMessage>(
|
|
chat_id, thread_id,
|
|
nullptr /*reply_to*/, nullptr /*options*/,
|
|
nullptr, // reply_markup
|
|
static_cast<td_api::object_ptr<td_api::InputMessageContent>>(td_api::make_object<td_api::inputMessageText>(
|
|
std::move(td_api::make_object<td_api::formattedText>(url, std::move(std::vector<td_api::object_ptr<td_api::textEntity>>()))),
|
|
nullptr /*link_preview_options*/, false /*clear_draft*/
|
|
))
|
|
), {});
|
|
});
|
|
if (!result) {
|
|
ctx->tg->send_query(td_api::make_object<td_api::sendMessage>(
|
|
msg.chat_id_, msg.message_thread_id_,
|
|
nullptr /*reply_to*/, nullptr /*options*/,
|
|
nullptr, // reply_markup
|
|
static_cast<td_api::object_ptr<td_api::InputMessageContent>>(td_api::make_object<td_api::inputMessageText>(
|
|
std::move(td_api::make_object<td_api::formattedText>("произошла какая-то ошибка :(", std::move(std::vector<td_api::object_ptr<td_api::textEntity>>()))),
|
|
nullptr /*link_preview_options*/, false /*clear_draft*/
|
|
))
|
|
), {});
|
|
}
|
|
} else if (std::strncmp(text.c_str(), "/shorten", 8) == 0) {
|
|
std::string textRaw("usage: /shorten <url>");
|
|
std::vector<td_api::object_ptr<td_api::textEntity>> empty;
|
|
auto text = static_cast<td_api::object_ptr<td_api::InputMessageContent>>(td_api::make_object<td_api::inputMessageText>(td_api::make_object<td_api::formattedText>(textRaw, std::move(empty)), nullptr, false));
|
|
ctx->tg->send_query(td_api::make_object<td_api::sendMessage>(msg.chat_id_, msg.message_thread_id_, nullptr, nullptr, nullptr, std::move(text)), {});
|
|
}
|
|
}
|
|
}
|
|
|
|
bool cmd::shorten_link(std::string link, context *ctx, std::function<void(std::string)> cb) {
|
|
spdlog::debug("creating CURL request");
|
|
CURL *req = curl_easy_init();
|
|
if (!req) {
|
|
return false;
|
|
}
|
|
char *escapedParam = curl_easy_escape(req, link.data(), link.size());
|
|
std::string url("https://slavasil.ru/create?url=");
|
|
url += escapedParam;
|
|
curl_free(escapedParam);
|
|
curl_easy_setopt(req, CURLOPT_URL, url.c_str());
|
|
curl_easy_setopt(req, CURLOPT_WRITEFUNCTION, curl_receive_cb);
|
|
curl_easy_setopt(req, CURLOPT_WRITEDATA, req);
|
|
curl_easy_setopt(req, CURLOPT_PRIVATE, ctx);
|
|
curl_easy_setopt(req, CURLOPT_FOLLOWLOCATION, 1);
|
|
ctx->requests.emplace(req, [cb, req, ctx](active_request &r){
|
|
std::string shortenedUrl(r.receivedData.data(), r.receivedData.size());
|
|
spdlog::info("Received data from HTTP server: {}", shortenedUrl);
|
|
cb(shortenedUrl);
|
|
ctx->requests.erase(req);
|
|
});
|
|
CURLMcode r = curl_multi_add_handle(ctx->curl, req);
|
|
return r == CURLM_OK;
|
|
} |