diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 0be412b01..65b7838c2 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -8214,6 +8214,10 @@ setDefaultGroupAdministratorRights default_group_administrator_rights:chatAdmini setDefaultChannelAdministratorRights default_channel_administrator_rights:chatAdministratorRights = Ok; +//@description Checks whether the specified bot can send messages to the user. Returns a 404 error if can't and the access can be granted by call to allowBotToSendMessages @bot_user_id Identifier of the target bot +canBotSendMessages bot_user_id:int53 = Ok; + + //@description Sets the name of a bot. Can be called only if userTypeBot.can_be_edited == true //@bot_user_id Identifier of the target bot //@language_code A two-letter ISO 639-1 language code. If empty, the name will be shown to all users for whose languages there is no dedicated name diff --git a/td/telegram/BotInfoManager.cpp b/td/telegram/BotInfoManager.cpp index 741617337..ab5b169d6 100644 --- a/td/telegram/BotInfoManager.cpp +++ b/td/telegram/BotInfoManager.cpp @@ -89,6 +89,40 @@ class SetBotBroadcastDefaultAdminRightsQuery final : public Td::ResultHandler { } }; +class CanBotSendMessageQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit CanBotSendMessageQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(UserId bot_user_id) { + auto r_input_user = td_->contacts_manager_->get_input_user(bot_user_id); + if (r_input_user.is_error()) { + return on_error(r_input_user.move_as_error()); + } + send_query( + G()->net_query_creator().create(telegram_api::bots_canSendMessage(r_input_user.move_as_ok()), {{bot_user_id}})); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + if (result_ptr.ok()) { + promise_.set_value(Unit()); + } else { + promise_.set_error(Status::Error(404, "Not Found")); + } + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + static Result> get_bot_input_user(const Td *td, UserId bot_user_id) { if (td->auth_manager_->is_bot()) { if (bot_user_id != UserId() && bot_user_id != td->contacts_manager_->get_my_id()) { @@ -317,6 +351,10 @@ void BotInfoManager::set_default_channel_administrator_rights(AdministratorRight td_->create_handler(std::move(promise))->send(administrator_rights); } +void BotInfoManager::can_bot_send_messages(UserId bot_user_id, Promise &&promise) { + td_->create_handler(std::move(promise))->send(bot_user_id); +} + void BotInfoManager::add_pending_set_query(UserId bot_user_id, const string &language_code, int type, const string &value, Promise &&promise) { pending_set_bot_info_queries_.emplace_back(bot_user_id, language_code, type, value, std::move(promise)); diff --git a/td/telegram/BotInfoManager.h b/td/telegram/BotInfoManager.h index 4c5f99fb9..d8294aeeb 100644 --- a/td/telegram/BotInfoManager.h +++ b/td/telegram/BotInfoManager.h @@ -26,6 +26,8 @@ class BotInfoManager final : public Actor { void set_default_channel_administrator_rights(AdministratorRights administrator_rights, Promise &&promise); + void can_bot_send_messages(UserId bot_user_id, Promise &&promise); + void set_bot_name(UserId bot_user_id, const string &language_code, const string &name, Promise &&promise); void get_bot_name(UserId bot_user_id, const string &language_code, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 5282369fd..1fd05cd06 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -7307,6 +7307,11 @@ void Td::on_request(uint64 id, const td_api::setDefaultChannelAdministratorRight AdministratorRights(request.default_channel_administrator_rights_, ChannelType::Broadcast), std::move(promise)); } +void Td::on_request(uint64 id, const td_api::canBotSendMessages &request) { + CREATE_OK_REQUEST_PROMISE(); + bot_info_manager_->can_bot_send_messages(UserId(request.bot_user_id_), std::move(promise)); +} + void Td::on_request(uint64 id, td_api::setBotName &request) { CLEAN_INPUT_STRING(request.name_); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index e226fbb6c..37445b44f 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1221,6 +1221,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::setDefaultChannelAdministratorRights &request); + void on_request(uint64 id, const td_api::canBotSendMessages &request); + void on_request(uint64 id, td_api::setBotName &request); void on_request(uint64 id, const td_api::getBotName &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index af1b1ddbf..fb2fc7659 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5510,6 +5510,10 @@ class CliClient final : public Actor { InputChatPhoto input_chat_photo; get_args(args, user_id, input_chat_photo); send_request(td_api::make_object(user_id, input_chat_photo)); + } else if (op == "cbsm") { + UserId bot_user_id; + get_args(args, bot_user_id); + send_request(td_api::make_object(bot_user_id)); } else if (op == "gbi") { UserId bot_user_id; string language_code;