F
F
Frag0S2018-05-11 17:24:50
Bots
Frag0S, 2018-05-11 17:24:50

Why does the bot freeze when sending a message to the telegram submenu?

Hello, tell me what's the matter.
1) If you go to the submenu and send any message to the bot, it does not respond to further button calls. For example, when you press the "Teachers" button, we will go to the submenu with the "IVT", "GF", "Back to the list" buttons, then if we send a message to the chat, the bot freezes.
2) Additional question)) Is it possible to implement the back button somehow more simply? Return not by the text of the button, but track where the user was before ...
Here is the source

spoiler
import config
import telebot
import json
from telebot import types

bot = telebot.TeleBot(config.token)

json_data = json.load(open('bd.json'))

@bot.message_handler(commands=['start'])
def first(message):
  key = telebot.types.ReplyKeyboardMarkup(True,False)
  key.row("Преподаватели")
  key.row("Расписание звонков")
  key.row("Расписание")
  bot.send_message(message.chat.id, "Выберите пункт", reply_markup=key)
  
@bot.message_handler(content_types=['text'])
def main(message):
  if message.text == "Расписание звонков":
    bot.send_photo(message.chat.id, open('/home/frag0s/Рабочий стол/bbot/rasp.jpg', 'rb'))
  elif message.text == "Расписание":
    for user in json_data:
      bot.send_message(message.chat.id, 
        		str(user['is541']['name_day'])+":\n"
        		+str(user['is541']['lesson1']['time'])+"\t"+str(user['is541']['lesson1']['name_lesson'])+"\n"
       			+str(user['is541']['lesson2']['time'])+"\t"+str(user['is541']['lesson2']['name_lesson'])+"\n"
        		+str(user['is541']['lesson3']['time'])+"\t"+str(user['is541']['lesson3']['name_lesson'])+"\n"
            +str(user['is541']['lesson4']['time'])+"\t"+str(user['is541']['lesson4']['name_lesson'])+"\n"
            +str(user['is541']['lesson5']['time'])+"\t"+str(user['is541']['lesson5']['name_lesson']))
  elif message.text == "Преподаватели":
    key = telebot.types.ReplyKeyboardMarkup(True,False)
    second(message)


def second(message):
  if message.text == "Преподаватели":
    keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    keyboard.row("ИВТ","ГФ")
    keyboard.row("Вернуться к списку")
    send = bot.send_message(message.from_user.id, "Выберите факультет", reply_markup=keyboard)
    bot.register_next_step_handler(send, third)

def third(message):
  if message.text == "ИВТ":
    keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    keyboard.row("Назад")
    send = bot.send_message(message.from_user.id, "name1", reply_markup=keyboard)
    bot.register_next_step_handler(send, fin)
  elif message.text == "ГФ":
    keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    keyboard.row("Назад")
    send = bot.send_message(message.from_user.id, "name2", reply_markup=keyboard)
    bot.register_next_step_handler(send, fin)
  elif message.text == "Вернуться к списку":
    first(message)

def six(message):
  if message.text == "Вернуться к списку":
    keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    keyboard.row("Преподаватели")
    keyboard.row("Расписание звонков")
    keyboard.row("Расписание")
    send = bot.send_message(message.from_user.id, "Выберите пункт")
    bot.register_next_step_handler(send, second)

def fin(message):
  if message.text == "Назад":
    keyboard = telebot.types.ReplyKeyboardMarkup(resize_keyboard=True, one_time_keyboard=False)
    keyboard.row("ИВТ", "ГФ")
    keyboard.row("Вернуться к списку")
    send = bot.send_message(message.from_user.id, "Выберите факультет", reply_markup=keyboard)
    bot.register_next_step_handler(send, third)

if __name__ == '__main__':
  bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ranc58, 2018-05-11
@Ranc58

Regarding question 2:
Implement a state machine and keep track of the state.
A very simplified example:

@bot.message_handler(func=lambda message: True)
def start_finite_machine(message):
    if STATE_DICT[chat_id] == 'position_1':
        position_1(message)
    elif STATE_DICT[chat_id] == 'position_2':
         position_2(message)
    elif STATE_DICT[chat_id] == 'position_3':
        position_3(message)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question