D
D
Dmitry Naumov2019-10-01 17:18:52
Python
Dmitry Naumov, 2019-10-01 17:18:52

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:
nPGtj5eG2T8.jpg
When I call one command many times, it does not complete and starts to duplicate with the following commands
eP00ickaqKY.jpg
U3UZ_hqSylQ.jpg
And so it continues with all menu items
tMPoRsU95PI.jpg
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

3 answer(s)
D
Dmitry Naumov, 2019-10-25
@levap12

I didn't manage to solve the problem myself, but this helped me:

types.ReplyKeyboardMarkup(one_time_keyboard=True,
                                       resize_keyboard=True)

one_time_keyboard=True
The keyboard is immediately removed when the user selects the desired menu item. Due to this, the user will not be able to repeatedly and quickly switch through the menu.

J
Jack Surfer, 2019-10-01
@JackSurfer

Put the functions in the correct order.

T
test293, 2021-12-02
@test293

Hey!
I recommend switching to states.
https://github.com/eternnoir/pyTelegramBotAPI/blob...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question