Add translateMessageText.

This commit is contained in:
levlam 2023-01-23 15:46:26 +03:00
parent 8560ddfe84
commit 136a50d0ae
8 changed files with 56 additions and 1 deletions

View File

@ -5993,6 +5993,12 @@ getMessageLinkInfo url:string = MessageLinkInfo;
//@to_language_code A two-letter ISO 639-1 language code of the language to which the message is translated
translateText text:formattedText to_language_code:string = FormattedText;
//@description Extracts text or caption of the given message and translates it to the given language
//@chat_id Identifier of the chat to which the message belongs
//@message_id Identifier of the message
//@to_language_code A two-letter ISO 639-1 language code of the language to which the message is translated
translateMessageText chat_id:int53 message_id:int53 to_language_code:string = FormattedText;
//@description Recognizes speech in a video note or a voice note message. The message must be successfully sent and must not be scheduled. May return an error with a message "MSG_VOICE_TOO_LONG" if media duration is too big to be recognized
//@chat_id Identifier of the chat to which the message belongs
//@message_id Identifier of the message

View File

@ -62,6 +62,7 @@
#include "td/telegram/TdDb.h"
#include "td/telegram/TdParameters.h"
#include "td/telegram/TopDialogCategory.h"
#include "td/telegram/TranslationManager.h"
#include "td/telegram/UpdatesManager.h"
#include "td/telegram/Version.h"
#include "td/telegram/WebPageId.h"
@ -19021,6 +19022,24 @@ 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_text(FullMessageId full_message_id, const string &to_language_code,
Promise<td_api::object_ptr<td_api::formattedText>> &&promise) {
auto m = get_message_force(full_message_id, "recognize_speech");
if (m == nullptr) {
return promise.set_error(Status::Error(400, "Message not found"));
}
auto text = get_message_content_text(m->content.get());
if (text == nullptr) {
return promise.set_value(td_api::make_object<td_api::formattedText>());
}
auto skip_bot_commands = need_skip_bot_commands(full_message_id.get_dialog_id(), m);
auto max_media_timestamp = get_message_max_media_timestamp(m);
td_->translation_manager_->translate_text(*text, skip_bot_commands, max_media_timestamp, to_language_code,
std::move(promise));
}
void MessagesManager::recognize_speech(FullMessageId full_message_id, Promise<Unit> &&promise) {
auto m = get_message_force(full_message_id, "recognize_speech");
if (m == nullptr) {

View File

@ -642,6 +642,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_text(FullMessageId full_message_id, const string &to_language_code,
Promise<td_api::object_ptr<td_api::formattedText>> &&promise);
void recognize_speech(FullMessageId full_message_id, Promise<Unit> &&promise);
void rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise<Unit> &&promise);

View File

@ -4713,6 +4713,14 @@ void Td::on_request(uint64 id, td_api::translateText &request) {
translation_manager_->translate_text(std::move(request.text_), request.to_language_code_, std::move(promise));
}
void Td::on_request(uint64 id, td_api::translateMessageText &request) {
CHECK_IS_USER();
CLEAN_INPUT_STRING(request.to_language_code_);
CREATE_REQUEST_PROMISE();
messages_manager_->translate_message_text({DialogId(request.chat_id_), MessageId(request.message_id_)},
request.to_language_code_, std::move(promise));
}
void Td::on_request(uint64 id, const td_api::recognizeSpeech &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();

View File

@ -557,6 +557,8 @@ class Td final : public Actor {
void on_request(uint64 id, td_api::translateText &request);
void on_request(uint64 id, td_api::translateMessageText &request);
void on_request(uint64 id, const td_api::recognizeSpeech &request);
void on_request(uint64 id, const td_api::rateSpeechRecognition &request);

View File

@ -89,8 +89,15 @@ void TranslationManager::translate_text(td_api::object_ptr<td_api::formattedText
TRY_RESULT_PROMISE(promise, entities, get_message_entities(td_->contacts_manager_.get(), std::move(text->entities_)));
TRY_STATUS_PROMISE(promise, fix_formatted_text(text->text_, entities, true, true, true, true, true));
translate_text(FormattedText{std::move(text->text_), std::move(entities)}, skip_bot_commands, max_media_timestamp,
to_language_code, std::move(promise));
}
void TranslationManager::translate_text(FormattedText text, bool skip_bot_commands, int32 max_media_timestamp,
const string &to_language_code,
Promise<td_api::object_ptr<td_api::formattedText>> &&promise) {
vector<FormattedText> texts;
texts.push_back(FormattedText{std::move(text->text_), std::move(entities)});
texts.push_back(std::move(text));
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), skip_bot_commands, max_media_timestamp, promise = std::move(promise)](

View File

@ -6,6 +6,7 @@
//
#pragma once
#include "td/telegram/MessageEntity.h"
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
@ -25,6 +26,9 @@ class TranslationManager final : public Actor {
void translate_text(td_api::object_ptr<td_api::formattedText> &&text, const string &to_language_code,
Promise<td_api::object_ptr<td_api::formattedText>> &&promise);
void translate_text(FormattedText text, bool skip_bot_commands, int32 max_media_timestamp,
const string &to_language_code, Promise<td_api::object_ptr<td_api::formattedText>> &&promise);
private:
void tear_down() final;

View File

@ -3066,6 +3066,12 @@ class CliClient final : public Actor {
string to_language_code;
get_args(args, to_language_code, text);
send_request(td_api::make_object<td_api::translateText>(as_formatted_text(text), to_language_code));
} else if (op == "tmt") {
ChatId chat_id;
MessageId message_id;
string to_language_code;
get_args(args, chat_id, message_id, to_language_code);
send_request(td_api::make_object<td_api::translateMessageText>(chat_id, message_id, to_language_code));
} else if (op == "rs") {
ChatId chat_id;
MessageId message_id;