2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/CallbackQueriesManager.h"
|
|
|
|
|
|
|
|
#include "td/telegram/AccessRights.h"
|
|
|
|
#include "td/telegram/AuthManager.h"
|
2024-01-03 21:07:50 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/InlineQueriesManager.h"
|
|
|
|
#include "td/telegram/MessagesManager.h"
|
2020-08-18 08:32:37 +02:00
|
|
|
#include "td/telegram/PasswordManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2024-04-02 02:52:34 +02:00
|
|
|
#include "td/telegram/UserManager.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GetBotCallbackAnswerQuery final : public Td::ResultHandler {
|
2021-09-27 22:22:10 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::callbackQueryAnswer>> promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
DialogId dialog_id_;
|
2019-02-21 19:30:11 +01:00
|
|
|
MessageId message_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
public:
|
2021-09-27 22:22:10 +02:00
|
|
|
explicit GetBotCallbackAnswerQuery(Promise<td_api::object_ptr<td_api::callbackQueryAnswer>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void send(DialogId dialog_id, MessageId message_id, const tl_object_ptr<td_api::CallbackQueryPayload> &payload,
|
2021-09-27 22:22:10 +02:00
|
|
|
tl_object_ptr<telegram_api::InputCheckPasswordSRP> &&password) {
|
2018-12-31 20:04:05 +01:00
|
|
|
dialog_id_ = dialog_id;
|
2019-02-21 19:30:11 +01:00
|
|
|
message_id_ = message_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
BufferSlice data;
|
|
|
|
CHECK(payload != nullptr);
|
|
|
|
switch (payload->get_id()) {
|
|
|
|
case td_api::callbackQueryPayloadData::ID:
|
|
|
|
flags = telegram_api::messages_getBotCallbackAnswer::DATA_MASK;
|
|
|
|
data = BufferSlice(static_cast<const td_api::callbackQueryPayloadData *>(payload.get())->data_);
|
|
|
|
break;
|
2020-08-18 08:32:37 +02:00
|
|
|
case td_api::callbackQueryPayloadDataWithPassword::ID:
|
|
|
|
CHECK(password != nullptr);
|
|
|
|
flags = telegram_api::messages_getBotCallbackAnswer::DATA_MASK |
|
|
|
|
telegram_api::messages_getBotCallbackAnswer::PASSWORD_MASK;
|
|
|
|
data = BufferSlice(static_cast<const td_api::callbackQueryPayloadDataWithPassword *>(payload.get())->data_);
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
case td_api::callbackQueryPayloadGame::ID:
|
|
|
|
flags = telegram_api::messages_getBotCallbackAnswer::GAME_MASK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
|
2020-03-15 22:17:11 +01:00
|
|
|
auto net_query = G()->net_query_creator().create(telegram_api::messages_getBotCallbackAnswer(
|
2020-07-31 04:50:11 +02:00
|
|
|
flags, false /*ignored*/, std::move(input_peer), message_id.get_server_message_id().get(), std::move(data),
|
2020-08-18 08:32:37 +02:00
|
|
|
std::move(password)));
|
2020-06-10 01:44:54 +02:00
|
|
|
net_query->need_resend_on_503_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
send_query(std::move(net_query));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getBotCallbackAnswer>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-19 17:11:16 +02:00
|
|
|
auto answer = result_ptr.move_as_ok();
|
2021-11-01 19:53:23 +01:00
|
|
|
promise_.set_value(
|
|
|
|
td_api::make_object<td_api::callbackQueryAnswer>(answer->message_, answer->alert_, answer->url_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2023-11-12 20:55:48 +01:00
|
|
|
td_->messages_manager_->on_get_message_error(dialog_id_, message_id_, status, "GetBotCallbackAnswerQuery");
|
|
|
|
if (status.message() == "BOT_RESPONSE_TIMEOUT") {
|
2020-07-06 16:39:53 +02:00
|
|
|
status = Status::Error(502, "The bot is not responding");
|
2019-02-21 19:30:11 +01:00
|
|
|
}
|
2021-11-08 13:20:38 +01:00
|
|
|
if (status.code() == 502 && td_->messages_manager_->is_message_edited_recently({dialog_id_, message_id_}, 31)) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise_.set_value(td_api::make_object<td_api::callbackQueryAnswer>());
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SetBotCallbackAnswerQuery final : public Td::ResultHandler {
|
2018-12-31 20:04:05 +01:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetBotCallbackAnswerQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(int32 flags, int64 callback_query_id, const string &text, const string &url, int32 cache_time) {
|
2020-03-15 22:17:11 +01:00
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_setBotCallbackAnswer(
|
|
|
|
flags, false /*ignored*/, callback_query_id, text, url, cache_time)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_setBotCallbackAnswer>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.ok();
|
|
|
|
if (!result) {
|
|
|
|
LOG(INFO) << "Sending answer to a callback query has failed";
|
|
|
|
}
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
CallbackQueriesManager::CallbackQueriesManager(Td *td) : td_(td) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallbackQueriesManager::answer_callback_query(int64 callback_query_id, const string &text, bool show_alert,
|
|
|
|
const string &url, int32 cache_time, Promise<Unit> &&promise) const {
|
|
|
|
int32 flags = 0;
|
|
|
|
if (!text.empty()) {
|
|
|
|
flags |= BOT_CALLBACK_ANSWER_FLAG_HAS_MESSAGE;
|
|
|
|
}
|
|
|
|
if (show_alert) {
|
|
|
|
flags |= BOT_CALLBACK_ANSWER_FLAG_NEED_SHOW_ALERT;
|
|
|
|
}
|
|
|
|
if (!url.empty()) {
|
|
|
|
flags |= BOT_CALLBACK_ANSWER_FLAG_HAS_URL;
|
|
|
|
}
|
|
|
|
td_->create_handler<SetBotCallbackAnswerQuery>(std::move(promise))
|
|
|
|
->send(flags, callback_query_id, text, url, cache_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<td_api::CallbackQueryPayload> CallbackQueriesManager::get_query_payload(int32 flags, BufferSlice &&data,
|
|
|
|
string &&game_short_name) {
|
|
|
|
bool has_data = (flags & telegram_api::updateBotCallbackQuery::DATA_MASK) != 0;
|
|
|
|
bool has_game = (flags & telegram_api::updateBotCallbackQuery::GAME_SHORT_NAME_MASK) != 0;
|
|
|
|
if (has_data == has_game) {
|
|
|
|
LOG(ERROR) << "Receive wrong flags " << flags << " in a callback query";
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_data) {
|
|
|
|
return make_tl_object<td_api::callbackQueryPayloadData>(data.as_slice().str());
|
|
|
|
}
|
|
|
|
if (has_game) {
|
|
|
|
return make_tl_object<td_api::callbackQueryPayloadGame>(game_short_name);
|
|
|
|
}
|
|
|
|
UNREACHABLE();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CallbackQueriesManager::on_new_query(int32 flags, int64 callback_query_id, UserId sender_user_id,
|
|
|
|
DialogId dialog_id, MessageId message_id, BufferSlice &&data,
|
|
|
|
int64 chat_instance, string &&game_short_name) {
|
|
|
|
if (!dialog_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive new callback query in invalid " << dialog_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!sender_user_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive new callback query from invalid " << sender_user_id << " in " << dialog_id;
|
|
|
|
return;
|
|
|
|
}
|
2024-04-02 02:52:34 +02:00
|
|
|
LOG_IF(ERROR, !td_->user_manager_->have_user(sender_user_id)) << "Receive unknown " << sender_user_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
LOG(ERROR) << "Receive new callback query";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive new callback query from " << message_id << " in " << dialog_id << " sent by "
|
|
|
|
<< sender_user_id;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto payload = get_query_payload(flags, std::move(data), std::move(game_short_name));
|
|
|
|
if (payload == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-04 13:38:01 +01:00
|
|
|
td_->dialog_manager_->force_create_dialog(dialog_id, "on_new_callback_query", true);
|
2024-04-02 02:52:34 +02:00
|
|
|
send_closure(G()->td(), &Td::send_update,
|
|
|
|
td_api::make_object<td_api::updateNewCallbackQuery>(
|
|
|
|
callback_query_id, td_->user_manager_->get_user_id_object(sender_user_id, "updateNewCallbackQuery"),
|
|
|
|
td_->dialog_manager_->get_chat_id_object(dialog_id, "updateNewCallbackQuery"), message_id.get(),
|
|
|
|
chat_instance, std::move(payload)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CallbackQueriesManager::on_new_inline_query(
|
|
|
|
int32 flags, int64 callback_query_id, UserId sender_user_id,
|
2021-09-03 11:27:59 +02:00
|
|
|
tl_object_ptr<telegram_api::InputBotInlineMessageID> &&inline_message_id, BufferSlice &&data, int64 chat_instance,
|
2018-12-31 20:04:05 +01:00
|
|
|
string &&game_short_name) {
|
|
|
|
if (!sender_user_id.is_valid()) {
|
|
|
|
LOG(ERROR) << "Receive new callback query from invalid " << sender_user_id;
|
|
|
|
return;
|
|
|
|
}
|
2024-04-02 02:52:34 +02:00
|
|
|
LOG_IF(ERROR, !td_->user_manager_->have_user(sender_user_id)) << "Receive unknown " << sender_user_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!td_->auth_manager_->is_bot()) {
|
|
|
|
LOG(ERROR) << "Receive new callback query";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CHECK(inline_message_id != nullptr);
|
|
|
|
|
|
|
|
auto payload = get_query_payload(flags, std::move(data), std::move(game_short_name));
|
|
|
|
if (payload == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-03 00:05:11 +01:00
|
|
|
send_closure(
|
|
|
|
G()->td(), &Td::send_update,
|
|
|
|
make_tl_object<td_api::updateNewInlineCallbackQuery>(
|
2024-04-02 02:52:34 +02:00
|
|
|
callback_query_id, td_->user_manager_->get_user_id_object(sender_user_id, "updateNewInlineCallbackQuery"),
|
2018-02-03 00:05:11 +01:00
|
|
|
InlineQueriesManager::get_inline_message_id(std::move(inline_message_id)), chat_instance,
|
|
|
|
std::move(payload)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void CallbackQueriesManager::send_callback_query(MessageFullId message_full_id,
|
2021-09-27 22:22:10 +02:00
|
|
|
tl_object_ptr<td_api::CallbackQueryPayload> &&payload,
|
|
|
|
Promise<td_api::object_ptr<td_api::callbackQueryAnswer>> &&promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (td_->auth_manager_->is_bot()) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Bot can't send callback queries to other bot"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (payload == nullptr) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Payload must be non-empty"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
2024-01-04 13:26:42 +01:00
|
|
|
td_->dialog_manager_->have_dialog_force(dialog_id, "send_callback_query");
|
2024-01-03 21:07:50 +01:00
|
|
|
if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Read)) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't access the chat"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
if (!td_->messages_manager_->have_message_force(message_full_id, "send_callback_query")) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
if (message_full_id.get_message_id().is_valid_scheduled()) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't send callback queries from scheduled messages"));
|
2019-11-26 21:47:13 +01:00
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
if (!message_full_id.get_message_id().is_server()) {
|
2021-09-27 22:22:10 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Bad message identifier"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2023-03-22 13:05:05 +01:00
|
|
|
if (dialog_id.get_type() == DialogType::SecretChat) {
|
|
|
|
return promise.set_error(Status::Error(400, "Secret chat messages can't have callback buttons"));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-08-18 08:32:37 +02:00
|
|
|
if (payload->get_id() == td_api::callbackQueryPayloadDataWithPassword::ID) {
|
|
|
|
auto password = static_cast<const td_api::callbackQueryPayloadDataWithPassword *>(payload.get())->password_;
|
2021-09-27 22:22:10 +02:00
|
|
|
send_closure(
|
|
|
|
td_->password_manager_, &PasswordManager::get_input_check_password_srp, std::move(password),
|
2023-09-21 18:11:17 +02:00
|
|
|
PromiseCreator::lambda([this, message_full_id, payload = std::move(payload), promise = std::move(promise)](
|
2021-09-27 22:22:10 +02:00
|
|
|
Result<tl_object_ptr<telegram_api::InputCheckPasswordSRP>> result) mutable {
|
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
send_get_callback_answer_query(message_full_id, std::move(payload), result.move_as_ok(), std::move(promise));
|
2021-09-27 22:22:10 +02:00
|
|
|
}));
|
2020-08-18 08:32:37 +02:00
|
|
|
} else {
|
2023-09-21 18:11:17 +02:00
|
|
|
send_get_callback_answer_query(message_full_id, std::move(payload), nullptr, std::move(promise));
|
2020-08-18 08:32:37 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-18 08:32:37 +02:00
|
|
|
void CallbackQueriesManager::send_get_callback_answer_query(
|
2023-09-21 18:11:17 +02:00
|
|
|
MessageFullId message_full_id, tl_object_ptr<td_api::CallbackQueryPayload> &&payload,
|
2021-09-27 22:22:10 +02:00
|
|
|
tl_object_ptr<telegram_api::InputCheckPasswordSRP> &&password,
|
|
|
|
Promise<td_api::object_ptr<td_api::callbackQueryAnswer>> &&promise) {
|
2021-10-07 15:36:21 +02:00
|
|
|
TRY_STATUS_PROMISE(promise, G()->close_status());
|
2021-09-27 22:22:10 +02:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Read)) {
|
2020-08-18 08:32:37 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't access the chat"));
|
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
if (!td_->messages_manager_->have_message_force(message_full_id, "send_callback_query")) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
2020-08-18 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
td_->create_handler<GetBotCallbackAnswerQuery>(std::move(promise))
|
2023-09-21 18:11:17 +02:00
|
|
|
->send(dialog_id, message_full_id.get_message_id(), payload, std::move(password));
|
2020-08-18 08:32:37 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|