Allow to edit description of owned bots.

This commit is contained in:
levlam 2023-03-28 17:49:48 +03:00
parent aec7d5a26f
commit 92f1d26f08
5 changed files with 99 additions and 34 deletions

View File

@ -401,7 +401,7 @@ photo has_stickers:Bool minithumbnail:minithumbnail sizes:vector<photoSize> = Ph
//@description Describes a sticker
//@id Unique sticker identifier within the set; 0 if none
//@set_id The identifier of the sticker set to which the sticker belongs; 0 if none
//@set_id Identifier of the sticker set to which the sticker belongs; 0 if none
//@width Sticker width; as defined by the sender
//@height Sticker height; as defined by the sender
//@emoji Emoji corresponding to the sticker
@ -6556,7 +6556,7 @@ shareChatWithBot chat_id:int53 message_id:int53 button_id:int32 shared_chat_id:i
//@description Sends an inline query to a bot and returns its results. Returns an error with code 502 if the bot fails to answer the query before the query timeout expires
//@bot_user_id The identifier of the target bot
//@bot_user_id Identifier of the target bot
//@chat_id Identifier of the chat where the query was sent
//@user_location Location of the user; pass null if unknown or the bot doesn't need user's location
//@query Text of the query
@ -7635,23 +7635,27 @@ setDefaultGroupAdministratorRights default_group_administrator_rights:chatAdmini
setDefaultChannelAdministratorRights default_channel_administrator_rights:chatAdministratorRights = Ok;
//@description Sets the text shown in the chat with the bot if the chat is empty; bots only
//@description Sets the text shown in the chat with a bot if the chat is empty. 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 description will be shown to all users, for which language there are no dedicated description
//@param_description New bot's description on the specified language
setBotInfoDescription language_code:string description:string = Ok;
setBotInfoDescription bot_user_id:int53 language_code:string description:string = Ok;
//@description Returns the text shown in the chat with the bot if the chat is empty in the given language; bots only
//@description Returns the text shown in the chat with a bot if the chat is empty in the given language. 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 or an empty string
getBotInfoDescription language_code:string = Text;
getBotInfoDescription bot_user_id:int53 language_code:string = Text;
//@description Sets the text shown on the bot's profile page and sent together with the link when users share the bot; bots only
//@description Sets the text shown on a bot's profile page and sent together with the link when users share the 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 short description will be shown to all users, for which language there are no dedicated description
//@short_description New bot's short description on the specified language
setBotInfoShortDescription language_code:string short_description:string = Ok;
setBotInfoShortDescription bot_user_id:int53 language_code:string short_description:string = Ok;
//@description Returns the text shown on the bot's profile page and sent together with the link when users share the bot in the given language; bots only
//@description Returns the text shown on a bot's profile page and sent together with the link when users share the bot in the given language. 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 or an empty string
getBotInfoShortDescription language_code:string = Text;
getBotInfoShortDescription bot_user_id:int53 language_code:string = Text;
//@description Returns all active sessions of the current user

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/Account.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/DeviceTokenManager.h"
#include "td/telegram/Global.h"
@ -590,6 +591,21 @@ class SetBotBroadcastDefaultAdminRightsQuery final : public Td::ResultHandler {
}
};
static Result<telegram_api::object_ptr<telegram_api::InputUser>> 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()) {
return Status::Error(400, "Invalid bot user identifier specified");
}
} else {
TRY_RESULT(bot_data, td->contacts_manager_->get_bot_data(bot_user_id));
if (!bot_data.can_be_edited) {
return Status::Error(400, "The bot can't be edited");
}
return td->contacts_manager_->get_input_user(bot_user_id);
}
return nullptr;
}
class SetBotInfoQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
@ -597,7 +613,7 @@ class SetBotInfoQuery final : public Td::ResultHandler {
explicit SetBotInfoQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(const string &language_code, bool set_about, const string &about, bool set_description,
void send(UserId bot_user_id, const string &language_code, bool set_about, const string &about, bool set_description,
const string &description) {
int32 flags = 0;
if (set_about) {
@ -606,8 +622,16 @@ class SetBotInfoQuery final : public Td::ResultHandler {
if (set_description) {
flags |= telegram_api::bots_setBotInfo::DESCRIPTION_MASK;
}
auto r_input_user = get_bot_input_user(td_, bot_user_id);
if (r_input_user.is_error()) {
return on_error(r_input_user.move_as_error());
}
if (r_input_user.ok() != nullptr) {
flags |= telegram_api::bots_setBotInfo::BOT_MASK;
}
send_query(G()->net_query_creator().create(
telegram_api::bots_setBotInfo(flags, nullptr, language_code, string(), about, description), {{"me"}}));
telegram_api::bots_setBotInfo(flags, r_input_user.move_as_ok(), language_code, string(), about, description),
{{bot_user_id}}));
}
void on_result(BufferSlice packet) final {
@ -636,10 +660,18 @@ class GetBotInfoQuery final : public Td::ResultHandler {
explicit GetBotInfoQuery(Promise<string> &&promise) : promise_(std::move(promise)) {
}
void send(const string &language_code, size_t index) {
void send(UserId bot_user_id, const string &language_code, size_t index) {
index_ = index;
int32 flags = 0;
send_query(G()->net_query_creator().create(telegram_api::bots_getBotInfo(flags, nullptr, language_code), {{"me"}}));
auto r_input_user = get_bot_input_user(td_, bot_user_id);
if (r_input_user.is_error()) {
return on_error(r_input_user.move_as_error());
}
if (r_input_user.ok() != nullptr) {
flags |= telegram_api::bots_getBotInfo::BOT_MASK;
}
send_query(G()->net_query_creator().create(
telegram_api::bots_getBotInfo(flags, r_input_user.move_as_ok(), language_code), {{bot_user_id}}));
}
void on_result(BufferSlice packet) final {
@ -804,26 +836,30 @@ void set_default_channel_administrator_rights(Td *td, AdministratorRights admini
td->create_handler<SetBotBroadcastDefaultAdminRightsQuery>(std::move(promise))->send(administrator_rights);
}
void set_bot_info_description(Td *td, const string &language_code, const string &description, Promise<Unit> &&promise) {
void set_bot_info_description(Td *td, UserId bot_user_id, const string &language_code, const string &description,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));
td->contacts_manager_->invalidate_user_full(td->contacts_manager_->get_my_id());
td->create_handler<SetBotInfoQuery>(std::move(promise))->send(language_code, false, string(), true, description);
td->create_handler<SetBotInfoQuery>(std::move(promise))
->send(bot_user_id, language_code, false, string(), true, description);
}
void get_bot_info_description(Td *td, const string &language_code, Promise<string> &&promise) {
void get_bot_info_description(Td *td, UserId bot_user_id, const string &language_code, Promise<string> &&promise) {
TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));
td->create_handler<GetBotInfoQuery>(std::move(promise))->send(language_code, 1);
td->create_handler<GetBotInfoQuery>(std::move(promise))->send(bot_user_id, language_code, 1);
}
void set_bot_info_about(Td *td, const string &language_code, const string &about, Promise<Unit> &&promise) {
void set_bot_info_about(Td *td, UserId bot_user_id, const string &language_code, const string &about,
Promise<Unit> &&promise) {
TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));
td->contacts_manager_->invalidate_user_full(td->contacts_manager_->get_my_id());
td->create_handler<SetBotInfoQuery>(std::move(promise))->send(language_code, true, about, false, string());
td->create_handler<SetBotInfoQuery>(std::move(promise))
->send(bot_user_id, language_code, true, about, false, string());
}
void get_bot_info_about(Td *td, const string &language_code, Promise<string> &&promise) {
void get_bot_info_about(Td *td, UserId bot_user_id, const string &language_code, Promise<string> &&promise) {
TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));
td->create_handler<GetBotInfoQuery>(std::move(promise))->send(language_code, 0);
td->create_handler<GetBotInfoQuery>(std::move(promise))->send(bot_user_id, language_code, 0);
}
void export_contact_token(Td *td, Promise<td_api::object_ptr<td_api::userLink>> &&promise) {

View File

@ -8,6 +8,7 @@
#include "td/telegram/DialogParticipant.h"
#include "td/telegram/td_api.h"
#include "td/telegram/UserId.h"
#include "td/utils/common.h"
#include "td/utils/Promise.h"
@ -50,13 +51,15 @@ void set_default_group_administrator_rights(Td *td, AdministratorRights administ
void set_default_channel_administrator_rights(Td *td, AdministratorRights administrator_rights,
Promise<Unit> &&promise);
void set_bot_info_description(Td *td, const string &language_code, const string &description, Promise<Unit> &&promise);
void set_bot_info_description(Td *td, UserId bot_user_id, const string &language_code, const string &description,
Promise<Unit> &&promise);
void get_bot_info_description(Td *td, const string &language_code, Promise<string> &&promise);
void get_bot_info_description(Td *td, UserId bot_user_id, const string &language_code, Promise<string> &&promise);
void set_bot_info_about(Td *td, const string &language_code, const string &about, Promise<Unit> &&promise);
void set_bot_info_about(Td *td, UserId bot_user_id, const string &language_code, const string &about,
Promise<Unit> &&promise);
void get_bot_info_about(Td *td, const string &language_code, Promise<string> &&promise);
void get_bot_info_about(Td *td, UserId bot_user_id, const string &language_code, Promise<string> &&promise);
void export_contact_token(Td *td, Promise<td_api::object_ptr<td_api::userLink>> &&promise);

View File

@ -7005,14 +7005,13 @@ void Td::on_request(uint64 id, const td_api::setDefaultChannelAdministratorRight
}
void Td::on_request(uint64 id, td_api::setBotInfoDescription &request) {
CHECK_IS_BOT();
CLEAN_INPUT_STRING(request.description_);
CREATE_OK_REQUEST_PROMISE();
set_bot_info_description(this, request.language_code_, request.description_, std::move(promise));
set_bot_info_description(this, UserId(request.bot_user_id_), request.language_code_, request.description_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getBotInfoDescription &request) {
CHECK_IS_BOT();
CREATE_REQUEST_PROMISE();
auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result<string> result) mutable {
if (result.is_error()) {
@ -7021,18 +7020,17 @@ void Td::on_request(uint64 id, const td_api::getBotInfoDescription &request) {
promise.set_value(td_api::make_object<td_api::text>(result.move_as_ok()));
}
});
get_bot_info_description(this, request.language_code_, std::move(query_promise));
get_bot_info_description(this, UserId(request.bot_user_id_), request.language_code_, std::move(query_promise));
}
void Td::on_request(uint64 id, td_api::setBotInfoShortDescription &request) {
CHECK_IS_BOT();
CLEAN_INPUT_STRING(request.short_description_);
CREATE_OK_REQUEST_PROMISE();
set_bot_info_about(this, request.language_code_, request.short_description_, std::move(promise));
set_bot_info_about(this, UserId(request.bot_user_id_), request.language_code_, request.short_description_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getBotInfoShortDescription &request) {
CHECK_IS_BOT();
CREATE_REQUEST_PROMISE();
auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result<string> result) mutable {
if (result.is_error()) {
@ -7041,7 +7039,7 @@ void Td::on_request(uint64 id, const td_api::getBotInfoShortDescription &request
promise.set_value(td_api::make_object<td_api::text>(result.move_as_ok()));
}
});
get_bot_info_about(this, request.language_code_, std::move(query_promise));
get_bot_info_about(this, UserId(request.bot_user_id_), request.language_code_, std::move(query_promise));
}
void Td::on_request(uint64 id, const td_api::setLocation &request) {

View File

@ -5016,6 +5016,30 @@ class CliClient final : public Actor {
InputChatPhoto input_chat_photo;
get_args(args, user_id, input_chat_photo);
send_request(td_api::make_object<td_api::suggestUserProfilePhoto>(user_id, input_chat_photo));
} else if (op == "sbid") {
UserId bot_user_id;
string language_code;
string description;
get_args(args, bot_user_id, language_code, description);
send_request(td_api::make_object<td_api::setBotInfoDescription>(bot_user_id, language_code, description));
} else if (op == "gbid") {
UserId bot_user_id;
string language_code;
string description;
get_args(args, bot_user_id, language_code);
send_request(td_api::make_object<td_api::getBotInfoDescription>(bot_user_id, language_code));
} else if (op == "sbisd") {
UserId bot_user_id;
string language_code;
string short_description;
get_args(args, bot_user_id, language_code, short_description);
send_request(
td_api::make_object<td_api::setBotInfoShortDescription>(bot_user_id, language_code, short_description));
} else if (op == "gbisd") {
UserId bot_user_id;
string language_code;
get_args(args, bot_user_id, language_code);
send_request(td_api::make_object<td_api::getBotInfoShortDescription>(bot_user_id, language_code));
} else if (op == "sh") {
const string &prefix = args;
send_request(td_api::make_object<td_api::searchHashtags>(prefix, 10));