aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/BotCommand.cpp
blob: b8e3a04248bdab76a78c63ec6dbf1a6c1b70a72b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2026
//
// 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/BotCommand.h"

#include "td/telegram/BotCommandScope.h"
#include "td/telegram/Global.h"
#include "td/telegram/misc.h"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/UserManager.h"

#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
#include "td/utils/SliceBuilder.h"
#include "td/utils/Status.h"
#include "td/utils/utf8.h"

#include <algorithm>

namespace td {

class SetBotCommandsQuery final : public Td::ResultHandler {
  Promise<Unit> promise_;

 public:
  explicit SetBotCommandsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
  }

  void send(BotCommandScope scope, const string &language_code, vector<BotCommand> &&commands) {
    send_query(G()->net_query_creator().create(telegram_api::bots_setBotCommands(
        scope.get_input_bot_command_scope(td_), language_code,
        transform(commands, [](const BotCommand &command) { return command.get_input_bot_command(); }))));
  }

  void on_result(BufferSlice packet) final {
    auto result_ptr = fetch_result<telegram_api::bots_setBotCommands>(packet);
    if (result_ptr.is_error()) {
      return on_error(result_ptr.move_as_error());
    }

    if (!result_ptr.ok()) {
      LOG(ERROR) << "Set bot commands request failed";
    }
    promise_.set_value(Unit());
  }

  void on_error(Status status) final {
    promise_.set_error(std::move(status));
  }
};

class ResetBotCommandsQuery final : public Td::ResultHandler {
  Promise<Unit> promise_;

 public:
  explicit ResetBotCommandsQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
  }

  void send(BotCommandScope scope, const string &language_code) {
    send_query(G()->net_query_creator().create(
        telegram_api::bots_resetBotCommands(scope.get_input_bot_command_scope(td_), language_code)));
  }

  void on_result(BufferSlice packet) final {
    auto result_ptr = fetch_result<telegram_api::bots_resetBotCommands>(packet);
    if (result_ptr.is_error()) {
      return on_error(result_ptr.move_as_error());
    }

    promise_.set_value(Unit());
  }

  void on_error(Status status) final {
    promise_.set_error(std::move(status));
  }
};

class GetBotCommandsQuery final : 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(BufferSlice packet) final {
    auto result_ptr = fetch_result<telegram_api::bots_getBotCommands>(packet);
    if (result_ptr.is_error()) {
      return on_error(result_ptr.move_as_error());
    }

    BotCommands commands(td_->user_manager_->get_my_id(), result_ptr.move_as_ok());
    promise_.set_value(commands.get_bot_commands_object(td_));
  }

  void on_error(Status status) final {
    promise_.set_error(std::move(status));
  }
};

BotCommand::BotCommand(telegram_api::object_ptr<telegram_api::botCommand> &&bot_command) {
  CHECK(bot_command != nullptr);
  command_ = std::move(bot_command->command_);
  description_ = std::move(bot_command->description_);
  is_ephemeral_ = bot_command->ephemeral_;
}

td_api::object_ptr<td_api::botCommand> BotCommand::get_bot_command_object() const {
  return td_api::make_object<td_api::botCommand>(command_, description_, is_ephemeral_);
}

telegram_api::object_ptr<telegram_api::botCommand> BotCommand::get_input_bot_command() const {
  return telegram_api::make_object<telegram_api::botCommand>(0, is_ephemeral_, command_, description_);
}

bool operator==(const BotCommand &lhs, const BotCommand &rhs) {
  return lhs.command_ == rhs.command_ && lhs.description_ == rhs.description_ && lhs.is_ephemeral_ == rhs.is_ephemeral_;
}

BotCommands::BotCommands(UserId bot_user_id, vector<telegram_api::object_ptr<telegram_api::botCommand>> &&bot_commands)
    : bot_user_id_(bot_user_id) {
  commands_ = transform(std::move(bot_commands), [](telegram_api::object_ptr<telegram_api::botCommand> &&bot_command) {
    return BotCommand(std::move(bot_command));
  });
}

