From 9b2e8dc472573e7f4866ab281975019795d02b05 Mon Sep 17 00:00:00 2001 From: chn Date: Thu, 23 May 2024 22:38:05 +0800 Subject: [PATCH] localPackages.hpcstat: chunk messages before sending to telegram --- local/pkgs/hpcstat/src/push.cpp | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/local/pkgs/hpcstat/src/push.cpp b/local/pkgs/hpcstat/src/push.cpp index 64d9bd11..a891f610 100644 --- a/local/pkgs/hpcstat/src/push.cpp +++ b/local/pkgs/hpcstat/src/push.cpp @@ -48,22 +48,28 @@ namespace hpcstat::push } // push to telegram for chn { - auto message = data + auto messages = data | ranges::views::filter([](const auto& pair) { return std::get<2>(pair.second) == "LNoYfq/SM7l8sFAy325WpC+li+kZl3jwST7TmP72Tz8"; }) | ranges::views::transform([](const auto& pair) { return fmt::format("{} {} {}", std::get<1>(pair.second), std::get<0>(pair.second), pair.first); }) - | ranges::views::join('\n') - | ranges::to; - if (message != "") + | ranges::views::chunk(20) + | ranges::views::transform([](auto chunk) { return chunk | ranges::views::join('\n'); }) + | ranges::to>; + if (!messages.empty()) { httplib::Client cli("https://api.chn.moe"); cli.enable_server_certificate_verification(false); - auto path = fmt::format - ("/notify.php?message={}", boost::urls::encode(message, boost::urls::unreserved_chars)); - auto res = cli.Get(path.c_str()); - if (res.error() != httplib::Error::Success) - { fmt::print("Push failed: {}\n", nameof::nameof_enum(res.error())); return false; } + for (auto& message : messages) + { + auto path = fmt::format + ("/notify.php?message={}", boost::urls::encode(message, boost::urls::unreserved_chars)); + auto res = cli.Get(path.c_str()); + if (res.error() != httplib::Error::Success) + { fmt::print("Push failed: {}\n", nameof::nameof_enum(res.error())); return false; } + else if (res->status != 200) + { fmt::print("Push failed: status code {}\n", res->status); return false; } + } } } return true;