Как отключить message_handler в telebot?

Планирую в бот добавить несколько хендлеров. Мне нужно поэтапно их включать и отключать. Например: человек общается с ботом, после приветствия бот спрашивает как дела.. Человек отвечает, диалог идёт дальше. Если прописать варианты ответа в тот-же хендлер, оставив в else например 'что ты сказал?', то бот будет отвечать двумя сообщениями (ответом и else). Можно ли как-то отключать и включать хендлеры?

#import_libraries
import telebot
import config
import random
import time
import datetime
from telebot import types

#randomisers
welcome_count = random.randint(0, 2)
how_are_you_count = random.randint(0, 2)
delay = random.randint(0, 3)

#inside_data
now = datetime.datetime.now()
token = config.token
how_are_you_messages = ['как дела?', 'как твои дела?', 'как ты?']
hello_in = ['привет', 'Привет', 'приветик', 'Приветик', 'здравствуй', 'Здравствуй',
    'привет*', 'Привет*', 'приветик*', 'Приветик*', 'здравствуй*', 'Здравствуй*']
hello_messages = ['здравствуй, милый', 'приветик', 'привет']
hello_res = hello_messages[welcome_count]
how_are_you_res = how_are_you_messages[how_are_you_count]
real_time = now.strftime('%H:%M:%S')

#keyboards
#how_are_you_keyboard
keyboard_hau = types.ReplyKeyboardMarkup(row_width=2) 
keyboard_hau_b1 = types.InlineKeyboardButton('всё отлично!!')
keyboard_hau_b2 = types.InlineKeyboardButton('всё хорошо')
keyboard_hau_b3 = types.InlineKeyboardButton('не очень')
keyboard_hau_b4 = types.InlineKeyboardButton('очень плохо')
keyboard_hau.add(keyboard_hau_b1)
keyboard_hau.add(keyboard_hau_b2)
keyboard_hau.add(keyboard_hau_b3)
keyboard_hau.add(keyboard_hau_b4)


#starter
bot = telebot.TeleBot(token)

#main_code
def update_time():
    real_time = now.strftime('%H:%M:%S')

@bot.message_handler(commands=['start'])
def welcome(message):
    now = datetime.datetime.now()
    print('[LOG - ' + now.strftime('%H:%M:%S') + '] new session started')
    now = datetime.datetime.now()
    print('[LOG - ' + now.strftime('%H:%M:%S') + '] user = ' + message.chat.username)

@bot.message_handler(content_types=['text'])
def hello_message(message):
    if message.text in hello_in:
        bot.send_message(message.chat.id, hello_res)
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] hello_message sent')
        time.sleep(delay)
        bot.send_message(message.chat.id, how_are_you_res, reply_markup=keyboard_hau)
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] how_are_you message sent')
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] how_are_you message delay(' + str(delay) + ')')
    else:
        bot.send_message(message.chat.id, 'что?')
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] hello_message not in hello_in')

#infinity_work
bot.infinity_polling()

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

Автор решения: arnold

Как вариант:

import telebot
import random
import time
import datetime
from telebot import types

#randomisers
welcome_count = random.randint(0, 2)
how_are_you_count = random.randint(0, 2)
delay = random.randint(0, 3)

#inside_data
now = datetime.datetime.now()
token = config.token
how_are_you_messages = ['как дела?', 'как твои дела?', 'как ты?']
hello_in = ['привет', 'Привет', 'приветик', 'Приветик', 'здравствуй', 'Здравствуй',
    'привет*', 'Привет*', 'приветик*', 'Приветик*', 'здравствуй*', 'Здравствуй*']
hello_messages = ['здравствуй, милый', 'приветик', 'привет']
hello_res = hello_messages[welcome_count]
how_are_you_res = how_are_you_messages[how_are_you_count]
real_time = now.strftime('%H:%M:%S')

#keyboards
#how_are_you_keyboard
keyboard_hau = types.ReplyKeyboardMarkup(row_width=2)
keyboard_hau_b1 = types.InlineKeyboardButton('всё отлично!!')
keyboard_hau_b2 = types.InlineKeyboardButton('всё хорошо')
keyboard_hau_b3 = types.InlineKeyboardButton('не очень')
keyboard_hau_b4 = types.InlineKeyboardButton('очень плохо')
keyboard_hau.add(keyboard_hau_b1)
keyboard_hau.add(keyboard_hau_b2)
keyboard_hau.add(keyboard_hau_b3)
keyboard_hau.add(keyboard_hau_b4)


#starter
bot = telebot.TeleBot(token)

#main_code
def update_time():
    real_time = now.strftime('%H:%M:%S')

@bot.message_handler(commands=['start'])
def welcome(message):
    now = datetime.datetime.now()
    print('[LOG - ' + now.strftime('%H:%M:%S') + '] new session started')
    now = datetime.datetime.now()
    print('[LOG - ' + now.strftime('%H:%M:%S') + '] user = ' + message.chat.username)


words = ['всё отлично!!', 'всё хорошо', 'не очень', 'очень плохо']


@bot.message_handler(content_types=['text'])
def hello_message(message):
    if message.text in hello_in:
        bot.send_message(message.chat.id, hello_res)
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] hello_message sent')
        time.sleep(delay)
        bot.send_message(message.chat.id, how_are_you_res, reply_markup=keyboard_hau)
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] how_are_you message sent')
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] how_are_you message delay(' + str(delay) + ')')

    elif message.text in words:
        if message.text == "всё отлично!!":
            bot.send_message(message.chat.id, "супер!")
        elif message.text == "всё хорошо":
            bot.send_message(message.chat.id, "рад слышать!")
        elif message.text == "не очень":
            bot.send_message(message.chat.id, "почему?")
        elif message.text == "очень плохо":
            bot.send_message(message.chat.id, "раскажи, что случилось")

    else:
        bot.send_message(message.chat.id, 'что?')
        now = datetime.datetime.now()
        print('[LOG - ' + now.strftime('%H:%M:%S') + '] hello_message not in hello_in')

#infinity_work
bot.infinity_polling()
→ Ссылка