Slightly refactored api_bot.

This commit is contained in:
23rd 2021-11-28 23:16:53 +03:00 committed by John Preston
parent 77d1d9bd3a
commit 9706b84409

View file

@ -34,8 +34,8 @@ void SendBotCallbackData(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
int row, int row,
int column, int column,
std::optional<MTPInputCheckPasswordSRP> password = std::nullopt, std::optional<Core::CloudPasswordResult> password = std::nullopt,
Fn<void(const MTP::Error &)> handleError = nullptr) { Fn<void(const QString &)> handleError = nullptr) {
if (!item->isRegular()) { if (!item->isRegular()) {
return; return;
} }
@ -78,7 +78,7 @@ void SendBotCallbackData(
history->peer->input, history->peer->input,
MTP_int(item->id), MTP_int(item->id),
MTP_bytes(sendData), MTP_bytes(sendData),
password.value_or(MTP_inputCheckPasswordEmpty()) password ? password->result : MTP_inputCheckPasswordEmpty()
)).done([=](const MTPmessages_BotCallbackAnswer &result) { )).done([=](const MTPmessages_BotCallbackAnswer &result) {
const auto item = owner->message(fullId); const auto item = owner->message(fullId);
if (!item) { if (!item) {
@ -88,34 +88,41 @@ void SendBotCallbackData(
button->requestId = 0; button->requestId = 0;
owner->requestItemRepaint(item); owner->requestItemRepaint(item);
} }
result.match([&](const MTPDmessages_botCallbackAnswer &data) { const auto &data = result.match([](
if (const auto message = data.vmessage()) { const auto &data) -> const MTPDmessages_botCallbackAnswer& {
if (data.is_alert()) { return data;
Ui::show(Box<Ui::InformBox>(qs(*message)));
} else {
if (withPassword) {
Ui::hideLayer();
}
Ui::Toast::Show(qs(*message));
}
} else if (const auto url = data.vurl()) {
const auto link = qs(*url);
if (!isGame) {
UrlClickHandler::Open(link);
return;
}
const auto scoreLink = AppendShareGameScoreUrl(
session,
link,
item->fullId());
BotGameUrlClickHandler(bot, scoreLink).onClick({});
session->sendProgressManager().update(
history,
Api::SendProgressType::PlayGame);
} else if (withPassword) {
Ui::hideLayer();
}
}); });
const auto message = data.vmessage()
? qs(*data.vmessage())
: QString();
const auto link = data.vurl() ? qs(*data.vurl()) : QString();
const auto showAlert = data.is_alert();
if (!message.isEmpty()) {
if (showAlert) {
Ui::show(Box<Ui::InformBox>(message));
} else {
if (withPassword) {
Ui::hideLayer();
}
Ui::Toast::Show(message);
}
} else if (!link.isEmpty()) {
if (!isGame) {
UrlClickHandler::Open(link);
return;
}
const auto scoreLink = AppendShareGameScoreUrl(
session,
link,
item->fullId());
BotGameUrlClickHandler(bot, scoreLink).onClick({});
session->sendProgressManager().update(
history,
Api::SendProgressType::PlayGame);
} else if (withPassword) {
Ui::hideLayer();
}
}).fail([=](const MTP::Error &error) { }).fail([=](const MTP::Error &error) {
const auto item = owner->message(fullId); const auto item = owner->message(fullId);
if (!item) { if (!item) {
@ -127,7 +134,7 @@ void SendBotCallbackData(
owner->requestItemRepaint(item); owner->requestItemRepaint(item);
} }
if (handleError) { if (handleError) {
handleError(error); handleError(error.type());
} }
}).send(); }).send();
@ -143,7 +150,7 @@ void SendBotCallbackData(
not_null<HistoryItem*> item, not_null<HistoryItem*> item,
int row, int row,
int column) { int column) {
SendBotCallbackData(item, row, column, MTP_inputCheckPasswordEmpty()); SendBotCallbackData(item, row, column, std::nullopt);
} }
void SendBotCallbackDataWithPassword( void SendBotCallbackDataWithPassword(
@ -170,9 +177,9 @@ void SendBotCallbackDataWithPassword(
return; return;
} }
api->cloudPassword().reload(); api->cloudPassword().reload();
SendBotCallbackData(item, row, column, MTP_inputCheckPasswordEmpty(), [=](const MTP::Error &error) { SendBotCallbackData(item, row, column, std::nullopt, [=](const QString &error) {
auto box = PrePasswordErrorBox( auto box = PrePasswordErrorBox(
error.type(), error,
session, session,
tr::lng_bots_password_confirm_check_about( tr::lng_bots_password_confirm_check_about(
tr::now, tr::now,
@ -212,7 +219,7 @@ void SendBotCallbackDataWithPassword(
return; return;
} }
if (const auto item = owner->message(fullId)) { if (const auto item = owner->message(fullId)) {
SendBotCallbackData(item, row, column, result.result, [=](const MTP::Error &error) { SendBotCallbackData(item, row, column, result, [=](const QString &error) {
if (*box) { if (*box) {
(*box)->handleCustomCheckError(error); (*box)->handleCustomCheckError(error);
} }