diff --git a/td/telegram/TranscriptionInfo.cpp b/td/telegram/TranscriptionInfo.cpp index fda092c54..f5df61dee 100644 --- a/td/telegram/TranscriptionInfo.cpp +++ b/td/telegram/TranscriptionInfo.cpp @@ -20,11 +20,11 @@ namespace td { class TranscribeAudioQuery final : public Td::ResultHandler { DialogId dialog_id_; - std::function>)> handler_; + std::function>)> handler_; public: void send(MessageFullId message_full_id, - std::function>)> &&handler) { + std::function>)> &&handler) { dialog_id_ = message_full_id.get_dialog_id(); handler_ = std::move(handler); auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read); @@ -43,14 +43,7 @@ class TranscribeAudioQuery final : public Td::ResultHandler { auto result = result_ptr.move_as_ok(); LOG(INFO) << "Receive result for TranscribeAudioQuery: " << to_string(result); - if (result->transcription_id_ == 0) { - return on_error(Status::Error(500, "Receive no recognition identifier")); - } - auto update = telegram_api::make_object(); - update->text_ = std::move(result->text_); - update->transcription_id_ = result->transcription_id_; - update->pending_ = result->pending_; - handler_(std::move(update)); + handler_(std::move(result)); } void on_error(Status status) final { @@ -97,7 +90,7 @@ class RateTranscribedAudioQuery final : public Td::ResultHandler { bool TranscriptionInfo::recognize_speech( Td *td, MessageFullId message_full_id, Promise &&promise, - std::function>)> &&handler) { + std::function>)> &&handler) { if (is_transcribed_) { promise.set_value(Unit()); return false; diff --git a/td/telegram/TranscriptionInfo.h b/td/telegram/TranscriptionInfo.h index 5fce1562d..ebd3cf9cc 100644 --- a/td/telegram/TranscriptionInfo.h +++ b/td/telegram/TranscriptionInfo.h @@ -36,7 +36,7 @@ class TranscriptionInfo { bool recognize_speech( Td *td, MessageFullId message_full_id, Promise &&promise, - std::function>)> &&handler); + std::function>)> &&handler); vector> on_final_transcription(string &&text, int64 transcription_id); diff --git a/td/telegram/TranscriptionManager.cpp b/td/telegram/TranscriptionManager.cpp index 1f9ce80b3..50b6059b9 100644 --- a/td/telegram/TranscriptionManager.cpp +++ b/td/telegram/TranscriptionManager.cpp @@ -236,14 +236,33 @@ void TranscriptionManager::recognize_speech(MessageFullId message_full_id, Promi auto *transcription_info = get_transcription_info(it->second, true); auto handler = [actor_id = actor_id(this), file_info = it->second]( - Result> r_update) { - send_closure(actor_id, &TranscriptionManager::on_transcribed_audio_update, file_info, true, std::move(r_update)); + Result> r_audio) { + send_closure(actor_id, &TranscriptionManager::on_transcribed_audio, file_info, std::move(r_audio)); }; if (transcription_info->recognize_speech(td_, message_full_id, std::move(promise), std::move(handler))) { on_transcription_updated(it->second.second); } } +void TranscriptionManager::on_transcribed_audio( + FileInfo file_info, Result> r_audio) { + if (G()->close_flag()) { + return; + } + if (r_audio.is_error()) { + return on_transcribed_audio_update(file_info, true, r_audio.move_as_error()); + } + auto audio = r_audio.move_as_ok(); + if (audio->transcription_id_ == 0) { + return on_transcribed_audio_update(file_info, true, Status::Error(500, "Receive no transcription identifier")); + } + auto update = telegram_api::make_object(); + update->text_ = std::move(audio->text_); + update->transcription_id_ = audio->transcription_id_; + update->pending_ = audio->pending_; + on_transcribed_audio_update(file_info, true, std::move(update)); +} + void TranscriptionManager::on_transcribed_audio_update( FileInfo file_info, bool is_initial, Result> r_update) { diff --git a/td/telegram/TranscriptionManager.h b/td/telegram/TranscriptionManager.h index ddc5bea0f..c812c524d 100644 --- a/td/telegram/TranscriptionManager.h +++ b/td/telegram/TranscriptionManager.h @@ -77,6 +77,9 @@ class TranscriptionManager final : public Actor { TranscriptionInfo *get_transcription_info(const FileInfo &file_info, bool allow_creation); + void on_transcribed_audio(FileInfo file_info, + Result> r_audio); + using TranscribedAudioHandler = std::function>)>; void subscribe_to_transcribed_audio_updates(int64 transcription_id, TranscribedAudioHandler on_update);