Skip to content

Commit

Permalink
wrapper to new file
Browse files Browse the repository at this point in the history
  • Loading branch information
feyhoa committed Sep 13, 2023
1 parent d0ad56f commit e3de577
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 48 deletions.
39 changes: 4 additions & 35 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion script_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: # класс для хранения дерева
Expand Down
13 changes: 1 addition & 12 deletions silero_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions wrapper.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e3de577

Please sign in to comment.