Support sending messages to business connection.

This commit is contained in:
levlam 2024-03-14 22:51:02 +03:00
parent f05754481f
commit 2ac6b4bcca
2 changed files with 77 additions and 4 deletions

View File

@ -2416,6 +2416,9 @@ class Client::JsonTextQuote final : public td::Jsonable {
void Client::JsonMessage::store(td::JsonValueScope *scope) const {
CHECK(message_ != nullptr);
auto object = scope->enter_object();
if (!message_->business_connection_id.empty()) {
object("business_connection_id", message_->business_connection_id);
}
object("message_id", as_client_message_id(message_->id));
if (message_->sender_user_id != 0) {
object("from", JsonUser(message_->sender_user_id, client_));
@ -3797,6 +3800,29 @@ class Client::TdOnSendMessageCallback final : public TdQueryCallback {
PromisedQueryPtr query_;
};
class Client::TdOnSendBusinessMessageCallback final : public TdQueryCallback {
public:
TdOnSendBusinessMessageCallback(Client *client, td::string business_connection_id, PromisedQueryPtr query)
: client_(client), business_connection_id_(std::move(business_connection_id)), query_(std::move(query)) {
}
void on_result(object_ptr<td_api::Object> result) final {
if (result->get_id() == td_api::error::ID) {
return fail_query_with_error(std::move(query_), move_object_as<td_api::error>(result));
}
CHECK(result->get_id() == td_api::businessMessage::ID);
auto message = client_->create_business_message(std::move(business_connection_id_),
move_object_as<td_api::businessMessage>(result));
answer_query(JsonMessage(message.get(), false, "sent business message", client_), std::move(query_));
}
private:
Client *client_;
td::string business_connection_id_;
PromisedQueryPtr query_;
};
class Client::TdOnSendMessageAlbumCallback final : public TdQueryCallback {
public:
TdOnSendMessageAlbumCallback(Client *client, int64 chat_id, std::size_t message_count, PromisedQueryPtr query)
@ -6554,6 +6580,15 @@ td_api::object_ptr<td_api::InputMessageReplyTo> Client::get_input_message_reply_
return nullptr;
}
td_api::object_ptr<td_api::InputMessageReplyTo> Client::get_input_message_reply_to(
InputReplyParameters &&reply_parameters) {
if (reply_parameters.reply_in_chat_id.empty() && reply_parameters.reply_to_message_id > 0) {
return make_object<td_api::inputMessageReplyToMessage>(0, reply_parameters.reply_to_message_id,
std::move(reply_parameters.quote));
}
return nullptr;
}
td::Result<Client::InputReplyParameters> Client::get_reply_parameters(const Query *query) {
if (!query->has_arg("reply_parameters")) {
InputReplyParameters result;
@ -11446,6 +11481,7 @@ void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_messa
bool force) {
auto chat_id = query->arg("chat_id");
auto message_thread_id = get_message_id(query.get(), "message_thread_id");
auto business_connection_id = query->arg("business_connection_id");
auto r_reply_parameters = get_reply_parameters(query.get());
if (r_reply_parameters.is_error()) {
return fail_query_with_error(std::move(query), 400, r_reply_parameters.error().message());
@ -11461,9 +11497,30 @@ void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_messa
resolve_reply_markup_bot_usernames(
std::move(reply_markup), std::move(query),
[this, chat_id = chat_id.str(), message_thread_id, reply_parameters = std::move(reply_parameters),
disable_notification, protect_content, input_message_content = std::move(input_message_content)](
object_ptr<td_api::ReplyMarkup> reply_markup, PromisedQueryPtr query) mutable {
[this, chat_id_str = chat_id.str(), message_thread_id, business_connection_id = business_connection_id.str(),
reply_parameters = std::move(reply_parameters), disable_notification, protect_content,
input_message_content = std::move(input_message_content)](object_ptr<td_api::ReplyMarkup> reply_markup,
PromisedQueryPtr query) mutable {
if (!business_connection_id.empty()) {
auto r_chat_id = get_business_connection_chat_id(chat_id_str);
if (r_chat_id.is_error()) {
return fail_query_with_error(std::move(query), 400, r_chat_id.error().message());
}
auto chat_id = r_chat_id.move_as_ok();
return check_business_connection(
business_connection_id, std::move(query),
[this, chat_id, reply_parameters = std::move(reply_parameters), disable_notification, protect_content,
reply_markup = std::move(reply_markup), input_message_content = std::move(input_message_content)](
const td::string &business_connection_id, PromisedQueryPtr query) mutable {
send_request(
make_object<td_api::sendBusinessMessage>(business_connection_id, chat_id,
get_input_message_reply_to(std::move(reply_parameters)),
disable_notification, protect_content,
std::move(reply_markup), std::move(input_message_content)),
td::make_unique<TdOnSendBusinessMessageCallback>(this, business_connection_id, std::move(query)));
});
}
auto on_success = [this, disable_notification, protect_content,
input_message_content = std::move(input_message_content),
reply_markup = std::move(reply_markup)](int64 chat_id, int64 message_thread_id,
@ -11481,7 +11538,7 @@ void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_messa
std::move(reply_markup), std::move(input_message_content)),
td::make_unique<TdOnSendMessageCallback>(this, chat_id, std::move(query)));
};
check_reply_parameters(chat_id, std::move(reply_parameters), message_thread_id, std::move(query),
check_reply_parameters(chat_id_str, std::move(reply_parameters), message_thread_id, std::move(query),
std::move(on_success));
});
}
@ -13069,6 +13126,15 @@ void Client::init_message(MessageInfo *message_info, object_ptr<td_api::message>
message = nullptr;
}
td::unique_ptr<Client::MessageInfo> Client::create_business_message(td::string business_connection_id,
object_ptr<td_api::businessMessage> &&message) {
auto message_info = td::make_unique<MessageInfo>();
CHECK(message != nullptr);
init_message(message_info.get(), std::move(message->message_), true);
message_info->business_connection_id = std::move(business_connection_id);
return message_info;
}
void Client::update_message_content(int64 chat_id, int64 message_id, object_ptr<td_api::MessageContent> &&content) {
auto message_info = get_message_editable(chat_id, message_id);
if (message_info == nullptr) {

View File

@ -196,6 +196,7 @@ class Client final : public WebhookActor::Callback {
class TdOnInitCallback;
class TdOnGetUserProfilePhotosCallback;
class TdOnSendMessageCallback;
class TdOnSendBusinessMessageCallback;
class TdOnSendMessageAlbumCallback;
class TdOnForwardMessagesCallback;
class TdOnDeleteFailedToSendMessageCallback;
@ -383,6 +384,8 @@ class Client final : public WebhookActor::Callback {
static object_ptr<td_api::InputMessageReplyTo> get_input_message_reply_to(CheckedReplyParameters &&reply_parameters);
static object_ptr<td_api::InputMessageReplyTo> get_input_message_reply_to(InputReplyParameters &&reply_parameters);
static td::Result<InputReplyParameters> get_reply_parameters(const Query *query);
static td::Result<InputReplyParameters> get_reply_parameters(td::JsonValue &&value);
@ -889,6 +892,7 @@ class Client final : public WebhookActor::Callback {
int64 via_bot_user_id = 0;
object_ptr<td_api::MessageContent> content;
object_ptr<td_api::ReplyMarkup> reply_markup;
td::string business_connection_id;
bool can_be_saved = false;
bool is_automatic_forward = false;
@ -987,6 +991,9 @@ class Client final : public WebhookActor::Callback {
const MessageInfo *get_message(int64 chat_id, int64 message_id, bool force_cache) const;
MessageInfo *get_message_editable(int64 chat_id, int64 message_id);
td::unique_ptr<MessageInfo> create_business_message(td::string business_connection_id,
object_ptr<td_api::businessMessage> &&message);
void update_message_content(int64 chat_id, int64 message_id, object_ptr<td_api::MessageContent> &&content);
void on_update_message_edited(int64 chat_id, int64 message_id, int32 edit_date,