Add getCommands method.

This commit is contained in:
levlam 2021-06-19 04:18:02 +03:00
parent 62a543cb35
commit 1202d55e27
5 changed files with 64 additions and 0 deletions

View File

@ -350,6 +350,9 @@ userTypeUnknown = UserType;
//@description Represents a command supported by a bot @command Text of the bot command @param_description Description of the bot command
botCommand command:string description:string = BotCommand;
//@description Contains a list of bot commands @commands List of bot commands
botCommands commands:vector<botCommand> = BotCommands;
//@description Provides information about a bot and its supported commands @param_description Long description shown on the user info page @commands A list of commands supported by the bot
botInfo description:string commands:vector<botCommand> = BotInfo;
@ -5014,11 +5017,16 @@ resendChangePhoneNumberCode = AuthenticationCodeInfo;
//@description Checks the authentication code sent to confirm a new phone number of the user @code Verification code received by SMS, phone call or flash call
checkChangePhoneNumberCode code:string = Ok;
//@description Sets the list of commands supported by the bot for the given user scope and language; for bots only @scope The scope to which the commands are applied
//@language_code A two-letter ISO 639-1 country code. If empty, the commands will be applied to all users from the given scope, for which language there are no dedicated commands
//@commands List of the bot's commands
setCommands scope:BotCommandScope language_code:string commands:vector<botCommand> = Ok;
//@description Returns the list of commands supported by the bot for the given user scope and language; for bots only @scope The scope to which the commands are applied
//@language_code A two-letter ISO 639-1 country code or an empty string
getCommands scope:BotCommandScope language_code:string = BotCommands;
//@description Returns all active sessions of the current user
getActiveSessions = Sessions;

View File

@ -1011,6 +1011,36 @@ class SetBotCommandsQuery : public Td::ResultHandler {
}
};
class GetBotCommandsQuery : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::botCommands>> promise_;
public:
explicit GetBotCommandsQuery(Promise<td_api::object_ptr<td_api::botCommands>> &&promise)
: promise_(std::move(promise)) {
}
void send(BotCommandScope scope, const string &language_code) {
send_query(G()->net_query_creator().create(
telegram_api::bots_getBotCommands(scope.get_input_bot_command_scope(td), language_code)));
}
void on_result(uint64 id, BufferSlice packet) override {
auto result_ptr = fetch_result<telegram_api::bots_getBotCommands>(packet);
if (result_ptr.is_error()) {
return on_error(id, result_ptr.move_as_error());
}
auto commands = transform(result_ptr.move_as_ok(), [](auto &&command) {
return td_api::make_object<td_api::botCommand>(command->command_, command->description_);
});
promise_.set_value(td_api::make_object<td_api::botCommands>(std::move(commands)));
}
void on_error(uint64 id, Status status) override {
promise_.set_error(std::move(status));
}
};
class CheckChannelUsernameQuery : public Td::ResultHandler {
Promise<bool> promise_;
ChannelId channel_id_;
@ -6307,6 +6337,21 @@ void ContactsManager::set_commands(td_api::object_ptr<td_api::BotCommandScope> &
td_->create_handler<SetBotCommandsQuery>(std::move(promise))->send(scope, language_code, std::move(new_commands));
}
void ContactsManager::get_commands(td_api::object_ptr<td_api::BotCommandScope> &&scope_ptr, string &&language_code,
Promise<td_api::object_ptr<td_api::botCommands>> &&promise) {
TRY_RESULT_PROMISE(promise, scope, BotCommandScope::get_bot_command_scope(td_, std::move(scope_ptr)));
if (!language_code.empty() && (language_code.size() != 2 || language_code[0] < 'a' || language_code[0] > 'z' ||
language_code[1] < 'a' || language_code[1] > 'z')) {
return promise.set_error(Status::Error(400, "Invalid language code specified"));
}
if (!scope.have_input_bot_command_scope(td_)) {
return promise.set_error(Status::Error(400, "Invalid scope specified"));
}
td_->create_handler<GetBotCommandsQuery>(std::move(promise))->send(scope, language_code);
}
void ContactsManager::set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise) {
auto new_description = strip_empty_characters(description, MAX_DESCRIPTION_LENGTH);
auto c = get_chat(chat_id);

View File

@ -331,6 +331,9 @@ class ContactsManager : public Actor {
void set_commands(td_api::object_ptr<td_api::BotCommandScope> &&scope_ptr, string &&language_code,
vector<td_api::object_ptr<td_api::botCommand>> &&commands, Promise<Unit> &&promise);
void get_commands(td_api::object_ptr<td_api::BotCommandScope> &&scope_ptr, string &&language_code,
Promise<td_api::object_ptr<td_api::botCommands>> &&promise);
void set_chat_description(ChatId chat_id, const string &description, Promise<Unit> &&promise);
void set_channel_username(ChannelId channel_id, const string &username, Promise<Unit> &&promise);

View File

@ -6878,6 +6878,12 @@ void Td::on_request(uint64 id, td_api::setCommands &request) {
std::move(request.commands_), std::move(promise));
}
void Td::on_request(uint64 id, td_api::getCommands &request) {
CHECK_IS_BOT();
CREATE_REQUEST_PROMISE();
contacts_manager_->get_commands(std::move(request.scope_), std::move(request.language_code_), std::move(promise));
}
void Td::on_request(uint64 id, const td_api::setLocation &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();

View File

@ -920,6 +920,8 @@ class Td final : public NetQueryCallback {
void on_request(uint64 id, td_api::setCommands &request);
void on_request(uint64 id, td_api::getCommands &request);
void on_request(uint64 id, const td_api::setLocation &request);
void on_request(uint64 id, td_api::setProfilePhoto &request);