def transcribe_audio_file(bot, update, path):
    chat_id = get_chat_id(update)
    lang = TBDB.get_chat_lang(chat_id)
    message_id = get_message_id(update)
    is_group = chat_id < 0

    api_key = config.get_config_prop("wit").get(lang, None)
    if api_key is None:
        logger.error("Language not found in wit.json %s", lang)
        message = bot.send_message(
            chat_id=chat_id,
            text=R.get_string_resource("unknown_api_key",
                                       lang).format(language=lang) + "\n",
            reply_to_message_id=message_id,
            parse_mode="html",
            is_group=is_group).result()
        return
    logger.debug("Using key %s for lang %s", api_key, lang)

    message = bot.send_message(
        chat_id=chat_id,
        text=R.get_string_resource("transcribing", lang) + "\n",
        reply_to_message_id=message_id,
        parse_mode="html",
        is_group=is_group).result()

    TranscriberBot.get().start_thread(message_id)
    logger.debug("Starting thread %d", message_id)

    keyboard = InlineKeyboardMarkup(
        [[InlineKeyboardButton("Stop", callback_data=message_id)]])

    text = ""
    if is_group:
        text = R.get_string_resource("transcription_text", lang) + "\n"
    success = False

    for speech in audiotools.transcribe(path, api_key):
        logger.debug("Thread %d running: %r", message_id,
                     TranscriberBot.get().thread_running(message_id))
        if TranscriberBot.get().thread_running(message_id) is False:
            TranscriberBot.get().del_thread(message_id)
            return

        retry = True
        retry_num = 0

        while retry and TranscriberBot.get().thread_running(message_id):
            try:
                if len(text + " " + speech) >= 4000:
                    text = R.get_string_resource("transcription_continues",
                                                 lang) + "\n"
                    message = bot.send_message(
                        chat_id=chat_id,
                        text=text + " " + speech + " <b>[...]</b>",
                        reply_to_message_id=message.message_id,
                        parse_mode="html",
                        is_group=is_group,
                        reply_markup=keyboard).result()
                else:
                    message = bot.edit_message_text(
                        text=text + " " + speech + " <b>[...]</b>",
                        chat_id=chat_id,
                        message_id=message.message_id,
                        parse_mode="html",
                        is_group=is_group,
                        reply_markup=keyboard).result()

                text += " " + speech
                retry = False
                success = True

            except telegram.error.TimedOut as t:
                logger.error("Timeout error %s", traceback.format_exc())
                retry_num += 1
                if retry_num >= 3:
                    retry = False

            except telegram.error.RetryAfter as r:
                logger.warning("Retrying after %d", r.retry_after)
                time.sleep(r.retry_after)

            except telegram.error.TelegramError as te:
                logger.error("Telegram error %s", traceback.format_exc())
                retry = False

            except Exception as e:
                logger.error("Exception %s", traceback.format_exc())
                retry = False

    retry = True
    retry_num = 0
    while retry and TranscriberBot.get().thread_running(message_id):
        try:
            if success:
                bot.edit_message_text(text=text,
                                      chat_id=chat_id,
                                      message_id=message.message_id,
                                      parse_mode="html",
                                      is_group=is_group)
            else:
                bot.edit_message_text(R.get_string_resource(
                    "transcription_failed", lang),
                                      chat_id=chat_id,
                                      message_id=message.message_id,
                                      parse_mode="html",
                                      is_group=is_group)
            retry = False
        except telegram.error.TimedOut as t:
            logger.error("Timeout error %s", traceback.format_exc())
            retry_num += 1
            if retry_num >= 3:
                retry = False

        except telegram.error.RetryAfter as r:
            logger.warning("Retrying after %d", r.retry_after)
            time.sleep(r.retry_after)

        except telegram.error.TelegramError as te:
            logger.error("Telegram error %s", traceback.format_exc())
            retry = False

        except Exception as e:
            logger.error("Exception %s", traceback.format_exc())
            retry = False

    TranscriberBot.get().del_thread(message_id)
Beispiel #2
0
    def run(self):
        path = self.path
        lang = "en"
        chat_id = get_chat_id(self.update)
        message_id = get_message_id(self.update)
        is_group = chat_id < 0

        print("Started Processing. ID: " + str(message_id))

        keyboard = InlineKeyboardMarkup(
            [[InlineKeyboardButton("Stop", callback_data=message_id)]])
        message = self.bot.send_message(chat_id=chat_id,
                                        text="Transcribing..\n",
                                        reply_to_message_id=message_id,
                                        parse_mode="html",
                                        is_group=is_group)
        text = "<b>Text:</b>" + "\n"
        success = False

        for speech in audiotools.transcribe(path, lang):
            retry = True
            retry_num = 0

            while retry:
                try:
                    if len(text + speech) >= 4080:
                        text = "<b>[continues]:</b>" + "\n"
                        message = self.bot.send_message(
                            chat_id=chat_id,
                            text=text + speech + " <b>[..]</b>",
                            reply_to_message_id=message.message_id,
                            parse_mode="html",
                            is_group=is_group,
                            reply_markup=keyboard)
                    else:
                        message = self.bot.edit_message_text(
                            text=text + speech + " <b>[..]</b>",
                            chat_id=chat_id,
                            message_id=message.message_id,
                            parse_mode="html",
                            is_group=is_group,
                            reply_markup=keyboard)

                    text += ' ' + speech
                    retry = False
                    success = True

                except telegram.error.TimedOut as t:
                    logger.error("Timeout error %s", traceback.format_exc())
                    retry_num += 1
                    if retry_num >= 3:
                        retry = False

                except telegram.error.RetryAfter as r:
                    logger.warning("Retrying after %d", r.retry_after)
                    time.sleep(r.retry_after)

                except telegram.error.TelegramError as te:
                    logger.error("Telegram error %s", traceback.format_exc())
                    retry = False

                except Exception as e:
                    logger.error("Exception %s", traceback.format_exc())
                    retry = False

        retry = True
        retry_num = 0
        while retry:
            try:
                if success:
                    self.bot.edit_message_text(text=text,
                                               chat_id=chat_id,
                                               message_id=message.message_id,
                                               parse_mode="html",
                                               is_group=is_group)
                else:
                    self.bot.edit_message_text("Could not transcribe audio\n",
                                               chat_id=chat_id,
                                               message_id=message.message_id,
                                               parse_mode="html",
                                               is_group=is_group)
                retry = False
            except telegram.error.TimedOut as t:
                logger.error("Timeout error %s", traceback.format_exc())
                retry_num += 1
                if retry_num >= 3:
                    retry = False

            except telegram.error.RetryAfter as r:
                logger.warning("Retrying after %d", r.retry_after)
                time.sleep(r.retry_after)

            except telegram.error.TelegramError as te:
                logger.error("Telegram error %s", traceback.format_exc())
                retry = False

            except Exception as e:
                logger.error("Exception %s", traceback.format_exc())
                retry = False
            finally:
                if os.path.exists(path):
                    os.remove(path)
                print("Completed. ID: " + str(message_id))