From 6e5d607b905578f557af855dc465306eaa3fd241 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 27 Aug 2024 15:55:54 +0300 Subject: [PATCH 01/13] Add and use Client::is_special_error_code. --- telegram-bot-api/Client.cpp | 42 ++++++++++++++++++++----------------- telegram-bot-api/Client.h | 2 ++ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 86bd10d..87c63f3 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -39,6 +39,24 @@ namespace telegram_bot_api { using td_api::make_object; using td_api::move_object_as; +Client::Client(td::ActorShared<> parent, const td::string &bot_token, bool is_test_dc, int64 tqueue_id, + std::shared_ptr parameters, td::ActorId stat_actor) + : parent_(std::move(parent)) + , bot_token_(bot_token) + , bot_token_id_("") + , is_test_dc_(is_test_dc) + , tqueue_id_(tqueue_id) + , parameters_(std::move(parameters)) + , stat_actor_(std::move(stat_actor)) { + static auto is_inited = init_methods(); + CHECK(is_inited); +} + +Client::~Client() { + td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), messages_, users_, groups_, + supergroups_, chats_, sticker_set_names_); +} + int Client::get_retry_after_time(td::Slice error_message) { td::Slice prefix = "Too Many Requests: retry after "; if (td::begins_with(error_message, prefix)) { @@ -179,22 +197,8 @@ void Client::fail_query_with_error(PromisedQueryPtr &&query, object_ptrcode_, error->message_, default_message); } -Client::Client(td::ActorShared<> parent, const td::string &bot_token, bool is_test_dc, int64 tqueue_id, - std::shared_ptr parameters, td::ActorId stat_actor) - : parent_(std::move(parent)) - , bot_token_(bot_token) - , bot_token_id_("") - , is_test_dc_(is_test_dc) - , tqueue_id_(tqueue_id) - , parameters_(std::move(parameters)) - , stat_actor_(std::move(stat_actor)) { - static auto is_inited = init_methods(); - CHECK(is_inited); -} - -Client::~Client() { - td::Scheduler::instance()->destroy_on_scheduler(SharedData::get_file_gc_scheduler_id(), messages_, users_, groups_, - supergroups_, chats_, sticker_set_names_); +bool Client::is_special_error_code(int32 error_code) { + return error_code == 401 || error_code == 429 || error_code >= 500; } bool Client::init_methods() { @@ -5281,7 +5285,7 @@ class Client::TdOnGetChatPinnedMessageCallback final : public TdQueryCallback { int64 pinned_message_id = 0; if (result->get_id() == td_api::error::ID) { auto error = move_object_as(result); - if (error->code_ == 429) { + if (is_special_error_code(error->code_)) { return fail_query_with_error(std::move(query_), std::move(error)); } else if (error->code_ != 404 && error->message_ != "CHANNEL_PRIVATE") { LOG(ERROR) << "Failed to get chat pinned message: " << to_string(error); @@ -5349,7 +5353,7 @@ class Client::TdOnGetChatPinnedMessageToUnpinCallback final : public TdQueryCall int64 pinned_message_id = 0; if (result->get_id() == td_api::error::ID) { auto error = move_object_as(result); - if (error->code_ == 429) { + if (is_special_error_code(error->code_)) { return fail_query_with_error(std::move(query_), std::move(error)); } else { return fail_query_with_error(std::move(query_), 400, "Message to unpin not found"); @@ -9959,7 +9963,7 @@ void Client::on_message_send_failed(int64 chat_id, int64 old_message_id, int64 n auto &query = *pending_send_message_queries_[query_id]; if (query.is_multisend) { if (query.error == nullptr || query.error->message_ == "Group send failed") { - if (error->code_ == 401 || error->code_ == 429 || error->code_ >= 500 || error->message_ == "Group send failed") { + if (is_special_error_code(error->code_) || error->message_ == "Group send failed") { query.error = std::move(error); } else { auto pos = (query.total_message_count - query.awaited_message_count + 1); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 3fef529..bc12bcb 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -810,6 +810,8 @@ class Client final : public WebhookActor::Callback { static void fail_query_with_error(PromisedQueryPtr &&query, object_ptr error, td::Slice default_message = td::Slice()); + static bool is_special_error_code(int32 error_code); + class JsonUpdates; void do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQueryPtr query); From 240eb7b4a909458bbec116af5d712905a74d2884 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 27 Aug 2024 17:42:44 +0300 Subject: [PATCH 02/13] Update TDLib to 1.8.36. --- td | 2 +- telegram-bot-api/Client.cpp | 86 +++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/td b/td index 8d08b34..87d8810 160000 --- a/td +++ b/td @@ -1 +1 @@ -Subproject commit 8d08b34e22a08e58db8341839c4e18ee06c516c5 +Subproject commit 87d881071fe514936bb17029e96761141287d2be diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 87c63f3..dd31e7f 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2421,8 +2421,7 @@ class Client::JsonChatShared final : public td::Jsonable { class Client::JsonGiveaway final : public td::Jsonable { public: - JsonGiveaway(const td_api::messagePremiumGiveaway *giveaway, const Client *client) - : giveaway_(giveaway), client_(client) { + JsonGiveaway(const td_api::messageGiveaway *giveaway, const Client *client) : giveaway_(giveaway), client_(client) { } void store(td::JsonValueScope *scope) const { auto object = scope->enter_object(); @@ -2447,19 +2446,29 @@ class Client::JsonGiveaway final : public td::Jsonable { if (!giveaway_->parameters_->prize_description_.empty()) { object("prize_description", giveaway_->parameters_->prize_description_); } - if (giveaway_->month_count_ > 0) { - object("premium_subscription_month_count", giveaway_->month_count_); + switch (giveaway_->prize_->get_id()) { + case td_api::giveawayPrizePremium::ID: { + auto month_count = static_cast(giveaway_->prize_.get())->month_count_; + if (month_count > 0) { + object("premium_subscription_month_count", month_count); + } + break; + } + case td_api::giveawayPrizeStars::ID: + break; + default: + UNREACHABLE(); } } private: - const td_api::messagePremiumGiveaway *giveaway_; + const td_api::messageGiveaway *giveaway_; const Client *client_; }; class Client::JsonGiveawayWinners final : public td::Jsonable { public: - JsonGiveawayWinners(const td_api::messagePremiumGiveawayWinners *giveaway_winners, const Client *client) + JsonGiveawayWinners(const td_api::messageGiveawayWinners *giveaway_winners, const Client *client) : giveaway_winners_(giveaway_winners), client_(client) { } void store(td::JsonValueScope *scope) const { @@ -2476,8 +2485,19 @@ class Client::JsonGiveawayWinners final : public td::Jsonable { if (giveaway_winners_->was_refunded_) { object("was_refunded", td::JsonTrue()); } - if (giveaway_winners_->month_count_ > 0) { - object("premium_subscription_month_count", giveaway_winners_->month_count_); + switch (giveaway_winners_->prize_->get_id()) { + case td_api::giveawayPrizePremium::ID: { + auto month_count = + static_cast(giveaway_winners_->prize_.get())->month_count_; + if (month_count > 0) { + object("premium_subscription_month_count", month_count); + } + break; + } + case td_api::giveawayPrizeStars::ID: + break; + default: + UNREACHABLE(); } if (!giveaway_winners_->prize_description_.empty()) { object("prize_description", giveaway_winners_->prize_description_); @@ -2490,14 +2510,13 @@ class Client::JsonGiveawayWinners final : public td::Jsonable { } private: - const td_api::messagePremiumGiveawayWinners *giveaway_winners_; + const td_api::messageGiveawayWinners *giveaway_winners_; const Client *client_; }; class Client::JsonGiveawayCompleted final : public td::Jsonable { public: - JsonGiveawayCompleted(const td_api::messagePremiumGiveawayCompleted *giveaway_completed, int64 chat_id, - const Client *client) + JsonGiveawayCompleted(const td_api::messageGiveawayCompleted *giveaway_completed, int64 chat_id, const Client *client) : giveaway_completed_(giveaway_completed), chat_id_(chat_id), client_(client) { } void store(td::JsonValueScope *scope) const { @@ -2514,7 +2533,7 @@ class Client::JsonGiveawayCompleted final : public td::Jsonable { } private: - const td_api::messagePremiumGiveawayCompleted *giveaway_completed_; + const td_api::messageGiveawayCompleted *giveaway_completed_; int64 chat_id_; const Client *client_; }; @@ -2767,13 +2786,13 @@ class Client::JsonExternalReplyInfo final : public td::Jsonable { } case td_api::messageUnsupported::ID: break; - case td_api::messagePremiumGiveaway::ID: { - auto content = static_cast(reply_->content_.get()); + case td_api::messageGiveaway::ID: { + auto content = static_cast(reply_->content_.get()); object("giveaway", JsonGiveaway(content, client_)); break; } - case td_api::messagePremiumGiveawayWinners::ID: { - auto content = static_cast(reply_->content_.get()); + case td_api::messageGiveawayWinners::ID: { + auto content = static_cast(reply_->content_.get()); object("giveaway_winners", JsonGiveawayWinners(content, client_)); break; } @@ -3271,21 +3290,21 @@ void Client::JsonMessage::store(td::JsonValueScope *scope) const { } case td_api::messagePremiumGiftCode::ID: break; - case td_api::messagePremiumGiveawayCreated::ID: + case td_api::messageGiveawayCreated::ID: object("giveaway_created", JsonEmptyObject()); break; - case td_api::messagePremiumGiveaway::ID: { - auto content = static_cast(message_->content.get()); + case td_api::messageGiveaway::ID: { + auto content = static_cast(message_->content.get()); object("giveaway", JsonGiveaway(content, client_)); break; } - case td_api::messagePremiumGiveawayWinners::ID: { - auto content = static_cast(message_->content.get()); + case td_api::messageGiveawayWinners::ID: { + auto content = static_cast(message_->content.get()); object("giveaway_winners", JsonGiveawayWinners(content, client_)); break; } - case td_api::messagePremiumGiveawayCompleted::ID: { - auto content = static_cast(message_->content.get()); + case td_api::messageGiveawayCompleted::ID: { + auto content = static_cast(message_->content.get()); object("giveaway_completed", JsonGiveawayCompleted(content, message_->chat_id, client_)); break; } @@ -3301,6 +3320,8 @@ void Client::JsonMessage::store(td::JsonValueScope *scope) const { } case td_api::messageGiftedStars::ID: break; + case td_api::messageGiveawayPrizeStars::ID: + break; default: UNREACHABLE(); } @@ -4142,7 +4163,7 @@ class Client::JsonStarTransactionPartner final : public td::Jsonable { case td_api::starTransactionPartnerGooglePlay::ID: case td_api::starTransactionPartnerUser::ID: case td_api::starTransactionPartnerBusiness::ID: - case td_api::starTransactionPartnerChannel::ID: + case td_api::starTransactionPartnerChat::ID: LOG(ERROR) << "Receive " << to_string(*source_); object("type", "other"); break; @@ -10362,7 +10383,7 @@ td::Status Client::process_send_paid_media_query(PromisedQueryPtr &query) { TRY_RESULT(caption, get_caption(query.get())); auto show_caption_above_media = to_bool(query->arg("show_caption_above_media")); do_send_message(make_object(star_count, std::move(paid_media), std::move(caption), - show_caption_above_media), + show_caption_above_media, td::string()), std::move(query)); return td::Status::OK(); } @@ -13758,10 +13779,10 @@ bool Client::need_skip_update_message(int64 chat_id, const object_ptr(message->content_.get()) ->old_background_message_id_; - case td_api::messagePremiumGiveawayCompleted::ID: - return static_cast(message->content_.get()) - ->giveaway_message_id_; + case td_api::messageGiveawayCompleted::ID: + return static_cast(message->content_.get())->giveaway_message_id_; case td_api::messagePaymentSuccessful::ID: UNREACHABLE(); return static_cast(0); From c571a9ee5b9c0152b140f38e03f93490a307dfb0 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 11:54:13 +0300 Subject: [PATCH 03/13] Use td_api::getStickerSetName instead of td_api::getStickerSet. --- telegram-bot-api/Client.cpp | 110 +++++++++++++++++++++--------------- telegram-bot-api/Client.h | 2 +- 2 files changed, 65 insertions(+), 47 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index dd31e7f..cc3e12d 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -5182,10 +5182,10 @@ class Client::TdOnGetStickerSetCallback final : public TdQueryCallback { nullptr); } - CHECK(result->get_id() == td_api::stickerSet::ID); + CHECK(result->get_id() == td_api::text::ID); client_->on_get_sticker_set(set_id_, new_callback_query_user_id_, new_message_chat_id_, new_message_business_connection_id_, new_business_callback_query_user_id_, - move_object_as(result)); + move_object_as(result)); } private: @@ -5199,9 +5199,13 @@ class Client::TdOnGetStickerSetCallback final : public TdQueryCallback { class Client::TdOnGetChatCustomEmojiStickerSetCallback final : public TdQueryCallback { public: - TdOnGetChatCustomEmojiStickerSetCallback(Client *client, int64 chat_id, int64 pinned_message_id, + TdOnGetChatCustomEmojiStickerSetCallback(Client *client, int64 sticker_set_id, int64 chat_id, int64 pinned_message_id, PromisedQueryPtr query) - : client_(client), chat_id_(chat_id), pinned_message_id_(pinned_message_id), query_(std::move(query)) { + : client_(client) + , sticker_set_id_(sticker_set_id) + , chat_id_(chat_id) + , pinned_message_id_(pinned_message_id) + , query_(std::move(query)) { } void on_result(object_ptr result) final { @@ -5212,9 +5216,9 @@ class Client::TdOnGetChatCustomEmojiStickerSetCallback final : public TdQueryCal if (result->get_id() == td_api::error::ID) { supergroup_info->custom_emoji_sticker_set_id = 0; } else { - CHECK(result->get_id() == td_api::stickerSet::ID); - auto sticker_set = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set->id_, sticker_set->name_); + CHECK(result->get_id() == td_api::text::ID); + auto sticker_set_name = move_object_as(result); + client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); } answer_query(JsonChat(chat_id_, client_, true, pinned_message_id_), std::move(query_)); @@ -5222,6 +5226,7 @@ class Client::TdOnGetChatCustomEmojiStickerSetCallback final : public TdQueryCal private: Client *client_; + int64 sticker_set_id_; int64 chat_id_; int64 pinned_message_id_; PromisedQueryPtr query_; @@ -5229,9 +5234,13 @@ class Client::TdOnGetChatCustomEmojiStickerSetCallback final : public TdQueryCal class Client::TdOnGetChatBusinessStartPageStickerSetCallback final : public TdQueryCallback { public: - TdOnGetChatBusinessStartPageStickerSetCallback(Client *client, int64 chat_id, int64 pinned_message_id, - PromisedQueryPtr query) - : client_(client), chat_id_(chat_id), pinned_message_id_(pinned_message_id), query_(std::move(query)) { + TdOnGetChatBusinessStartPageStickerSetCallback(Client *client, int64 sticker_set_id, int64 chat_id, + int64 pinned_message_id, PromisedQueryPtr query) + : client_(client) + , sticker_set_id_(sticker_set_id) + , chat_id_(chat_id) + , pinned_message_id_(pinned_message_id) + , query_(std::move(query)) { } void on_result(object_ptr result) final { @@ -5245,9 +5254,9 @@ class Client::TdOnGetChatBusinessStartPageStickerSetCallback final : public TdQu user_info->business_info->start_page_->sticker_->set_id_ = 0; } } else { - CHECK(result->get_id() == td_api::stickerSet::ID); - auto sticker_set = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set->id_, sticker_set->name_); + CHECK(result->get_id() == td_api::text::ID); + auto sticker_set_name = move_object_as(result); + client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); } answer_query(JsonChat(chat_id_, client_, true, pinned_message_id_), std::move(query_)); @@ -5255,6 +5264,7 @@ class Client::TdOnGetChatBusinessStartPageStickerSetCallback final : public TdQu private: Client *client_; + int64 sticker_set_id_; int64 chat_id_; int64 pinned_message_id_; PromisedQueryPtr query_; @@ -5262,8 +5272,13 @@ class Client::TdOnGetChatBusinessStartPageStickerSetCallback final : public TdQu class Client::TdOnGetChatStickerSetCallback final : public TdQueryCallback { public: - TdOnGetChatStickerSetCallback(Client *client, int64 chat_id, int64 pinned_message_id, PromisedQueryPtr query) - : client_(client), chat_id_(chat_id), pinned_message_id_(pinned_message_id), query_(std::move(query)) { + TdOnGetChatStickerSetCallback(Client *client, int64 sticker_set_id, int64 chat_id, int64 pinned_message_id, + PromisedQueryPtr query) + : client_(client) + , sticker_set_id_(sticker_set_id) + , chat_id_(chat_id) + , pinned_message_id_(pinned_message_id) + , query_(std::move(query)) { } void on_result(object_ptr result) final { @@ -5274,16 +5289,16 @@ class Client::TdOnGetChatStickerSetCallback final : public TdQueryCallback { if (result->get_id() == td_api::error::ID) { supergroup_info->sticker_set_id = 0; } else { - CHECK(result->get_id() == td_api::stickerSet::ID); - auto sticker_set = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set->id_, sticker_set->name_); + CHECK(result->get_id() == td_api::text::ID); + auto sticker_set_name = move_object_as(result); + client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); } auto sticker_set_id = supergroup_info->custom_emoji_sticker_set_id; if (sticker_set_id != 0 && client_->get_sticker_set_name(sticker_set_id).empty()) { - return client_->send_request(make_object(sticker_set_id), + return client_->send_request(make_object(sticker_set_id), td::make_unique( - client_, chat_id_, pinned_message_id_, std::move(query_))); + client_, sticker_set_id, chat_id_, pinned_message_id_, std::move(query_))); } answer_query(JsonChat(chat_id_, client_, true, pinned_message_id_), std::move(query_)); @@ -5291,6 +5306,7 @@ class Client::TdOnGetChatStickerSetCallback final : public TdQueryCallback { private: Client *client_; + int64 sticker_set_id_; int64 chat_id_; int64 pinned_message_id_; PromisedQueryPtr query_; @@ -5327,16 +5343,16 @@ class Client::TdOnGetChatPinnedMessageCallback final : public TdQueryCallback { auto sticker_set_id = supergroup_info->sticker_set_id; if (sticker_set_id != 0 && client_->get_sticker_set_name(sticker_set_id).empty()) { - return client_->send_request( - make_object(sticker_set_id), - td::make_unique(client_, chat_id_, pinned_message_id, std::move(query_))); + return client_->send_request(make_object(sticker_set_id), + td::make_unique( + client_, sticker_set_id, chat_id_, pinned_message_id, std::move(query_))); } sticker_set_id = supergroup_info->custom_emoji_sticker_set_id; if (sticker_set_id != 0 && client_->get_sticker_set_name(sticker_set_id).empty()) { - return client_->send_request(make_object(sticker_set_id), + return client_->send_request(make_object(sticker_set_id), td::make_unique( - client_, chat_id_, pinned_message_id, std::move(query_))); + client_, sticker_set_id, chat_id_, pinned_message_id, std::move(query_))); } } else if (chat_info->type == ChatInfo::Type::Private) { auto user_info = client_->get_user_info(chat_info->user_id); @@ -5347,9 +5363,9 @@ class Client::TdOnGetChatPinnedMessageCallback final : public TdQueryCallback { if (sticker != nullptr) { auto sticker_set_id = sticker->set_id_; if (sticker_set_id != 0 && client_->get_sticker_set_name(sticker_set_id).empty()) { - return client_->send_request(make_object(sticker_set_id), + return client_->send_request(make_object(sticker_set_id), td::make_unique( - client_, chat_id_, pinned_message_id, std::move(query_))); + client_, sticker_set_id, chat_id_, pinned_message_id, std::move(query_))); } } } @@ -5825,8 +5841,8 @@ class Client::TdOnReturnStickerSetCallback final : public TdQueryCallback { class Client::TdOnGetStickerSetPromiseCallback final : public TdQueryCallback { public: - TdOnGetStickerSetPromiseCallback(Client *client, td::Promise &&promise) - : client_(client), promise_(std::move(promise)) { + TdOnGetStickerSetPromiseCallback(Client *client, int64 sticker_set_id, td::Promise &&promise) + : client_(client), sticker_set_id_(sticker_set_id), promise_(std::move(promise)) { } void on_result(object_ptr result) final { @@ -5835,14 +5851,15 @@ class Client::TdOnGetStickerSetPromiseCallback final : public TdQueryCallback { return promise_.set_error(td::Status::Error(error->code_, error->message_)); } - CHECK(result->get_id() == td_api::stickerSet::ID); - auto sticker_set = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set->id_, sticker_set->name_); + CHECK(result->get_id() == td_api::text::ID); + auto sticker_set_name = move_object_as(result); + client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); promise_.set_value(td::Unit()); } private: Client *client_; + int64 sticker_set_id_; td::Promise promise_; }; @@ -5874,8 +5891,9 @@ class Client::TdOnGetStickersCallback final : public TdQueryCallback { auto lock = mpas.get_promise(); for (auto sticker_set_id : sticker_set_ids) { - client_->send_request(make_object(sticker_set_id), - td::make_unique(client_, mpas.get_promise())); + client_->send_request( + make_object(sticker_set_id), + td::make_unique(client_, sticker_set_id, mpas.get_promise())); } lock.set_value(td::Unit()); } @@ -6145,7 +6163,7 @@ void Client::on_get_callback_query_message(object_ptr message, void Client::on_get_sticker_set(int64 set_id, int64 new_callback_query_user_id, int64 new_message_chat_id, const td::string &new_message_business_connection_id, - int64 new_business_callback_query_user_id, object_ptr sticker_set) { + int64 new_business_callback_query_user_id, object_ptr sticker_set_name) { if (new_callback_query_user_id != 0) { auto &queue = new_callback_query_queues_[new_callback_query_user_id]; CHECK(queue.has_active_request_); @@ -6178,8 +6196,8 @@ void Client::on_get_sticker_set(int64 set_id, int64 new_callback_query_user_id, CHECK(set_id != 0); if (set_id != GREAT_MINDS_SET_ID) { td::string &set_name = sticker_set_names_[set_id]; - if (sticker_set != nullptr) { - set_name = std::move(sticker_set->name_); + if (sticker_set_name != nullptr) { + set_name = std::move(sticker_set_name->text_); } } @@ -13517,7 +13535,7 @@ void Client::process_new_callback_query_queue(int64 user_id, int state) { if (!have_sticker_set_name(message_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(message_sticker_set_id), + make_object(message_sticker_set_id), td::make_unique(this, message_sticker_set_id, user_id, 0, td::string(), 0)); } auto reply_to_message_id = get_same_chat_reply_to_message_id(message_info); @@ -13528,7 +13546,7 @@ void Client::process_new_callback_query_queue(int64 user_id, int state) { if (!have_sticker_set_name(reply_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(reply_sticker_set_id), + make_object(reply_sticker_set_id), td::make_unique(this, reply_sticker_set_id, user_id, 0, td::string(), 0)); } } @@ -13579,7 +13597,7 @@ void Client::process_new_business_callback_query_queue(int64 user_id) { if (!have_sticker_set_name(message_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(message_sticker_set_id), + make_object(message_sticker_set_id), td::make_unique(this, message_sticker_set_id, 0, 0, td::string(), user_id)); } if (message_ref->reply_to_message_ != nullptr) { @@ -13588,7 +13606,7 @@ void Client::process_new_business_callback_query_queue(int64 user_id) { if (!have_sticker_set_name(reply_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(reply_sticker_set_id), + make_object(reply_sticker_set_id), td::make_unique(this, reply_sticker_set_id, 0, 0, td::string(), user_id)); } } @@ -14163,7 +14181,7 @@ void Client::process_new_message_queue(int64 chat_id, int state) { if (!have_sticker_set_name(message_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(message_sticker_set_id), + make_object(message_sticker_set_id), td::make_unique(this, message_sticker_set_id, 0, chat_id, td::string(), 0)); } if (reply_to_message_id > 0) { @@ -14173,7 +14191,7 @@ void Client::process_new_message_queue(int64 chat_id, int state) { if (!have_sticker_set_name(reply_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(reply_sticker_set_id), + make_object(reply_sticker_set_id), td::make_unique(this, reply_sticker_set_id, 0, chat_id, td::string(), 0)); } } @@ -14263,7 +14281,7 @@ void Client::process_new_business_message_queue(const td::string &connection_id) if (!have_sticker_set_name(message_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(message_sticker_set_id), + make_object(message_sticker_set_id), td::make_unique(this, message_sticker_set_id, 0, 0, connection_id, 0)); } if (message_ref->reply_to_message_ != nullptr) { @@ -14272,7 +14290,7 @@ void Client::process_new_business_message_queue(const td::string &connection_id) if (!have_sticker_set_name(reply_sticker_set_id)) { queue.has_active_request_ = true; return send_request( - make_object(reply_sticker_set_id), + make_object(reply_sticker_set_id), td::make_unique(this, reply_sticker_set_id, 0, 0, connection_id, 0)); } } @@ -14426,7 +14444,7 @@ void Client::init_message(MessageInfo *message_info, object_ptr auto sticker_set_id = get_sticker_set_id(message_info->content); if (!have_sticker_set_name(sticker_set_id)) { - send_request(make_object(sticker_set_id), + send_request(make_object(sticker_set_id), td::make_unique(this, sticker_set_id, 0, 0, td::string(), 0)); } } else if (message->content_->get_id() == td_api::messagePoll::ID) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index bc12bcb..5b430d9 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -266,7 +266,7 @@ class Client final : public WebhookActor::Callback { void on_get_sticker_set(int64 set_id, int64 new_callback_query_user_id, int64 new_message_chat_id, const td::string &new_message_business_connection_id, - int64 new_business_callback_query_user_id, object_ptr sticker_set); + int64 new_business_callback_query_user_id, object_ptr sticker_set_name); void on_get_sticker_set_name(int64 set_id, const td::string &name); From dfe0dc9881a4b55739026a38e832a80b625f756b Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 12:01:43 +0300 Subject: [PATCH 04/13] Add Client::on_get_sticker_set_name overload. --- telegram-bot-api/Client.cpp | 22 ++++++++++------------ telegram-bot-api/Client.h | 2 ++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index cc3e12d..32fc2d4 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -5216,9 +5216,7 @@ class Client::TdOnGetChatCustomEmojiStickerSetCallback final : public TdQueryCal if (result->get_id() == td_api::error::ID) { supergroup_info->custom_emoji_sticker_set_id = 0; } else { - CHECK(result->get_id() == td_api::text::ID); - auto sticker_set_name = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); + client_->on_get_sticker_set_name(sticker_set_id_, std::move(result)); } answer_query(JsonChat(chat_id_, client_, true, pinned_message_id_), std::move(query_)); @@ -5254,9 +5252,7 @@ class Client::TdOnGetChatBusinessStartPageStickerSetCallback final : public TdQu user_info->business_info->start_page_->sticker_->set_id_ = 0; } } else { - CHECK(result->get_id() == td_api::text::ID); - auto sticker_set_name = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); + client_->on_get_sticker_set_name(sticker_set_id_, std::move(result)); } answer_query(JsonChat(chat_id_, client_, true, pinned_message_id_), std::move(query_)); @@ -5289,9 +5285,7 @@ class Client::TdOnGetChatStickerSetCallback final : public TdQueryCallback { if (result->get_id() == td_api::error::ID) { supergroup_info->sticker_set_id = 0; } else { - CHECK(result->get_id() == td_api::text::ID); - auto sticker_set_name = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); + client_->on_get_sticker_set_name(sticker_set_id_, std::move(result)); } auto sticker_set_id = supergroup_info->custom_emoji_sticker_set_id; @@ -5851,9 +5845,7 @@ class Client::TdOnGetStickerSetPromiseCallback final : public TdQueryCallback { return promise_.set_error(td::Status::Error(error->code_, error->message_)); } - CHECK(result->get_id() == td_api::text::ID); - auto sticker_set_name = move_object_as(result); - client_->on_get_sticker_set_name(sticker_set_id_, sticker_set_name->text_); + client_->on_get_sticker_set_name(sticker_set_id_, std::move(result)); promise_.set_value(td::Unit()); } @@ -6222,6 +6214,12 @@ void Client::on_get_sticker_set_name(int64 set_id, const td::string &name) { } } +void Client::on_get_sticker_set_name(int64 set_id, object_ptr sticker_set_name) { + CHECK(sticker_set_name->get_id() == td_api::text::ID); + auto text = move_object_as(sticker_set_name); + on_get_sticker_set_name(set_id, text->text_); +} + template void Client::check_user_read_access(const UserInfo *user_info, PromisedQueryPtr query, OnSuccess on_success) { CHECK(user_info != nullptr); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 5b430d9..820398b 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -270,6 +270,8 @@ class Client final : public WebhookActor::Callback { void on_get_sticker_set_name(int64 set_id, const td::string &name); + void on_get_sticker_set_name(int64 set_id, object_ptr sticker_set_name); + class TdQueryCallback { public: virtual void on_result(object_ptr result) = 0; From f8c757d07d30898ecc2f04e99cb0a14fdfbbb6aa Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 12:10:37 +0300 Subject: [PATCH 05/13] Add TransactionPartnerUser.paid_media_payload. --- telegram-bot-api/Client.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 32fc2d4..169672f 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -4148,6 +4148,9 @@ class Client::JsonStarTransactionPartner final : public td::Jsonable { object("paid_media", td::json_array(purpose->media_, [client = client_](auto &media) { return JsonPaidMedia(media.get(), client); })); + if (!purpose->payload_.empty()) { + object("paid_media_payload", purpose->payload_); + } break; } default: From 3fdee61a88d92974d7d456ffdb5cdd9463557457 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 12:13:10 +0300 Subject: [PATCH 06/13] Add sendPaidMedia.payload. --- telegram-bot-api/Client.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 169672f..3db925d 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -10400,9 +10400,10 @@ td::Status Client::process_send_paid_media_query(PromisedQueryPtr &query) { int32 star_count = get_integer_arg(query.get(), "star_count", 0, 0, 1000000000); TRY_RESULT(paid_media, get_paid_media(query.get(), "media")); TRY_RESULT(caption, get_caption(query.get())); + auto payload = query->arg("payload").str(); auto show_caption_above_media = to_bool(query->arg("show_caption_above_media")); do_send_message(make_object(star_count, std::move(paid_media), std::move(caption), - show_caption_above_media, td::string()), + show_caption_above_media, payload), std::move(query)); return td::Status::OK(); } From 7422214945a17a3face29b3aa58cde7b05de7048 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 12:27:23 +0300 Subject: [PATCH 07/13] Add GiveawayCreated.prize_star_count. --- telegram-bot-api/Client.cpp | 22 ++++++++++++++++++++-- telegram-bot-api/Client.h | 1 + 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 3db925d..468af8d 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2419,6 +2419,22 @@ class Client::JsonChatShared final : public td::Jsonable { const Client *client_; }; +class Client::JsonGiveawayCreated final : public td::Jsonable { + public: + explicit JsonGiveawayCreated(const td_api::messageGiveawayCreated *giveaway_created) + : giveaway_created_(giveaway_created) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + if (giveaway_created_->star_count_ > 0) { + object("prize_star_count", giveaway_created_->star_count_); + } + } + + private: + const td_api::messageGiveawayCreated *giveaway_created_; +}; + class Client::JsonGiveaway final : public td::Jsonable { public: JsonGiveaway(const td_api::messageGiveaway *giveaway, const Client *client) : giveaway_(giveaway), client_(client) { @@ -3290,9 +3306,11 @@ void Client::JsonMessage::store(td::JsonValueScope *scope) const { } case td_api::messagePremiumGiftCode::ID: break; - case td_api::messageGiveawayCreated::ID: - object("giveaway_created", JsonEmptyObject()); + case td_api::messageGiveawayCreated::ID: { + auto content = static_cast(message_->content.get()); + object("giveaway_created", JsonGiveawayCreated(content)); break; + } case td_api::messageGiveaway::ID: { auto content = static_cast(message_->content.get()); object("giveaway", JsonGiveaway(content, client_)); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 820398b..dba71ac 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -194,6 +194,7 @@ class Client final : public WebhookActor::Callback { class JsonSharedUser; class JsonUsersShared; class JsonChatShared; + class JsonGiveawayCreated; class JsonGiveaway; class JsonGiveawayWinners; class JsonGiveawayCompleted; From 1548e96656b5214bcf6d98c5f0b6ed758d68977e Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 12:29:44 +0300 Subject: [PATCH 08/13] Add Giveaway.prize_star_count. --- telegram-bot-api/Client.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 468af8d..c7c8db7 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2470,8 +2470,13 @@ class Client::JsonGiveaway final : public td::Jsonable { } break; } - case td_api::giveawayPrizeStars::ID: + case td_api::giveawayPrizeStars::ID: { + auto star_count = static_cast(giveaway_->prize_.get())->star_count_; + if (star_count > 0) { + object("prize_star_count", star_count); + } break; + } default: UNREACHABLE(); } From c08e6b3b84092111d4411761575c30c49714c1cb Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 12:32:03 +0300 Subject: [PATCH 09/13] Add GiveawayWinners.prize_star_count. --- telegram-bot-api/Client.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index c7c8db7..676011d 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2515,8 +2515,13 @@ class Client::JsonGiveawayWinners final : public td::Jsonable { } break; } - case td_api::giveawayPrizeStars::ID: + case td_api::giveawayPrizeStars::ID: { + auto star_count = static_cast(giveaway_winners_->prize_.get())->star_count_; + if (star_count > 0) { + object("prize_star_count", star_count); + } break; + } default: UNREACHABLE(); } From cb41fe9930bb09cece032d26dc5659dbc7e47545 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 28 Aug 2024 13:13:51 +0300 Subject: [PATCH 10/13] Add "purchased_paid_media" update. --- telegram-bot-api/Client.cpp | 28 ++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 676011d..9542b2d 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -3610,6 +3610,23 @@ class Client::JsonPreCheckoutQuery final : public td::Jsonable { const Client *client_; }; +class Client::JsonPaidMediaPurchased final : public td::Jsonable { + public: + JsonPaidMediaPurchased(const td_api::updatePaidMediaPurchased *update, const Client *client) + : update_(update), client_(client) { + } + + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("from", JsonUser(update_->user_id_, client_)); + object("payload", update_->payload_); + } + + private: + const td_api::updatePaidMediaPurchased *update_; + const Client *client_; +}; + class Client::JsonCustomJson final : public td::Jsonable { public: explicit JsonCustomJson(const td::string &json) : json_(json) { @@ -7252,6 +7269,9 @@ void Client::on_update(object_ptr result) { case td_api::updateNewPreCheckoutQuery::ID: add_new_pre_checkout_query(move_object_as(result)); break; + case td_api::updatePaidMediaPurchased::ID: + add_update_purchased_paid_media(move_object_as(result)); + break; case td_api::updateNewCustomEvent::ID: add_new_custom_event(move_object_as(result)); break; @@ -13349,6 +13369,8 @@ td::Slice Client::get_update_type_name(UpdateType update_type) { return td::Slice("edited_business_message"); case UpdateType::BusinessMessagesDeleted: return td::Slice("deleted_business_messages"); + case UpdateType::PurchasedPaidMedia: + return td::Slice("purchased_paid_media"); default: UNREACHABLE(); return td::Slice(); @@ -13672,6 +13694,12 @@ void Client::add_new_pre_checkout_query(object_ptrsender_user_id_ + (static_cast(4) << 33)); } +void Client::add_update_purchased_paid_media(object_ptr &&query) { + CHECK(query != nullptr); + add_update(UpdateType::PurchasedPaidMedia, JsonPaidMediaPurchased(query.get(), this), 86400, + query->user_id_ + (static_cast(12) << 33)); +} + void Client::add_new_custom_event(object_ptr &&event) { CHECK(event != nullptr); add_update(UpdateType::CustomEvent, JsonCustomJson(event->event_), 600, 0); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index dba71ac..24176c4 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -148,6 +148,7 @@ class Client final : public WebhookActor::Callback { class JsonInlineCallbackQuery; class JsonShippingQuery; class JsonPreCheckoutQuery; + class JsonPaidMediaPurchased; class JsonBotCommand; class JsonBotMenuButton; class JsonBotName; @@ -1108,6 +1109,8 @@ class Client final : public WebhookActor::Callback { void add_new_pre_checkout_query(object_ptr &&query); + void add_update_purchased_paid_media(object_ptr &&query); + void add_new_custom_event(object_ptr &&event); void add_new_custom_query(object_ptr &&query); @@ -1152,6 +1155,7 @@ class Client final : public WebhookActor::Callback { BusinessMessage, EditedBusinessMessage, BusinessMessagesDeleted, + PurchasedPaidMedia, Size }; From c96ab1328873fb1f9e0ddaed8d3c525a67b4246a Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 30 Aug 2024 11:44:17 +0300 Subject: [PATCH 11/13] Add ChatBoostSourceGiveaway.prize_star_count. --- telegram-bot-api/Client.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 9542b2d..7748516 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -3934,6 +3934,9 @@ class Client::JsonChatBoostSource final : public td::Jsonable { const auto *source = static_cast(boost_source_); object("source", "giveaway"); object("giveaway_message_id", as_client_message_id_unchecked(source->giveaway_message_id_)); + if (source->star_count_ > 0) { + object("prize_star_count", source->star_count_); + } if (source->user_id_ != 0) { object("user", JsonUser(source->user_id_, client_)); } else if (source->is_unclaimed_) { From 4acd7d28e619d3f738adfe7c6b9d8ae097247b04 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 30 Aug 2024 11:45:49 +0300 Subject: [PATCH 12/13] Add GiveawayCompleted.is_star_giveaway. --- telegram-bot-api/Client.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 7748516..6095f43 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -2551,6 +2551,9 @@ class Client::JsonGiveawayCompleted final : public td::Jsonable { if (giveaway_completed_->unclaimed_prize_count_ > 0) { object("unclaimed_prize_count", giveaway_completed_->unclaimed_prize_count_); } + if (giveaway_completed_->is_star_giveaway_) { + object("is_star_giveaway", td::JsonTrue()); + } const MessageInfo *giveaway_message = client_->get_message(chat_id_, giveaway_completed_->giveaway_message_id_, true); if (giveaway_message != nullptr) { From a186a9ae823d91678ace87ef5b920688c555f5b5 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 6 Sep 2024 16:22:58 +0300 Subject: [PATCH 13/13] Update version to 7.10. --- CMakeLists.txt | 2 +- telegram-bot-api/telegram-bot-api.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b5e56ca..74f1233 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ if (POLICY CMP0065) cmake_policy(SET CMP0065 NEW) endif() -project(TelegramBotApi VERSION 7.9 LANGUAGES CXX) +project(TelegramBotApi VERSION 7.10 LANGUAGES CXX) if (POLICY CMP0069) option(TELEGRAM_BOT_API_ENABLE_LTO "Use \"ON\" to enable Link Time Optimization.") diff --git a/telegram-bot-api/telegram-bot-api.cpp b/telegram-bot-api/telegram-bot-api.cpp index 9035a3b..a234ade 100644 --- a/telegram-bot-api/telegram-bot-api.cpp +++ b/telegram-bot-api/telegram-bot-api.cpp @@ -165,7 +165,7 @@ int main(int argc, char *argv[]) { auto start_time = td::Time::now(); auto shared_data = std::make_shared(); auto parameters = std::make_unique(); - parameters->version_ = "7.9"; + parameters->version_ = "7.10"; parameters->shared_data_ = shared_data; parameters->start_time_ = start_time; auto net_query_stats = td::create_net_query_stats();