diff --git a/bot.py b/bot.py index 763a52e..6e705af 100644 --- a/bot.py +++ b/bot.py @@ -39,12 +39,13 @@ from logs import init_logger from script_engine import Engine from voice_module import work_with_audio +from wrapper import dialog_wrapper from silero_module import bot_answer_audio, clear_audio_cache -from config import DEBUG_MODE, DIALOG_MODE +from config import (DEBUG_MODE, + DEBUG_ON, DEBUG_OFF) DAYS_OFFSET = 7 -TEXT_MODE, VOICE_MODE, DEBUG_ON, DEBUG_OFF = "text", "voice", "true", "false" PREPARE, TYPING, SELECT_YES_NO, MENU = "PREPARE", "TYPING", "SELECT_YES_NO", "MENU" @@ -126,7 +127,7 @@ def button(update: Update, context: CallbackContext) -> str: push_user_feeling(update.effective_user, query.data, update.effective_message.date) # debugging zone - if DEBUG_MODE: + if DEBUG_MODE == DEBUG_ON: user = init_user(update.effective_user) schedule = get_schedule_by_user(user, is_test=True) print(schedule) @@ -236,38 +237,6 @@ def send_audio_answer(update: Update, context: CallbackContext): error(update, context) -def dialog_wrapper(update: Update, text: str, reply_markup=None) -> None: - - if DIALOG_MODE == VOICE_MODE: - - try: - audio = bot_answer_audio(text) - - except Exception as er: - if DEBUG_MODE == DEBUG_ON: - raise er - elif DEBUG_MODE == DEBUG_OFF: - update.message.reply_text(f'Ошибка в синтезе речи, попробуйте позже.') - - else: - update.effective_user.send_voice(voice=audio.content, reply_markup=reply_markup) - clear_audio_cache() - - # audio = bot_answer_audio(text) - # - # if audio: - # update.effective_user.send_voice(voice=audio.content, reply_markup=reply_markup) - # clear_audio_cache() - # else: - # if DEBUG_MODE == DEBUG_ON: - # pass - # elif DEBUG_MODE == DEBUG_OFF: - # update.message.reply_text(f'Ошибка в синтезе речи, попробуйте позже.') - - elif DIALOG_MODE == TEXT_MODE: - update.effective_user.send_message(text=text, reply_markup=reply_markup) - - def main(token, mode): init_logger() diff --git a/config.py b/config.py index be0dfe0..512aa62 100644 --- a/config.py +++ b/config.py @@ -6,3 +6,5 @@ LANGUAGE = getenv('LANGUAGE') SAMPLE_RATE = getenv('SAMPLE_RATE') SPEAKER = getenv('SPEAKER') + +TEXT_MODE, VOICE_MODE, DEBUG_ON, DEBUG_OFF = "text", "voice", "true", "false" diff --git a/script_engine.py b/script_engine.py index 19e40f3..ccccdb3 100644 --- a/script_engine.py +++ b/script_engine.py @@ -7,7 +7,7 @@ from telegram.ext import CallbackContext from db import init_user, get_survey_progress, init_survey_progress, get_user_answer from keyboard import yes_no_keyboard -from bot import dialog_wrapper +from wrapper import dialog_wrapper class Script: # класс для хранения дерева diff --git a/silero_module.py b/silero_module.py index 3d22beb..452885a 100644 --- a/silero_module.py +++ b/silero_module.py @@ -5,23 +5,12 @@ class VoiceSettings: - link = 'http://silero-tts-service:9898' + link = 'http://silero-tts-service:989' speaker = SPEAKER language = LANGUAGE sample_rate = SAMPLE_RATE -# def bot_answer_audio(bot_text): -# -# request_params = {'VOICE': VoiceSettings.speaker, 'INPUT_TEXT': bot_text} -# try: -# answer = requests.get(VoiceSettings.link + '/process', params=request_params) -# except requests.exceptions.RequestException: -# return None -# -# return answer - - def bot_answer_audio(bot_text): request_params = {'VOICE': VoiceSettings.speaker, 'INPUT_TEXT': bot_text} return requests.get(VoiceSettings.link + '/process', params=request_params) diff --git a/wrapper.py b/wrapper.py new file mode 100644 index 0000000..74063d7 --- /dev/null +++ b/wrapper.py @@ -0,0 +1,26 @@ +from telegram import Update +from silero_module import bot_answer_audio, clear_audio_cache + +from config import (DEBUG_MODE, DIALOG_MODE, + TEXT_MODE, VOICE_MODE, DEBUG_ON, DEBUG_OFF) + + +def dialog_wrapper(update: Update, text: str, reply_markup=None) -> None: + + if DIALOG_MODE == VOICE_MODE: + + try: + audio = bot_answer_audio(text) + + except Exception as er: + if DEBUG_MODE == DEBUG_ON: + raise er + elif DEBUG_MODE == DEBUG_OFF: + update.message.reply_text('Ошибка в синтезе речи, попробуйте позже.') + + else: + update.effective_user.send_voice(voice=audio.content, reply_markup=reply_markup) + clear_audio_cache() + + elif DIALOG_MODE == TEXT_MODE: + update.effective_user.send_message(text=text, reply_markup=reply_markup)