Почему зависает Telegram бот на Python?

def gopay(message):
    markup = types.ReplyKeyboardMarkup()

    if message.text.lower() == 'на главную':
        start_page(message)
    current_user = user_manager.get_user(message)
    path = current_user.path
    if not path:
        current_user.path = os.path.abspath(os.curdir) + '/app' + '/' + 'files/'
        try:
            db.session.commit()
        except:
            db.session.rollback()

    if "Apple" in os.listdir(current_user.path):
        markup.add(types.KeyboardButton('Главное меню'))
        main_folder = os.listdir(user_manager.get_user(message).path)
        main_folder.sort()
        for i in main_folder:
            markup.add(types.KeyboardButton(i))

        msg = bot.send_message(message.chat.id, "Выберите производителя",
                               reply_markup=markup)

    else:
        markup.add(types.KeyboardButton('Главное меню'))
        markup.add(types.KeyboardButton('Назад'))
        current_folder = os.listdir(current_user.path)
        current_folder.sort()
        for i in current_folder:
            if '.jpg' not in i:
                markup.add(types.KeyboardButton(i))
        msg = bot.send_message(message.chat.id, current_user.path.split('/')[-1].title() + ". Выберите устройство",
                               reply_markup=markup)
    bot.register_next_step_handler(msg, pay)
def pay(message):
    if message.text.lower() == 'назад':
        current_user = user_manager.get_user(message)
        current_user.path = '/'.join(current_user.path.split('/')[:-1])
        try:
            db.session.commit()
        except:
            db.session.rollback()
        gopay(message)
    elif message.text.lower() in ['главное меню', 'на главную']:
        start_page(message)
    elif "." not in message.text:
        current_user = user_manager.get_user(message)
        current_user.path = current_user.path + os.sep + message.text
        try:
            db.session.commit()
        except:
            db.session.rollback()
            current_user.path = current_user.path + os.sep + message.text
            db.session.commit()
        gopay(message)
    else:
        current_user = user_manager.get_user(message)
        path = current_user.path
        if message.text not in os.listdir(path):
            bot.send_message(message.chat.id, 'Ошибка. Вы ввели неверное значение.')
            gopay(message)

        else:
            ...

Выше привожу фрагмент кода. current_user - это объект пользователя Flack SQL alchemy, а сам скрипт - это реализация каталога из структуры папок в файловой системе. Периодически бот может зависать по непонятным мне причинам и просто игнорировать сообщение пользователя. Проблема однозначно на стороне СУБД, но что я именно делаю не так? Понимаю, что это откровенно не лучшее решение реализации каталога, но тем не менее - ботом пользуются менее 10 человек, нагрузки практически нет. Хочу понять почему это происходит и как решить проблему. Бот запущен на хостинге в Reg.ru с поддержкой Flask на панели управления ISP Manager. Пробовал меня СУБД с SQLite на MySQL, но это не помогло. Как обнаружить причину, если с логгированием сейчас трудности, а искусственно получить зависание не удается? Библиотеки: Flask, FlaskSQLAlchemy, Telebot.


Ответы (0 шт):