Answer the question
In order to leave comments, you need to log in
telebot state machines register_next_step_handler?
The essence of the question is why the register_next_step_handler function does not end after being called multiple times, but continues to be called with subsequent bot commands.
I give an example in the form of screenshots:
When I call one command many times, it does not complete and starts to duplicate with the following commands
And so it continues with all menu items
Here is the source code that I got
import telebot
import user
import json
from telebot import apihelper
from telebot.types import Message
from telebot import types
bot = telebot.TeleBot(config.token)
apihelper.proxy = {'https': 'xxx'}
def keyboard_menu():
print("вызов keyboard_menu")
keyboard_menu = telebot.types.ReplyKeyboardMarkup(True)
keyboard_menu.row('Город')
keyboard_menu.row('Способы оплаты')
return keyboard_menu
def keyboard_sity():
print("вызов keyboard_sity")
keyboard_menu = telebot.types.ReplyKeyboardMarkup(True)
keyboard_menu.row('Главное меню')
return keyboard_menu
def send_text(message):
if message.text == 'Город':
bot.send_message(message.chat.id, 'Выберите город из ниже предоставленных:',reply_markup=keyboard_sity())
bot.register_next_step_handler(message, sity)
else:
bot.send_message(message.chat.id, '*Выберите один из пунктов меню*',parse_mode="Markdown",reply_markup=keyboard_menu())
bot.register_next_step_handler(message, send_text)
def sity(message):
if message.text.lower() == 'главное меню':
bot.send_message(message.chat.id, '*Выберите один из пунктов меню*' ,reply_markup=keyboard_menu() , parse_mode="Markdown")
bot.register_next_step_handler(message,send_text)
else:
bot.send_message(message.chat.id,'такого города не знаем =(')
bot.register_next_step_handler(message, sity)
@bot.message_handler(commands=['start'])
def start_message(message):
if message.text == '/start':
start_bot = f'*Приветствуем {message.chat.username}*\n\n*Выберите один из пунктов меню*'
bot.send_message(message.chat.id, start_bot ,reply_markup=keyboard_menu(),parse_mode="Markdown")
@bot.message_handler(content_types=['text'])
def text(message):
bot.send_message(message.chat.id,'*Нажмите /start*',reply_markup=telebot.types.ReplyKeyboardRemove(),parse_mode="Markdown")
bot.register_next_step_handler(message, send_text)
while True:
try:
bot.polling(none_stop=True,interval=3)
except Exception as e:
print(e)
time.sleep(15)
Answer the question
In order to leave comments, you need to log in
I didn't manage to solve the problem myself, but this helped me:
types.ReplyKeyboardMarkup(one_time_keyboard=True,
resize_keyboard=True)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question