Compare commits

..

3 Commits

Author SHA1 Message Date
94f6a58f74 slightly better error handling 2025-01-10 13:21:20 +00:00
2af5f1d00b avoid printing too much to the log 2025-01-10 13:20:58 +00:00
057ccada5e change hardcoded shortener domain name 2025-01-10 13:18:35 +00:00
2 changed files with 16 additions and 9 deletions

View File

@ -39,7 +39,7 @@ void cmd::handle_regular_message(context *ctx, td_api::message &msg) {
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>>()))),
std::move(td_api::make_object<td_api::formattedText>(!url.empty() ? url : "error :(", std::move(std::vector<td_api::object_ptr<td_api::textEntity>>()))),
nullptr /*link_preview_options*/, false /*clear_draft*/
))
), {});
@ -50,7 +50,7 @@ void cmd::handle_regular_message(context *ctx, td_api::message &msg) {
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>>()))),
std::move(td_api::make_object<td_api::formattedText>("error :(", std::move(std::vector<td_api::object_ptr<td_api::textEntity>>()))),
nullptr /*link_preview_options*/, false /*clear_draft*/
))
), {});
@ -137,7 +137,7 @@ void cmd::handle_callback_query(context *ctx, td_api::updateNewInlineCallbackQue
nullptr, // reply_markup
static_cast<td_api::object_ptr<td_api::InputMessageContent>>(td_api::make_object<td_api::inputMessageText>(
td_api::make_object<td_api::formattedText>(
url,
!url.empty() ? url : "error :(",
std::move(std::vector<td_api::object_ptr<td_api::textEntity>>())
),
td_api::make_object<td_api::linkPreviewOptions>(true, "", false, false, false),
@ -171,7 +171,7 @@ void cmd::handle_chosen_inline_result(context *ctx, td_api::updateNewChosenInlin
nullptr, // reply_markup
static_cast<td_api::object_ptr<td_api::InputMessageContent>>(td_api::make_object<td_api::inputMessageText>(
td_api::make_object<td_api::formattedText>(
url,
!url.empty() ? url : "error :(",
std::move(std::vector<td_api::object_ptr<td_api::textEntity>>())
),
td_api::make_object<td_api::linkPreviewOptions>(true, "", false, false, false),
@ -189,7 +189,7 @@ bool cmd::shorten_link(std::string link, context *ctx, std::function<void(std::s
return false;
}
char *escapedParam = curl_easy_escape(req, link.data(), link.size());
std::string url("https://slavasil.ru/create?url=");
std::string url("https://xurl.pw/create?url=");
url += escapedParam;
curl_free(escapedParam);
curl_easy_setopt(req, CURLOPT_URL, url.c_str());
@ -198,9 +198,16 @@ bool cmd::shorten_link(std::string link, context *ctx, std::function<void(std::s
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);
long statusCode;
curl_easy_getinfo(req, CURLINFO_RESPONSE_CODE, &statusCode);
if (statusCode == 200) {
std::string shortenedUrl(r.receivedData.data(), r.receivedData.size());
spdlog::info("Received data from HTTP server: {}", shortenedUrl);
cb(shortenedUrl);
} else {
spdlog::error("Shortener returned error: {}", statusCode);
cb("");
}
ctx->requests.erase(req);
});
CURLMcode r = curl_multi_add_handle(ctx->curl, req);

View File

@ -334,7 +334,7 @@ void check_curl_multi_info(context *ctx) {
}
size_t curl_receive_cb(char *ptr, size_t size, size_t nmemb, CURL *curl) {
spdlog::debug("received {} bytes from server: '{}'", nmemb, std::string(ptr, nmemb));
spdlog::debug("received {} bytes from: '{}'", nmemb, nmemb <= 50 ? std::string(ptr, nmemb) : std::string(ptr, 47)+"...");
context *ctx = nullptr;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ctx);