td_api::object_ptr<td_api::botCommands> BotCommands::get_bot_commands_object(Td *td) const {
  auto commands = transform(commands_, [](const auto &command) { return command.get_bot_command_object(); });
  return td_api::make_object<td_api::botCommands>(
      td->user_manager_->get_user_id_object(bot_user_id_, "get_bot_commands_object"), std::move(commands));
}

bool BotCommands::update_all_bot_commands(vector<BotCommands> &all_bot_commands, BotCommands &&bot_commands) {
  auto is_from_bot = [bot_user_id = bot_commands.bot_user_id_](const BotCommands &commands) {
    return commands.bot_user_id_ == bot_user_id;
  };

  if (bot_commands.commands_.empty()) {
    return td::remove_if(all_bot_commands, is_from_bot);
  }
  auto it = std::find_if(all_bot_commands.begin(), all_bot_commands.end(), is_from_bot);
  if (it != all_bot_commands.end()) {
    if (*it != bot_commands) {
      *it = std::move(bot_commands);
      return true;
    }
    return false;
  }
  all_bot_commands.push_back(std::move(bot_commands));
  return true;
}

bool operator==(const BotCommands &lhs, const BotCommands &rhs) {
  return lhs.bot_user_id_ == rhs.bot_user_id_ && lhs.commands_ == rhs.commands_;
}

void set_commands(Td *td, td_api::object_ptr<td_api::BotCommandScope> &&scope_ptr, string &&language_code,
                  vector<td_api::object_ptr<td_api::botCommand>> &&commands, Promise<Unit> &&promise) {
  TRY_RESULT_PROMISE(promise, scope, BotCommandScope::get_bot_command_scope(td, std::move(scope_ptr)));
  TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));

  vector<BotCommand> new_commands;
  for (auto &command : commands) {
    if (command == nullptr) {
      return promise.set_error(400, "Command must be non-empty");
    }
    if (!clean_input_string(command->command_)) {
      return promise.set_error(400, "Command must be encoded in UTF-8");
    }
    if (!clean_input_string(command->description_)) {
      return promise.set_error(400, "Command description must be encoded in UTF-8");
    }

    const size_t MAX_COMMAND_TEXT_LENGTH = 32;
    command->command_ = trim(command->command_);
    if (command->command_[0] == '/') {
      command->command_ = command->command_.substr(1);
    }
    if (command->command_.empty()) {
      return promise.set_error(400, "Command must be non-empty");
    }
    if (utf8_length(command->command_) > MAX_COMMAND_TEXT_LENGTH) {
      return promise.set_error(400, PSLICE() << "Command length must not exceed " << MAX_COMMAND_TEXT_LENGTH);
    }

    const size_t MAX_COMMAND_DESCRIPTION_LENGTH = 256;
    command->description_ = trim(command->description_);
    auto description_length = utf8_length(command->description_);
    if (command->description_.empty()) {
      return promise.set_error(400, "Command description must be non-empty");
    }
    if (description_length > MAX_COMMAND_DESCRIPTION_LENGTH) {
      return promise.set_error(
          400, PSLICE() << "Command description length must not exceed " << MAX_COMMAND_DESCRIPTION_LENGTH);
    }

    new_commands.emplace_back(std::move(command->command_), std::move(command->description_), command->is_ephemeral_);
  }

  td->create_handler<SetBotCommandsQuery>(std::move(promise))->send(scope, language_code, std::move(new_commands));
}

void delete_commands(Td *td, td_api::object_ptr<td_api::BotCommandScope> &&scope_ptr, string &&language_code,
                     Promise<Unit> &&promise) {
  TRY_RESULT_PROMISE(promise, scope, BotCommandScope::get_bot_command_scope(td, std::move(scope_ptr)));
  TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));

  td->create_handler<ResetBotCommandsQuery>(std::move(promise))->send(scope, language_code);
}

void get_commands(Td *td, 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)));
  TRY_STATUS_PROMISE(promise, validate_bot_language_code(language_code));

  td->create_handler<GetBotCommandsQuery>(std::move(promise))->send(scope, language_code);
}

}  // namespace td