Во время использования telegram бота команды по типу /start, /help не срабатывают с первого раза. С чем это может быть связано ?
import logging
import telebot
BOT_TOKEN = '7777'
bot = telebot.TeleBot(BOT_TOKEN)
user_last_messages = {}
BACK_BTN = 'Back'
NEXT_BTN = 'Next'
def get_reply_markup(keyboard_matrix):
reply_markup = telebot.types.ReplyKeyboardMarkup(
resize_keyboard=True)
for row in keyboard_matrix:
reply_markup.row(*row)
return reply_markup
# [3]
def get_matrix_of_numbers_from_1_to_50():
matrix = []
numbers_from_1_to_50 = list(range(1, 51))
try:
while True:
matrix_row = []
for _ in range(5):
next_number = numbers_from_1_to_50.pop(0)
matrix_row.append(str(next_number))
matrix.append(matrix_row)
except IndexError:
return matrix
# [3]
def get_reply_markup_of_numbers_from_1_to_50():
keyboard_matrix = get_matrix_of_numbers_from_1_to_50()
reply_markup = get_reply_markup(keyboard_matrix)
reply_markup.row(BACK_BTN)
return reply_markup
def get_reply_markup_with_back_btn():
return get_reply_markup([[BACK_BTN]])
@bot.message_handler(commands=['start'])
# [3]
def handle_all_command(message):
# [4]
reply_markup = get_reply_markup_of_numbers_from_1_to_50()
# [4]
sent_message = bot.send_message(
chat_id=message.from_user.id, text='Hi', reply_markup=reply_markup)
bot.register_next_step_handler(sent_message, handle_numbers_keyboard_press)
# [3]
def handle_numbers_keyboard_press(message):
user_last_messages[message.from_user.id] = message
if not message.text.isdigit():
return None
back_btn_reply_markup = get_reply_markup_with_back_btn()
inline_keyboard = telebot.types.InlineKeyboardMarkup()
inline_keyboard.add(
telebot.types.InlineKeyboardButton(
text='Next ➡️', callback_data='Next'))
# [5]
bot.send_message(
chat_id=message.from_user.id,
text=f'Number is:',
reply_markup=back_btn_reply_markup)
return bot.send_message(
chat_id=message.from_user.id,
text=message.text,
reply_markup= inline_keyboard)
@bot.callback_query_handler(func=lambda c: True)
def callback_inline(callback_data):
# [6]
if callback_data.data != NEXT_BTN:
return None
user_last_message = user_last_messages[callback_data.from_user.id]
user_last_message.text = str(int(user_last_message.text) + 1)
bot_sent_message = handle_numbers_keyboard_press(user_last_message)
bot.register_next_step_handler(bot_sent_message, handle_all_command)
if __name__ == '__main__':
bot.polling(none_stop=True)