From 398074809982cd0605d89dbb5cd59b7c1f0ab2f8 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 9 Dec 2021 18:48:39 +0300 Subject: [PATCH] Use channel_bot as sender of channel messages. --- telegram-bot-api/Client.cpp | 59 ++++++++++++++++++++----------------- telegram-bot-api/Client.h | 1 + 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index a374e3e..dbf9f4f 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -4448,6 +4448,9 @@ void Client::on_update(object_ptr result) { if (name == "group_anonymous_bot_user_id" && update->value_->get_id() == td_api::optionValueInteger::ID) { group_anonymous_bot_user_id_ = move_object_as(update->value_)->value_; } + if (name == "channel_bot_user_id" && update->value_->get_id() == td_api::optionValueInteger::ID) { + channel_bot_user_id_ = move_object_as(update->value_)->value_; + } if (name == "telegram_service_notifications_chat_id" && update->value_->get_id() == td_api::optionValueInteger::ID) { service_notifications_user_id_ = move_object_as(update->value_)->value_; @@ -9536,33 +9539,6 @@ Client::FullMessageId Client::add_message(object_ptr &&message, message_info->media_album_id = message->media_album_id_; message_info->via_bot_user_id = message->via_bot_user_id_; - CHECK(message->sender_id_ != nullptr); - switch (message->sender_id_->get_id()) { - case td_api::messageSenderUser::ID: { - auto sender_id = move_object_as(message->sender_id_); - message_info->sender_user_id = sender_id->user_id_; - CHECK(message_info->sender_user_id > 0); - break; - } - case td_api::messageSenderChat::ID: { - auto sender_id = move_object_as(message->sender_id_); - message_info->sender_chat_id = sender_id->chat_id_; - - auto chat_type = get_chat_type(chat_id); - if (chat_type != ChatType::Channel) { - if (message_info->sender_chat_id == chat_id) { - message_info->sender_user_id = group_anonymous_bot_user_id_; - } else { - message_info->sender_user_id = service_notifications_user_id_; - } - CHECK(message_info->sender_user_id > 0); - } - break; - } - default: - UNREACHABLE(); - } - message_info->initial_chat_id = 0; message_info->initial_sender_user_id = 0; message_info->initial_sender_chat_id = 0; @@ -9611,6 +9587,35 @@ Client::FullMessageId Client::add_message(object_ptr &&message, get_chat_type(chat_id) == ChatType::Supergroup && get_chat_type(from_chat_id) == ChatType::Channel; } + CHECK(message->sender_id_ != nullptr); + switch (message->sender_id_->get_id()) { + case td_api::messageSenderUser::ID: { + auto sender_id = move_object_as(message->sender_id_); + message_info->sender_user_id = sender_id->user_id_; + CHECK(message_info->sender_user_id > 0); + break; + } + case td_api::messageSenderChat::ID: { + auto sender_id = move_object_as(message->sender_id_); + message_info->sender_chat_id = sender_id->chat_id_; + + auto chat_type = get_chat_type(chat_id); + if (chat_type != ChatType::Channel) { + if (message_info->sender_chat_id == chat_id) { + message_info->sender_user_id = group_anonymous_bot_user_id_; + } else if (message_info->is_automatic_forward) { + message_info->sender_user_id = service_notifications_user_id_; + } else { + message_info->sender_user_id = channel_bot_user_id_; + } + CHECK(message_info->sender_user_id > 0); + } + break; + } + default: + UNREACHABLE(); + } + message_info->can_be_saved = message->can_be_saved_; message_info->author_signature = std::move(message->author_signature_); diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 5df4572..7468ab0 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -866,6 +866,7 @@ class Client : public WebhookActor::Callback { int32 authorization_date_ = -1; int64 group_anonymous_bot_user_id_ = 0; + int64 channel_bot_user_id_ = 0; int64 service_notifications_user_id_ = 0; static std::unordered_map methods_;