From defcb525715af55b6264f1f7658db987a984299c Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 25 Mar 2022 01:00:48 +0300 Subject: [PATCH] Support WebApp buttons. --- telegram-bot-api/Client.cpp | 37 +++++++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 1 + 2 files changed, 38 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index efb7c7a..91303b0 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -1545,6 +1545,19 @@ class Client::JsonCallbackGame final : public Jsonable { } }; +class Client::JsonWebAppInfo final : public Jsonable { + public: + explicit JsonWebAppInfo(const td::string &url) : url_(url) { + } + void store(JsonValueScope *scope) const { + auto object = scope->enter_object(); + object("url", url_); + } + + private: + const td::string &url_; +}; + class Client::JsonInlineKeyboardButton final : public Jsonable { public: explicit JsonInlineKeyboardButton(const td_api::inlineKeyboardButton *button) : button_(button) { @@ -1593,6 +1606,11 @@ class Client::JsonInlineKeyboardButton final : public Jsonable { object("url", PSLICE() << "tg://user?id=" << type->user_id_); break; } + case td_api::inlineKeyboardButtonTypeWebApp::ID: { + auto type = static_cast(button_->type_.get()); + object("web_app", JsonWebAppInfo(type->url_)); + break; + } default: UNREACHABLE(); break; @@ -4808,6 +4826,13 @@ td::Result> Client::get_keyboard_butt text, make_object(force_regular, force_quiz)); } + if (has_json_object_field(object, "web_app")) { + TRY_RESULT(web_app, get_json_object_field(object, "web_app", JsonValue::Type::Object, false)); + auto &web_app_object = web_app.get_object(); + TRY_RESULT(url, get_json_object_string_field(web_app_object, "url")); + return make_object(text, make_object(url)); + } + return make_object(text, nullptr); } if (button.type() == JsonValue::Type::String) { @@ -4904,6 +4929,13 @@ td::Result> Client::get_inline_ text, make_object(url, bot_user_id, forward_text)); } + if (has_json_object_field(object, "web_app")) { + TRY_RESULT(web_app, get_json_object_field(object, "web_app", JsonValue::Type::Object, false)); + auto &web_app_object = web_app.get_object(); + TRY_RESULT(url, get_json_object_string_field(web_app_object, "url")); + return make_object(text, make_object(url)); + } + return Status::Error(400, "Text buttons are unallowed in the inline keyboard"); } @@ -9467,6 +9499,11 @@ bool Client::are_equal_inline_keyboard_buttons(const td_api::inlineKeyboardButto auto rhs_type = static_cast(rhs->type_.get()); return lhs_type->user_id_ == rhs_type->user_id_; } + case td_api::inlineKeyboardButtonTypeWebApp::ID: { + auto lhs_type = static_cast(lhs->type_.get()); + auto rhs_type = static_cast(rhs->type_.get()); + return lhs_type->url_ == rhs_type->url_; + } default: UNREACHABLE(); return false; diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index ad7c0d3..cabb2d3 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -119,6 +119,7 @@ class Client final : public WebhookActor::Callback { class JsonEntity; class JsonVectorEntities; class JsonCallbackGame; + class JsonWebAppInfo; class JsonInlineKeyboardButton; class JsonInlineKeyboard; class JsonReplyMarkup;