From 84199e5328fa709428b929e8e11fcc4974e438bd Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 8 Apr 2022 03:07:52 +0300 Subject: [PATCH] Add getChatMenuButton. --- telegram-bot-api/Client.cpp | 54 ++++++++++++++++++++++++++++++++++++- telegram-bot-api/Client.h | 5 +++- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index b81c6cc..bed4440 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -194,6 +194,7 @@ bool Client::init_methods() { methods_.emplace("deletemycommands", &Client::process_delete_my_commands_query); methods_.emplace("getmydefaultadministratorrights", &Client::process_get_my_default_administrator_rights_query); methods_.emplace("setmydefaultadministratorrights", &Client::process_set_my_default_administrator_rights_query); + methods_.emplace("getchatmenubutton", &Client::process_get_chat_menu_button_query); methods_.emplace("getuserprofilephotos", &Client::process_get_user_profile_photos_query); methods_.emplace("sendmessage", &Client::process_send_message_query); methods_.emplace("sendanimation", &Client::process_send_animation_query); @@ -2293,6 +2294,25 @@ class Client::JsonBotCommand final : public Jsonable { const td_api::botCommand *command_; }; +class Client::JsonBotMenuButton final : public Jsonable { + public: + explicit JsonBotMenuButton(const td_api::botMenuButton *menu_button) : menu_button_(menu_button) { + } + void store(JsonValueScope *scope) const { + auto object = scope->enter_object(); + if (menu_button_->text_.empty()) { + object("type", menu_button_->url_.empty() ? "commands" : "default"); + } else { + object("type", "web_app"); + object("text", menu_button_->text_); + object("web_app", JsonWebAppInfo(menu_button_->url_)); + } + } + + private: + const td_api::botMenuButton *menu_button_; +}; + class Client::JsonChatAdministratorRights final : public Jsonable { public: JsonChatAdministratorRights(const td_api::chatAdministratorRights *rights, Client::ChatType chat_type) @@ -3369,6 +3389,25 @@ class Client::TdOnGetMyDefaultAdministratorRightsCallback final : public TdQuery PromisedQueryPtr query_; }; +class Client::TdOnGetMenuButtonCallback final : public TdQueryCallback { + public: + explicit TdOnGetMenuButtonCallback(PromisedQueryPtr query) : 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::botMenuButton::ID); + auto menu_button = move_object_as(result); + answer_query(JsonBotMenuButton(menu_button.get()), std::move(query_)); + } + + private: + PromisedQueryPtr query_; +}; + class Client::TdOnGetChatFullInfoCallback final : public TdQueryCallback { public: TdOnGetChatFullInfoCallback(Client *client, int64 chat_id, PromisedQueryPtr query) @@ -3860,7 +3899,7 @@ template void Client::check_user_read_access(const UserInfo *user_info, PromisedQueryPtr query, OnSuccess on_success) { CHECK(user_info != nullptr); if (!user_info->have_access) { - return fail_query(400, "Bad Request: have no access to the user", std::move(query)); + // return fail_query(400, "Bad Request: have no access to the user", std::move(query)); } on_success(std::move(query)); } @@ -6793,6 +6832,19 @@ td::Status Client::process_set_my_default_administrator_rights_query(PromisedQue return Status::OK(); } +td::Status Client::process_get_chat_menu_button_query(PromisedQueryPtr &query) { + if (query->has_arg("chat_id")) { + TRY_RESULT(user_id, get_user_id(query.get(), "chat_id")); + check_user(user_id, std::move(query), [this, user_id](PromisedQueryPtr query) { + send_request(make_object(user_id), + td::make_unique(std::move(query))); + }); + } else { + send_request(make_object(0), td::make_unique(std::move(query))); + } + return Status::OK(); +} + td::Status Client::process_get_user_profile_photos_query(PromisedQueryPtr &query) { TRY_RESULT(user_id, get_user_id(query.get())); int32 offset = get_integer_arg(query.get(), "offset", 0, 0); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index fe340c6..d3e9211 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -134,6 +134,7 @@ class Client final : public WebhookActor::Callback { class JsonShippingQuery; class JsonPreCheckoutQuery; class JsonBotCommand; + class JsonBotMenuButton; class JsonChatAdministratorRights; class JsonChatPhotos; class JsonChatMember; @@ -175,6 +176,7 @@ class Client final : public WebhookActor::Callback { class TdOnGetEditedMessageCallback; class TdOnGetCallbackQueryMessageCallback; class TdOnGetStickerSetCallback; + class TdOnGetMenuButtonCallback; class TdOnGetMyCommandsCallback; class TdOnGetMyDefaultAdministratorRightsCallback; class TdOnGetChatFullInfoCallback; @@ -454,9 +456,10 @@ class Client final : public WebhookActor::Callback { Status process_get_me_query(PromisedQueryPtr &query); Status process_get_my_commands_query(PromisedQueryPtr &query); Status process_set_my_commands_query(PromisedQueryPtr &query); + Status process_delete_my_commands_query(PromisedQueryPtr &query); Status process_get_my_default_administrator_rights_query(PromisedQueryPtr &query); Status process_set_my_default_administrator_rights_query(PromisedQueryPtr &query); - Status process_delete_my_commands_query(PromisedQueryPtr &query); + Status process_get_chat_menu_button_query(PromisedQueryPtr &query); Status process_get_user_profile_photos_query(PromisedQueryPtr &query); Status process_send_message_query(PromisedQueryPtr &query); Status process_send_animation_query(PromisedQueryPtr &query);