Add keyboardButtonTypeWebView.

This commit is contained in:
levlam 2022-03-27 02:20:26 +03:00
parent c6f6733669
commit 0ae8401ab5
4 changed files with 39 additions and 1 deletions

View File

@ -1125,6 +1125,9 @@ keyboardButtonTypeRequestLocation = KeyboardButtonType;
//@description A button that allows the user to create and send a poll when pressed; available only in private chats @force_regular If true, only regular polls must be allowed to create @force_quiz If true, only polls in quiz mode must be allowed to create
keyboardButtonTypeRequestPoll force_regular:Bool force_quiz:Bool = KeyboardButtonType;
//@description A button that opens a specified URL in web view by calling getSimpleWebViewUrl @url An HTTP URL to pass to getSimpleWebViewUrl
keyboardButtonTypeWebView url:string = KeyboardButtonType;
//@description Represents a single button in a bot keyboard @text Text of the button @type Type of the button
keyboardButton text:string type:KeyboardButtonType = KeyboardButton;

View File

@ -54,6 +54,9 @@ static StringBuilder &operator<<(StringBuilder &string_builder, const KeyboardBu
case KeyboardButton::Type::RequestPollRegular:
string_builder << "RequestPollRegular";
break;
case KeyboardButton::Type::WebView:
string_builder << "WebView";
break;
default:
UNREACHABLE();
}
@ -217,6 +220,13 @@ static KeyboardButton get_keyboard_button(tl_object_ptr<telegram_api::KeyboardBu
button.text = std::move(keyboard_button->text_);
break;
}
case telegram_api::keyboardButtonSimpleWebView::ID: {
auto keyboard_button = move_tl_object_as<telegram_api::keyboardButtonSimpleWebView>(keyboard_button_ptr);
button.type = KeyboardButton::Type::WebView;
button.text = std::move(keyboard_button->text_);
button.url = std::move(keyboard_button->url_);
break;
}
default:
LOG(ERROR) << "Unsupported keyboard button: " << to_string(keyboard_button_ptr);
}
@ -430,6 +440,13 @@ static Result<KeyboardButton> get_keyboard_button(tl_object_ptr<td_api::keyboard
}
break;
}
case td_api::keyboardButtonTypeWebView::ID:
if (!request_buttons_allowed) {
return Status::Error(400, "Web view can be used in private chats only");
}
current_button.type = KeyboardButton::Type::WebView;
current_button.url = std::move(static_cast<td_api::keyboardButtonTypeWebView *>(button->type_.get())->url_);
break;
default:
UNREACHABLE();
}
@ -690,6 +707,8 @@ static tl_object_ptr<telegram_api::KeyboardButton> get_keyboard_button(const Key
return make_tl_object<telegram_api::keyboardButtonRequestPoll>(1, true, keyboard_button.text);
case KeyboardButton::Type::RequestPollRegular:
return make_tl_object<telegram_api::keyboardButtonRequestPoll>(1, false, keyboard_button.text);
case KeyboardButton::Type::WebView:
return make_tl_object<telegram_api::keyboardButtonSimpleWebView>(keyboard_button.text, keyboard_button.url);
default:
UNREACHABLE();
return nullptr;
@ -828,6 +847,9 @@ static tl_object_ptr<td_api::keyboardButton> get_keyboard_button_object(const Ke
case KeyboardButton::Type::RequestPollRegular:
type = make_tl_object<td_api::keyboardButtonTypeRequestPoll>(true, false);
break;
case KeyboardButton::Type::WebView:
type = make_tl_object<td_api::keyboardButtonTypeWebView>(keyboard_button.url);
break;
default:
UNREACHABLE();
return nullptr;

View File

@ -26,10 +26,12 @@ struct KeyboardButton {
RequestLocation,
RequestPoll,
RequestPollQuiz,
RequestPollRegular
RequestPollRegular,
WebView
};
Type type;
string text;
string url; // WebView only
};
struct InlineKeyboardButton {

View File

@ -15,20 +15,31 @@ namespace td {
template <class StorerT>
void store(KeyboardButton button, StorerT &storer) {
bool has_url = !button.url.empty();
;
BEGIN_STORE_FLAGS();
STORE_FLAG(has_url);
END_STORE_FLAGS();
store(button.type, storer);
store(button.text, storer);
if (has_url) {
store(button.url, storer);
}
}
template <class ParserT>
void parse(KeyboardButton &button, ParserT &parser) {
bool has_url = false;
if (parser.version() >= static_cast<int32>(Version::AddKeyboardButtonFlags)) {
BEGIN_PARSE_FLAGS();
PARSE_FLAG(has_url);
END_PARSE_FLAGS();
}
parse(button.type, parser);
parse(button.text, parser);
if (has_url) {
parse(button.url, parser);
}
}
template <class StorerT>