// // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024 // // 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/BotQueries.h" #include "td/telegram/Global.h" #include "td/telegram/Td.h" #include "td/telegram/telegram_api.h" #include "td/utils/buffer.h" #include "td/utils/logging.h" namespace td { class SendCustomRequestQuery final : public Td::ResultHandler { Promise> promise_; public: explicit SendCustomRequestQuery(Promise> &&promise) : promise_(std::move(promise)) { } void send(const string &method, const string ¶meters) { send_query(G()->net_query_creator().create( telegram_api::bots_sendCustomRequest(method, telegram_api::make_object(parameters)))); } 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()); } auto result = result_ptr.move_as_ok(); promise_.set_value(td_api::make_object(result->data_)); } void on_error(Status status) final { promise_.set_error(std::move(status)); } }; class AnswerCustomQueryQuery final : public Td::ResultHandler { Promise promise_; public: explicit AnswerCustomQueryQuery(Promise &&promise) : promise_(std::move(promise)) { } void send(int64 custom_query_id, const string &data) { send_query(G()->net_query_creator().create(telegram_api::bots_answerWebhookJSONQuery( custom_query_id, telegram_api::make_object(data)))); } 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()); } bool result = result_ptr.ok(); if (!result) { LOG(INFO) << "Sending answer to a custom query has failed"; } promise_.set_value(Unit()); } void on_error(Status status) final { promise_.set_error(std::move(status)); } }; void send_bot_custom_query(Td *td, const string &method, const string ¶meters, Promise> &&promise) { td->create_handler(std::move(promise))->send(method, parameters); } void answer_bot_custom_query(Td *td, int64 custom_query_id, const string &data, Promise &&promise) { td->create_handler(std::move(promise))->send(custom_query_id, data); } } // namespace td