Support translation of formatted text.
This commit is contained in:
parent
3121757b32
commit
e26aecc87a
@ -5933,11 +5933,10 @@ getMessageEmbeddingCode chat_id:int53 message_id:int53 for_album:Bool = Text;
|
||||
getMessageLinkInfo url:string = MessageLinkInfo;
|
||||
|
||||
|
||||
//@description Translates a text to the given language. Returns a 404 error if the translation can't be performed
|
||||
//@description Translates a text to the given language
|
||||
//@text Text to translate
|
||||
//@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
|
||||
translateText text:string from_language_code:string to_language_code:string = Text;
|
||||
translateText text:formattedText 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
|
||||
|
@ -4702,12 +4702,9 @@ void Td::on_request(uint64 id, td_api::getMessageLinkInfo &request) {
|
||||
|
||||
void Td::on_request(uint64 id, td_api::translateText &request) {
|
||||
CHECK_IS_USER();
|
||||
CLEAN_INPUT_STRING(request.text_);
|
||||
CLEAN_INPUT_STRING(request.from_language_code_);
|
||||
CLEAN_INPUT_STRING(request.to_language_code_);
|
||||
CREATE_REQUEST_PROMISE();
|
||||
translation_manager_->translate_text(request.text_, request.from_language_code_, request.to_language_code_,
|
||||
std::move(promise));
|
||||
translation_manager_->translate_text(std::move(request.text_), request.to_language_code_, std::move(promise));
|
||||
}
|
||||
|
||||
void Td::on_request(uint64 id, const td_api::recognizeSpeech &request) {
|
||||
|
@ -7,27 +7,32 @@
|
||||
#include "td/telegram/TranslationManager.h"
|
||||
|
||||
#include "td/telegram/Global.h"
|
||||
#include "td/telegram/MessageEntity.h"
|
||||
#include "td/telegram/Td.h"
|
||||
#include "td/telegram/telegram_api.h"
|
||||
|
||||
#include "td/utils/algorithm.h"
|
||||
#include "td/utils/buffer.h"
|
||||
#include "td/utils/Status.h"
|
||||
|
||||
namespace td {
|
||||
|
||||
class TranslateTextQuery final : public Td::ResultHandler {
|
||||
Promise<td_api::object_ptr<td_api::text>> promise_;
|
||||
Promise<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> promise_;
|
||||
|
||||
public:
|
||||
explicit TranslateTextQuery(Promise<td_api::object_ptr<td_api::text>> &&promise) : promise_(std::move(promise)) {
|
||||
explicit TranslateTextQuery(Promise<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> &&promise)
|
||||
: promise_(std::move(promise)) {
|
||||
}
|
||||
|
||||
void send(const string &text, const string &from_language_code, const string &to_language_code) {
|
||||
void send(vector<FormattedText> &&texts, const string &to_language_code) {
|
||||
int flags = telegram_api::messages_translateText::TEXT_MASK;
|
||||
vector<telegram_api::object_ptr<telegram_api::textWithEntities>> texts;
|
||||
texts.push_back(telegram_api::make_object<telegram_api::textWithEntities>(text, Auto()));
|
||||
send_query(G()->net_query_creator().create(
|
||||
telegram_api::messages_translateText(flags, nullptr, vector<int32>{}, std::move(texts), to_language_code)));
|
||||
auto input_texts =
|
||||
transform(std::move(texts), [contacts_manager = td_->contacts_manager_.get()](FormattedText &&text) {
|
||||
return get_input_text_with_entities(contacts_manager, std::move(text), "TranslateTextQuery");
|
||||
});
|
||||
send_query(G()->net_query_creator().create(telegram_api::messages_translateText(
|
||||
flags, nullptr, vector<int32>{}, std::move(input_texts), to_language_code)));
|
||||
}
|
||||
|
||||
void on_result(BufferSlice packet) final {
|
||||
@ -38,10 +43,7 @@ class TranslateTextQuery final : public Td::ResultHandler {
|
||||
|
||||
auto ptr = result_ptr.move_as_ok();
|
||||
LOG(INFO) << "Receive result for TranslateTextQuery: " << to_string(ptr);
|
||||
if (ptr->result_.empty()) {
|
||||
return promise_.set_value(nullptr);
|
||||
}
|
||||
promise_.set_value(td_api::make_object<td_api::text>(ptr->result_[0]->text_));
|
||||
promise_.set_value(std::move(ptr->result_));
|
||||
}
|
||||
|
||||
void on_error(Status status) final {
|
||||
@ -56,10 +58,40 @@ void TranslationManager::tear_down() {
|
||||
parent_.reset();
|
||||
}
|
||||
|
||||
void TranslationManager::translate_text(const string &text, const string &from_language_code,
|
||||
void TranslationManager::translate_text(td_api::object_ptr<td_api::formattedText> &&text,
|
||||
const string &to_language_code,
|
||||
Promise<td_api::object_ptr<td_api::text>> &&promise) {
|
||||
td_->create_handler<TranslateTextQuery>(std::move(promise))->send(text, from_language_code, to_language_code);
|
||||
Promise<td_api::object_ptr<td_api::formattedText>> &&promise) {
|
||||
if (text == nullptr) {
|
||||
return promise.set_error(Status::Error(400, "Text must be non-empty"));
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
vector<FormattedText> texts;
|
||||
texts.push_back(FormattedText{std::move(text->text_), std::move(entities)});
|
||||
|
||||
auto query_promise = PromiseCreator::lambda(
|
||||
[actor_id = actor_id(this), promise = std::move(promise)](
|
||||
Result<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> result) mutable {
|
||||
if (result.is_error()) {
|
||||
return promise.set_error(result.move_as_error());
|
||||
}
|
||||
send_closure(actor_id, &TranslationManager::on_get_translated_texts, result.move_as_ok(), std::move(promise));
|
||||
});
|
||||
|
||||
td_->create_handler<TranslateTextQuery>(std::move(query_promise))->send(std::move(texts), to_language_code);
|
||||
}
|
||||
|
||||
void TranslationManager::on_get_translated_texts(vector<telegram_api::object_ptr<telegram_api::textWithEntities>> texts,
|
||||
Promise<td_api::object_ptr<td_api::formattedText>> &&promise) {
|
||||
TRY_STATUS_PROMISE(promise, G()->close_status());
|
||||
if (texts.size() != 1u) {
|
||||
return promise.set_error(Status::Error(500, "Receive invalid number of results"));
|
||||
}
|
||||
auto formatted_text =
|
||||
get_formatted_text(td_->contacts_manager_.get(), std::move(texts[0]), "on_get_translated_texts");
|
||||
promise.set_value(get_formatted_text_object(formatted_text, true, -1));
|
||||
}
|
||||
|
||||
} // namespace td
|
||||
|
@ -7,6 +7,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "td/telegram/td_api.h"
|
||||
#include "td/telegram/telegram_api.h"
|
||||
|
||||
#include "td/actor/actor.h"
|
||||
|
||||
@ -21,12 +22,15 @@ class TranslationManager final : public Actor {
|
||||
public:
|
||||
TranslationManager(Td *td, ActorShared<> parent);
|
||||
|
||||
void translate_text(const string &text, const string &from_language_code, const string &to_language_code,
|
||||
Promise<td_api::object_ptr<td_api::text>> &&promise);
|
||||
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);
|
||||
|
||||
private:
|
||||
void tear_down() final;
|
||||
|
||||
void on_get_translated_texts(vector<telegram_api::object_ptr<telegram_api::textWithEntities>> texts,
|
||||
Promise<td_api::object_ptr<td_api::formattedText>> &&promise);
|
||||
|
||||
Td *td_;
|
||||
ActorShared<> parent_;
|
||||
};
|
||||
|
@ -3038,10 +3038,9 @@ class CliClient final : public Actor {
|
||||
send_request(td_api::make_object<td_api::getMessageLinkInfo>(args));
|
||||
} else if (op == "tt") {
|
||||
string text;
|
||||
string from_language_code;
|
||||
string to_language_code;
|
||||
get_args(args, text, from_language_code, to_language_code);
|
||||
send_request(td_api::make_object<td_api::translateText>(text, from_language_code, 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 == "rs") {
|
||||
ChatId chat_id;
|
||||
MessageId message_id;
|
||||
|
Loading…
Reference in New Issue
Block a user