Answer the question
In order to leave comments, you need to log in
How to insert register_next_step_handler into callback_query_handler?
Tell me how to insert register_next_step_handler into the inline keyboard handler?
elif message.text == ' Регистрация велосипеда ':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
btn1 = types.KeyboardButton(' Где наша мастерская? ')
btn2 = types.KeyboardButton(' Посмотреть прайс на ремонт ')
btn3 = types.KeyboardButton(' Наши контакты ')
btn4 = types.KeyboardButton('⏭ Далее ⏭')
markup.add(btn1, btn2, btn3, btn4)
bot.send_message(message.chat.id, 'Давайте запишем Ваш байк в базу данных.', reply_markup=markup)
markup_inline = types.InlineKeyboardMarkup()
bk1 = types.InlineKeyboardButton(text='Stels', callback_data='Stels')
bk2 = types.InlineKeyboardButton(text='Forvard', callback_data='Forvard')
bk3 = types.InlineKeyboardButton(text='Кама', callback_data='Кама')
bk4 = types.InlineKeyboardButton(text='Урал', callback_data='Урал')
markup_inline.add(bk1, bk2, bk3, bk4)
bot.send_message(message.chat.id, 'Выберите модель', reply_markup=markup_inline)
bot.register_next_step_handler(message, reg)
@bot.callback_query_handler(func=lambda call: True)
def reg(call):
if call.data == 'Stels':
chat_id = call.message.chat.id
conn = sqlite3.connect('db.db')
cursor = conn.cursor()
cursor.execute("UPDATE users SET brand = ? WHERE chat_id = ?", (f'{call.data}', f'{chat_id}'))
conn.commit()
conn.close()
elif call.data == 'Forvard':
chat_id = call.message.chat.id
conn = sqlite3.connect('db.db')
cursor = conn.cursor()
cursor.execute("UPDATE users SET brand = ? WHERE chat_id = ?", (f'{call.data}', f'{chat_id}'))
conn.commit()
conn.close()
elif call.data == 'Forvard':
chat_id = call.message.chat.id
conn = sqlite3.connect('db.db')
cursor = conn.cursor()
cursor.execute("UPDATE users SET brand = ? WHERE chat_id = ?", (f'{call.data}', f'{chat_id}'))
conn.commit()
conn.close()
elif call.data == 'Кама':
chat_id = call.message.chat.id
conn = sqlite3.connect('db.db')
cursor = conn.cursor()
cursor.execute("UPDATE users SET brand = ? WHERE chat_id = ?", (f'{call.data}', f'{chat_id}'))
conn.commit()
conn.close()
elif call.data == 'Урал':
chat_id = call.message.chat.id
conn = sqlite3.connect('db.db')
cursor = conn.cursor()
cursor.execute("UPDATE users SET brand = ? WHERE chat_id = ?", (f'{call.data}', f'{chat_id}'))
conn.commit()
conn.close()
bot.send_message(chat_id, f'Записали!\nИдем дальше.')
bot.send_message(chat_id, 'Введите серийный номер')
Answer the question
In order to leave comments, you need to log in
I advise you to start by choosing a model is great. This is apparently the only item that is selected by the Inline buttons.
And if the user has a bike not from the proposed set, it will immediately jump off and will not enter too much.
I will take your code as a basis and slightly modify it
...
elif message.text == ' Регистрация велосипеда ':
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
btn1 = types.KeyboardButton(' Где наша мастерская? ')
btn2 = types.KeyboardButton(' Посмотреть прайс на ремонт ')
btn3 = types.KeyboardButton(' Наши контакты ')
btn4 = types.KeyboardButton('⏭ Далее ⏭')
markup.add(btn1, btn2, btn3, btn4)
bot.send_message(message.chat.id, 'Давайте запишем Ваш байк в базу данных.', reply_markup=markup)
markup_inline = types.InlineKeyboardMarkup()
# Обратите внимание на callback_data при создании кнопки
bk1 = types.InlineKeyboardButton(text='Stels', callback_data='bike_model|Stels')
bk2 = types.InlineKeyboardButton(text='Forvard', callback_data='bike_model|Forvard')
bk3 = types.InlineKeyboardButton(text='Кама', callback_data='bike_model|Кама')
bk4 = types.InlineKeyboardButton(text='Урал', callback_data='bike_model|Урал')
markup_inline.add(bk1, bk2, bk3, bk4)
bot.send_message(message.chat.id, 'Выберите модель', reply_markup=markup_inline)
# На случай, если у вас будут еще какие-либо инлайн кнопки,
# этот хендлер сработает только на тех, у которых callback_data начинается с bike_model
@bot.callback_query_handler(func=lambda call: call.data.startswith('bike_model'))
def reg(call):
# получаем модель велика
bike_model = call.data.split('|')[1]
chat_id = call.message.chat.id
conn = sqlite3.connect('db.db')
cursor = conn.cursor()
cursor.execute(f"UPDATE users SET brand = {bike_model} WHERE chat_id = {chat_id}")
conn.commit()
conn.close()
bot.send_message(chat_id, f'Записали!\nИдем дальше.')
mes = bot.send_message(chat_id, 'Введите серийный номер')
bot.register_next_step_handler(mes, serial_number)
def serial_number(message):
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question