Add td_api::getBusinessConnection.

This commit is contained in:
levlam 2024-03-07 18:02:30 +03:00
parent 74cf61d401
commit f78a0e4326
6 changed files with 129 additions and 1 deletions

View File

@ -8022,6 +8022,10 @@ stopPoll chat_id:int53 message_id:int53 reply_markup:ReplyMarkup = Ok;
hideSuggestedAction action:SuggestedAction = Ok;
//@description Returns information about a business connection by its identifier; for bots only @connection_id Identifier of the business connection to return
getBusinessConnection connection_id:string = BusinessConnection;
//@description Returns information about a button of type inlineKeyboardButtonTypeLoginUrl. The method needs to be called when the user presses the button
//@chat_id Chat identifier of the message with the button
//@message_id Message identifier of the message with the button

View File

@ -18,6 +18,34 @@
namespace td {
class GetBotBusinessConnectionQuery final : public Td::ResultHandler {
Promise<telegram_api::object_ptr<telegram_api::Updates>> promise_;
public:
explicit GetBotBusinessConnectionQuery(Promise<telegram_api::object_ptr<telegram_api::Updates>> &&promise)
: promise_(std::move(promise)) {
}
void send(const string &connection_id) {
send_query(G()->net_query_creator().create(telegram_api::account_getBotBusinessConnection(connection_id)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::account_getBotBusinessConnection>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(DEBUG) << "Receive result for GetBotBusinessConnectionQuery: " << to_string(ptr);
promise_.set_value(std::move(ptr));
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
struct BusinessConnectionManager::BusinessConnection {
string connection_id_;
UserId user_id_;
@ -78,4 +106,78 @@ void BusinessConnectionManager::on_update_bot_business_connect(
td_api::make_object<td_api::updateBusinessConnection>(stored_connection->get_business_connection_object(td_)));
}
void BusinessConnectionManager::get_business_connection(
const string &connection_id, Promise<td_api::object_ptr<td_api::businessConnection>> &&promise) {
auto connection = business_connections_.get_pointer(connection_id);
if (connection != nullptr) {
return promise.set_value(connection->get_business_connection_object(td_));
}
if (connection_id.empty()) {
return promise.set_error(Status::Error(400, "Connection iedntifier must be non-empty"));
}
auto &queries = get_business_connection_queries_[connection_id];
queries.push_back(std::move(promise));
if (queries.size() == 1u) {
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), connection_id](Result<telegram_api::object_ptr<telegram_api::Updates>> r_updates) {
send_closure(actor_id, &BusinessConnectionManager::on_get_business_connection, connection_id,
std::move(r_updates));
});
td_->create_handler<GetBotBusinessConnectionQuery>(std::move(query_promise))->send(connection_id);
}
}
void BusinessConnectionManager::on_get_business_connection(
const string &connection_id, Result<telegram_api::object_ptr<telegram_api::Updates>> r_updates) {
G()->ignore_result_if_closing(r_updates);
auto queries_it = get_business_connection_queries_.find(connection_id);
CHECK(queries_it != get_business_connection_queries_.end());
CHECK(!queries_it->second.empty());
auto promises = std::move(queries_it->second);
get_business_connection_queries_.erase(queries_it);
if (r_updates.is_error()) {
return fail_promises(promises, r_updates.move_as_error());
}
auto connection = business_connections_.get_pointer(connection_id);
if (connection != nullptr) {
for (auto &promise : promises) {
promise.set_value(connection->get_business_connection_object(td_));
}
return;
}
auto updates_ptr = r_updates.move_as_ok();
if (updates_ptr->get_id() != telegram_api::updates::ID) {
LOG(ERROR) << "Receive " << to_string(updates_ptr);
return fail_promises(promises, Status::Error(500, "Receive invalid business connection info"));
}
auto updates = telegram_api::move_object_as<telegram_api::updates>(updates_ptr);
if (updates->updates_.size() != 1 || updates->updates_[0]->get_id() != telegram_api::updateBotBusinessConnect::ID) {
if (updates->updates_.empty()) {
return fail_promises(promises, Status::Error(400, "Business connnection not found"));
}
LOG(ERROR) << "Receive " << to_string(updates);
return fail_promises(promises, Status::Error(500, "Receive invalid business connection info"));
}
auto update = telegram_api::move_object_as<telegram_api::updateBotBusinessConnect>(updates->updates_[0]);
td_->contacts_manager_->on_get_users(std::move(updates->users_), "on_get_business_connection");
td_->contacts_manager_->on_get_chats(std::move(updates->chats_), "on_get_business_connection");
auto business_connection = make_unique<BusinessConnection>(update->connection_);
if (!business_connection->is_valid() || connection_id != business_connection->connection_id_) {
LOG(ERROR) << "Receive for " << connection_id << ": " << to_string(update->connection_);
return fail_promises(promises, Status::Error(500, "Receive invalid business connection info"));
}
auto &stored_connection = business_connections_[connection_id];
CHECK(stored_connection == nullptr);
stored_connection = std::move(business_connection);
for (auto &promise : promises) {
promise.set_value(stored_connection->get_business_connection_object(td_));
}
}
} // namespace td

View File

@ -6,11 +6,14 @@
//
#pragma once
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/actor/actor.h"
#include "td/utils/common.h"
#include "td/utils/Promise.h"
#include "td/utils/Status.h"
#include "td/utils/WaitFreeHashMap.h"
namespace td {
@ -28,12 +31,20 @@ class BusinessConnectionManager final : public Actor {
void on_update_bot_business_connect(telegram_api::object_ptr<telegram_api::botBusinessConnection> &&connection);
void get_business_connection(const string &connection_id,
Promise<td_api::object_ptr<td_api::businessConnection>> &&promise);
private:
struct BusinessConnection;
void tear_down() final;
void on_get_business_connection(const string &connection_id,
Result<telegram_api::object_ptr<telegram_api::Updates>> r_updates);
WaitFreeHashMap<string, unique_ptr<BusinessConnection>> business_connections_;
void tear_down() final;
FlatHashMap<string, vector<Promise<td_api::object_ptr<td_api::businessConnection>>>> get_business_connection_queries_;
Td *td_;
ActorShared<> parent_;

View File

@ -8639,6 +8639,13 @@ void Td::on_request(uint64 id, const td_api::hideSuggestedAction &request) {
dismiss_suggested_action(SuggestedAction(request.action_), std::move(promise));
}
void Td::on_request(uint64 id, td_api::getBusinessConnection &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.connection_id_);
CREATE_REQUEST_PROMISE();
business_connection_manager_->get_business_connection(request.connection_id_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getLoginUrlInfo &request) {
CHECK_IS_USER();
CREATE_REQUEST_PROMISE();

View File

@ -1628,6 +1628,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::hideSuggestedAction &request);
void on_request(uint64 id, td_api::getBusinessConnection &request);
void on_request(uint64 id, const td_api::getLoginUrlInfo &request);
void on_request(uint64 id, const td_api::getLoginUrl &request);

View File

@ -6038,6 +6038,8 @@ class CliClient final : public Actor {
UserId bot_user_id;
get_args(args, bot_user_id);
send_request(td_api::make_object<td_api::deleteBusinessConnectedBot>(bot_user_id));
} else if (op == "gbc") {
send_request(td_api::make_object<td_api::getBusinessConnection>(args));
} else if (op == "sco") {
SearchQuery query;
get_args(args, query);