callback_query_handler не работает в telebot. Python
Проблема в том что когда пользователь нажимает кнопку со временем программа не запускает функцию handle_callback_query при єтом в дебагере видно что поле callback_data у кнопок создалось и значения уникальные.
Код моего бота:
import json
import telebot
from telebot import types
TOKEN = 'my token :) '
bot = telebot.TeleBot(TOKEN)
times = ["08:00", "10:00", "12:00", "14:00", "16:00", "18:00", "20:00", "22:00"]
rooms = [str(i) for i in range(1, 40)]
selected_date = ''
selected_time = ''
selected_machine = ''
user_room = ''
@bot.callback_query_handler(func=lambda call: True)
def handle_callback_query(call):
print("handle_callback_query started")
if call.data[2:9] == "машинка":
bot.send_message(call.message.chat.id, f"кнопка с callback_data {call.data}")
@bot.message_handler(commands=['start'])
def main(msg, text="Вітаю!"):
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.row("Список", "Записатись")
bot.send_message(msg.chat.id, text, reply_markup=markup)
@bot.message_handler()
def info(msg):
with open('test_data.json', 'r', encoding='utf-8') as file:
content: dict = json.load(file)
if msg.text in ["Записатись", "Повернутись до дат"]:
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
counter_btn = 0
list_of_date = []
reply_text = "Оберіть дату: "
for date in content:
for mach in content.get(date):
if content.get(date).get(mach) and date not in list_of_date:
list_of_date.append(date)
for date in list_of_date:
counter_btn += 1
button = types.InlineKeyboardButton(date)
markup.add(button)
return_btn = types.InlineKeyboardButton("Головне меню")
markup.add(return_btn)
if counter_btn == 0:
reply_text = 'Нажаль в найближчі дні пральня зайнята'
bot.send_message(msg.chat.id, reply_text, reply_markup=markup)
elif msg.text == "Список":
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
button = types.InlineKeyboardButton("Головне меню")
markup.add(button)
bot.send_message(msg.chat.id, "Список записів на прання", reply_markup=markup)
elif msg.text == "Головне меню":
main(msg, "Головне меню: ")
elif msg.text in content:
global selected_date
selected_date = msg.text
print(selected_date)
# отримання вільних місць на кожну з машинок
output = [[], []]
for date in content:
if date == msg.text:
for machine in content.get(date):
for time in content.get(date).get(machine):
if machine == '1 машинка':
output[0].append(time)
elif machine == '2 машинка':
output[1].append(time)
empty_butt = ' ' # змінна для кнопки без вибору
reply = []
# створення пар для відображення
if len(output[0]) == len(output[1]):
for i in range(len(output[0])):
reply.append((output[0][i], output[1][i]))
elif len(output[0]) > len(output[1]):
for i in range(len(output[1])):
reply.append((output[0][i], output[1][i]))
for i in range(len(output[1]), len(output[0])):
reply.append((output[0][i], empty_butt))
elif len(output[0]) < len(output[1]):
for i in range(len(output[0])):
reply.append((output[0][i], output[1][i]))
for i in range(len(output[0]), len(output[1])):
reply.append((empty_butt, output[1][i]))
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
for pair in reply:
button1 = types.InlineKeyboardButton(pair[0], callback_data=f'1 машинка{reply.index(pair)}')
button2 = types.InlineKeyboardButton(pair[1], callback_data=f'2 машинка{reply.index(pair)}')
markup.add(button1, button2)
bot.send_message(msg.chat.id, "Оберіть час: ", reply_markup=markup)
elif msg.text in times:
bot.send_message(msg.chat.id, "Відправте мені номер вашої кімнати ")
elif msg.text in rooms:
return_btn = types.InlineKeyboardButton("Головне меню")
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.add(return_btn)
bot.send_message(msg.chat.id, "Записано", reply_markup=markup)
if __name__ == '__main__':
bot.infinity_polling()