Good day. To begin with I will give all the code of my program, and then ask questions on the items.

# -*- coding: utf-8 -*- import config import telebot import MongoDB from collections import OrderedDict bot = telebot.TeleBot(config.token) counter = 0 Group_id = None Classes = [] @bot.message_handler(commands=['add']) def add(message): bot.send_message(message.chat.id, "Введите номер группы для добавления в базу данных") add_answer @bot.message_handler(commands=['get']) def get(message): bot.send_message(message.chat.id, "Введите номер группы") get_answer @bot.message_handler(func=lambda message: True, content_types=['text']) def get_answer(message): db = MongoDB.MongoDB(config.database_address) bot.send_message(message.chat.id, db.get(Group_id=message.text.upper())) return False @bot.message_handler(func=lambda message: True, content_types=['text']) def add_answer(message): global Group_id global counter global Classes if (counter == 0): Group_id = message.text.upper() bot.send_message(message.chat.id, "Введите расписание для " + config.Days_of_week_for_chat[0]) counter += 1 Classes.append(Group_id) elif (counter == 1): bot.send_message(message.chat.id, "Введите расписание для " + config.Days_of_week_for_chat[1]) counter += 1 Classes.append(message.text) elif (counter == 2): bot.send_message(message.chat.id, "Введите расписание для " + config.Days_of_week_for_chat[2]) counter += 1 Classes.append(message.text) elif (counter == 3): bot.send_message(message.chat.id, "Введите расписание для " + config.Days_of_week_for_chat[3]) counter += 1 Classes.append(message.text) elif (counter == 4): bot.send_message(message.chat.id, "Введите расписание для " + config.Days_of_week_for_chat[4]) counter += 1 Classes.append(message.text) elif (counter == 5): bot.send_message(message.chat.id, "Введите расписание для " + config.Days_of_week_for_chat[5]) counter += 1 Classes.append(message.text) elif (counter == 6): Classes.append(message.text) Schedule = OrderedDict() Temp = dict(zip(config.Days_of_week, Classes)) for days in config.Days_of_week: Schedule[days] = Temp[days] db = MongoDB.MongoDB(config.database_address) bot.send_message(message.chat.id, db.insert(Group_id, Schedule)) return False if __name__ == '__main__': bot.polling(none_stop=True) 

First question: As you can see, in the add_answer (message) function, I decided to add a question by adding values ​​by days of the week through a global variable. If this is the only correct option in this case, then it is good, but if there is a more concise way, please tell us about it. I tried to solve this problem and like this:

 for day in config.Days_of_week_for_chat: bot.send_message(message.chat.id, "Введите расписание для " + day) 

but failed because the bot issued the following: Fiasco

In my bot there are two commands / get and / add. If I use the / get command and then / add, the messages continue to be processed / get. In other words, the question is how to stop processing commands with one of the methods, that is, to return the program to its initial state? Here's what it looks like: enter image description here

One person told me that the whole thing is the same third and fourth handler, and in this case it is necessary to build a state machine. Honestly, I don’t quite understand how to do this, and it’s interesting to know other solutions to this problem. The third question: It comes from here.

One person told me that the whole thing is the same third and fourth handler

I do not quite understand how this affects, because the function names are different, and I thought that python would be enough.

    1 answer 1

    Pay attention to the decorators over these handlers @bot.message_handler(func=lambda message: True, content_types=['text']) , in parameters of which func is the condition for triggering the handler.

    As an option, you can use the following approach for resolution using the register_next_step_handler method. Example: https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py