From e000e2610f42d6b2cd7e202647cf96b1e398ab62 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 13 Nov 2024 17:08:57 +0300 Subject: [PATCH] Add getAvailableGifts. --- telegram-bot-api/Client.cpp | 63 +++++++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 4 +++ 2 files changed, 67 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 2c370ab..d58a4bd 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -253,6 +253,7 @@ bool Client::init_methods() { methods_.emplace("createinvoicelink", &Client::process_create_invoice_link_query); methods_.emplace("getstartransactions", &Client::process_get_star_transactions_query); methods_.emplace("refundstarpayment", &Client::process_refund_star_payment_query); + methods_.emplace("getavailablegifts", &Client::process_get_available_gifts_query); methods_.emplace("sendgift", &Client::process_send_gift_query); methods_.emplace("setgamescore", &Client::process_set_game_score_query); methods_.emplace("getgamehighscores", &Client::process_get_game_high_scores_query); @@ -4064,6 +4065,41 @@ class Client::JsonGameHighScore final : public td::Jsonable { const Client *client_; }; +class Client::JsonGift final : public td::Jsonable { + public: + JsonGift(const td_api::gift *gift, const Client *client) : gift_(gift), client_(client) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("id", td::to_string(gift_->id_)); + object("sticker", JsonSticker(gift_->sticker_.get(), client_)); + object("star_count", gift_->star_count_); + if (gift_->total_count_ > 0) { + object("remaining_count", gift_->remaining_count_); + object("total_count", gift_->total_count_); + } + } + + private: + const td_api::gift *gift_; + const Client *client_; +}; + +class Client::JsonGifts final : public td::Jsonable { + public: + JsonGifts(const td_api::gifts *gifts, const Client *client) : gifts_(gifts), client_(client) { + } + void store(td::JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("gifts", + td::json_array(gifts_->gifts_, [client = client_](auto &gift) { return JsonGift(gift.get(), client); })); + } + + private: + const td_api::gifts *gifts_; + const Client *client_; +}; + class Client::JsonMessageReactionUpdated final : public td::Jsonable { public: JsonMessageReactionUpdated(const td_api::updateMessageReaction *update, const Client *client) @@ -5916,6 +5952,28 @@ class Client::TdOnGetUserChatBoostsCallback final : public TdQueryCallback { PromisedQueryPtr query_; }; +class Client::TdOnGetGiftsCallback final : public TdQueryCallback { + public: + TdOnGetGiftsCallback(const Client *client, PromisedQueryPtr query) : client_(client), query_(std::move(query)) { + } + + void on_result(object_ptr result) final { + if (result->get_id() == td_api::error::ID) { + return fail_query_with_error(std::move(query_), move_object_as(result)); + } + + CHECK(result->get_id() == td_api::gifts::ID); + auto gifts = move_object_as(result); + td::remove_if(gifts->gifts_, + [](const auto &gift) { return gift->total_count_ > 0 && gift->remaining_count_ == 0; }); + answer_query(JsonGifts(gifts.get(), client_), std::move(query_)); + } + + private: + const Client *client_; + PromisedQueryPtr query_; +}; + class Client::TdOnReturnFileCallback final : public TdQueryCallback { public: TdOnReturnFileCallback(const Client *client, PromisedQueryPtr query) : client_(client), query_(std::move(query)) { @@ -11222,6 +11280,11 @@ td::Status Client::process_refund_star_payment_query(PromisedQueryPtr &query) { return td::Status::OK(); } +td::Status Client::process_get_available_gifts_query(PromisedQueryPtr &query) { + send_request(make_object(), td::make_unique(this, std::move(query))); + return td::Status::OK(); +} + td::Status Client::process_send_gift_query(PromisedQueryPtr &query) { auto gift_id = td::to_integer(query->arg("gift_id")); TRY_RESULT(user_id, get_user_id(query.get())); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index a137af4..8044cce 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -170,6 +170,8 @@ class Client final : public WebhookActor::Callback { class JsonForumTopicEdited; class JsonForumTopicInfo; class JsonGameHighScore; + class JsonGift; + class JsonGifts; class JsonMessageReactionUpdated; class JsonMessageReactionCountUpdated; class JsonBusinessConnection; @@ -248,6 +250,7 @@ class Client final : public WebhookActor::Callback { class TdOnGetSupergroupMembersCallback; class TdOnGetSupergroupMemberCountCallback; class TdOnGetUserChatBoostsCallback; + class TdOnGetGiftsCallback; class TdOnCreateInvoiceLinkCallback; class TdOnGetStarTransactionsQueryCallback; class TdOnReplacePrimaryChatInviteLinkCallback; @@ -683,6 +686,7 @@ class Client final : public WebhookActor::Callback { td::Status process_create_invoice_link_query(PromisedQueryPtr &query); td::Status process_get_star_transactions_query(PromisedQueryPtr &query); td::Status process_refund_star_payment_query(PromisedQueryPtr &query); + td::Status process_get_available_gifts_query(PromisedQueryPtr &query); td::Status process_send_gift_query(PromisedQueryPtr &query); td::Status process_set_game_score_query(PromisedQueryPtr &query); td::Status process_get_game_high_scores_query(PromisedQueryPtr &query);