From b56b77866053d0feccac36e60d7fb02a6e780f6f Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 26 May 2022 20:40:43 +0300 Subject: [PATCH] Add rateSpeechRecognition. --- td/generate/scheme/td_api.tl | 3 ++ td/telegram/Td.cpp | 7 ++++ td/telegram/Td.h | 2 ++ td/telegram/VoiceNotesManager.cpp | 57 +++++++++++++++++++++++++++++++ td/telegram/VoiceNotesManager.h | 2 ++ td/telegram/cli.cpp | 6 ++++ 6 files changed, 77 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 094f323e5..7cbeb85e8 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4823,6 +4823,9 @@ translateText text:string from_language_code:string to_language_code:string = Te //@message_id Identifier of the message recognizeSpeech chat_id:int53 message_id:int53 = Ok; +//@description Rates recognized speech in a voice note message @chat_id Identifier of the chat to which the message belongs @message_id Identifier of the message @is_good Pass true if the speech recognition is good +rateSpeechRecognition chat_id:int53 message_id:int53 is_good:Bool = Ok; + //@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; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index cd1bd1b25..f33c37123 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4804,6 +4804,13 @@ void Td::on_request(uint64 id, const td_api::recognizeSpeech &request) { std::move(promise)); } +void Td::on_request(uint64 id, const td_api::rateSpeechRecognition &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + voice_notes_manager_->rate_speech_recognition({DialogId(request.chat_id_), MessageId(request.message_id_)}, + request.is_good_, 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))); } diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 8896928b9..110387eab 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -534,6 +534,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::recognizeSpeech &request); + void on_request(uint64 id, const td_api::rateSpeechRecognition &request); + void on_request(uint64 id, const td_api::getFile &request); void on_request(uint64 id, td_api::getRemoteFile &request); diff --git a/td/telegram/VoiceNotesManager.cpp b/td/telegram/VoiceNotesManager.cpp index 89e291d97..eccf7787a 100644 --- a/td/telegram/VoiceNotesManager.cpp +++ b/td/telegram/VoiceNotesManager.cpp @@ -58,6 +58,42 @@ class TranscribeAudioQuery final : public Td::ResultHandler { } }; +class RateTranscribedAudioQuery final : public Td::ResultHandler { + Promise promise_; + DialogId dialog_id_; + + public: + explicit RateTranscribedAudioQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(FullMessageId full_message_id, int64 transcription_id, bool is_good) { + dialog_id_ = full_message_id.get_dialog_id(); + auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read); + if (input_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chat")); + } + send_query(G()->net_query_creator().create(telegram_api::messages_rateTranscribedAudio( + std::move(input_peer), full_message_id.get_message_id().get_server_message_id().get(), transcription_id, + is_good))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + bool result = result_ptr.ok(); + LOG(INFO) << "Receive result for RateTranscribedAudioQuery: " << result; + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "RateTranscribedAudioQuery"); + promise_.set_error(std::move(status)); + } +}; + VoiceNotesManager::VoiceNotesManager(Td *td) : td_(td) { } @@ -322,6 +358,27 @@ void VoiceNotesManager::on_voice_note_transcription_updated(FileId file_id) { } } +void VoiceNotesManager::rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise &&promise) { + if (!td_->messages_manager_->have_message_force(full_message_id, "rate_speech_recognition")) { + return promise.set_error(Status::Error(400, "Message not found")); + } + + auto it = message_voice_notes_.find(full_message_id); + if (it == message_voice_notes_.end()) { + return promise.set_error(Status::Error(400, "Invalid message specified")); + } + + auto file_id = it->second; + auto voice_note = get_voice_note(file_id); + CHECK(voice_note != nullptr); + if (!voice_note->is_transcribed) { + return promise.set_value(Unit()); + } + CHECK(voice_note->transcription_id != 0); + td_->create_handler(std::move(promise)) + ->send(full_message_id, voice_note->transcription_id, is_good); +} + SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_note_file_id, tl_object_ptr input_file, const string &caption, int32 layer) const { diff --git a/td/telegram/VoiceNotesManager.h b/td/telegram/VoiceNotesManager.h index a67d479fa..f2ec3f63d 100644 --- a/td/telegram/VoiceNotesManager.h +++ b/td/telegram/VoiceNotesManager.h @@ -40,6 +40,8 @@ class VoiceNotesManager { void recognize_speech(FullMessageId full_message_id, Promise &&promise); + void rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise &&promise); + void on_update_transcribed_audio(string &&text, int64 transcription_id, bool is_final); void on_voice_note_transcribed(FileId file_id, string &&text, int64 transcription_id, bool is_final); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 65e364269..5cfafc4c8 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2894,6 +2894,12 @@ class CliClient final : public Actor { MessageId message_id; get_args(args, chat_id, message_id); send_request(td_api::make_object(chat_id, message_id)); + } else if (op == "rsr") { + ChatId chat_id; + MessageId message_id; + bool is_good; + get_args(args, chat_id, message_id, is_good); + send_request(td_api::make_object(chat_id, message_id, is_good)); } else if (op == "gf" || op == "GetFile") { FileId file_id; get_args(args, file_id);