Add td_api::translateMessage.

This commit is contained in:
levlam 2022-01-14 22:28:28 +03:00
parent 5a51462de8
commit 8339a5818e
6 changed files with 93 additions and 0 deletions

View File

@ -4398,6 +4398,14 @@ getMessageEmbeddingCode chat_id:int53 message_id:int53 for_album:Bool = Text;
getMessageLinkInfo url:string = MessageLinkInfo;
//@description Translates a message text or caption to the given language. Returns a 404 error if the translation can't be performed
//@chat_id Identifier of the chat to which the message belongs
//@message_id Identifier of the message
//@from_language_code A two-letter ISO 639-1 language code of the language from which the message is translated. If empty, the language will be detected automatically
//@to_language_code A two-letter ISO 639-1 language code of the language to which the message is translated
translateMessage chat_id:int53 message_id:int53 from_language_code:string to_language_code:string = Text;
//@description Returns list of message sender identifiers, which can be used to send messages in a chat @chat_id Chat identifier
getChatAvailableMessageSenders chat_id:int53 = MessageSenders;

View File

@ -680,6 +680,47 @@ class GetMessageReadParticipantsQuery final : public Td::ResultHandler {
}
};
class TranslateTextQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::text>> promise_;
public:
explicit TranslateTextQuery(Promise<td_api::object_ptr<td_api::text>> &&promise) : promise_(std::move(promise)) {
}
void send(const string &text, const string &from_language_code, const string &to_language_code) {
int flags = telegram_api::messages_translateText::TEXT_MASK;
if (!from_language_code.empty()) {
flags |= telegram_api::messages_translateText::FROM_LANG_MASK;
}
send_query(G()->net_query_creator().create(
telegram_api::messages_translateText(flags, nullptr, 0, text, from_language_code, to_language_code)));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_translateText>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto ptr = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for TranslateTextQuery: " << to_string(ptr);
switch (ptr->get_id()) {
case telegram_api::messages_translateNoResult::ID:
return promise_.set_value(nullptr);
case telegram_api::messages_translateResultText::ID: {
auto text = telegram_api::move_object_as<telegram_api::messages_translateResultText>(ptr);
return promise_.set_value(td_api::make_object<td_api::text>(text->text_));
}
default:
UNREACHABLE();
}
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class ExportChannelMessageLinkQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
ChannelId channel_id_;
@ -17675,6 +17716,28 @@ void MessagesManager::on_get_message_viewers(DialogId dialog_id, vector<UserId>
promise.set_value(td_->contacts_manager_->get_users_object(-1, user_ids));
}
void MessagesManager::translate_message(FullMessageId full_message_id, const string &from_language_code,
const string &to_language_code,
Promise<td_api::object_ptr<td_api::text>> &&promise) {
auto dialog_id = full_message_id.get_dialog_id();
Dialog *d = get_dialog_force(dialog_id, "translate_message");
if (d == nullptr) {
return promise.set_error(Status::Error(400, "Chat not found"));
}
auto m = get_message_force(d, full_message_id.get_message_id(), "translate_message");
if (m == nullptr) {
return promise.set_error(Status::Error(400, "Message not found"));
}
const FormattedText *text = get_message_content_text(m->content.get());
if (text == nullptr) {
return promise.set_error(Status::Error(400, "Message have no text"));
}
td_->create_handler<TranslateTextQuery>(std::move(promise))->send(text->text, from_language_code, to_language_code);
}
void MessagesManager::get_dialog_info_full(DialogId dialog_id, Promise<Unit> &&promise, const char *source) {
switch (dialog_id.get_type()) {
case DialogType::User:

View File

@ -601,6 +601,9 @@ class MessagesManager final : public Actor {
void get_message_viewers(FullMessageId full_message_id, Promise<td_api::object_ptr<td_api::users>> &&promise);
void translate_message(FullMessageId full_message_id, const string &from_language_code,
const string &to_language_code, Promise<td_api::object_ptr<td_api::text>> &&promise);
bool is_message_edited_recently(FullMessageId full_message_id, int32 seconds);
bool is_deleted_secret_chat(DialogId dialog_id) const;

View File

@ -4742,6 +4742,15 @@ void Td::on_request(uint64 id, td_api::getMessageLinkInfo &request) {
CREATE_REQUEST(GetMessageLinkInfoRequest, std::move(request.url_));
}
void Td::on_request(uint64 id, td_api::translateMessage &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.from_language_code_);
CLEAN_INPUT_STRING(request.to_language_code_);
CREATE_REQUEST_PROMISE();
messages_manager_->translate_message({DialogId(request.chat_id_), MessageId(request.message_id_)},
request.from_language_code_, request.to_language_code_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::getFile &request) {
send_closure(actor_id(this), &Td::send_result, id, file_manager_->get_file_object(FileId(request.file_id_, 0)));
}

View File

@ -533,6 +533,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::getMessageLinkInfo &request);
void on_request(uint64 id, td_api::translateMessage &request);
void on_request(uint64 id, const td_api::getFile &request);
void on_request(uint64 id, td_api::getRemoteFile &request);

View File

@ -2742,6 +2742,14 @@ class CliClient final : public Actor {
send_request(td_api::make_object<td_api::getMessageEmbeddingCode>(chat_id, message_id, for_album));
} else if (op == "gmli") {
send_request(td_api::make_object<td_api::getMessageLinkInfo>(args));
} else if (op == "tm") {
ChatId chat_id;
MessageId message_id;
string from_language_code;
string to_language_code;
get_args(args, chat_id, message_id, from_language_code, to_language_code);
send_request(
td_api::make_object<td_api::translateMessage>(chat_id, message_id, from_language_code, to_language_code));
} else if (op == "gf" || op == "GetFile") {
send_request(td_api::make_object<td_api::getFile>(as_file_id(args)));
} else if (op == "gfdps") {