Не работает кнопки на инлайн клавиатуре в библиотеке telebot
import telebot
import config
from telebot import types
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands=['start'])
def welcome(message):
#sti = open('static/welcome.webp', 'rb')
#bot.send_sticker(message.chat.id, sti)
# keyboard
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Записаться на приём")
item2 = types.KeyboardButton("посмотреть запись")
item3 = types.KeyboardButton("Отрыть адрес в 2гис")
markup.add(item1, item2, item3)
bot.send_message(message.chat.id, "Ассаляму алейкум, {0.first_name}!\nЯ - <b>{1.first_name}</b>, который осуществляет ремонт впрыска, электрик. Адрес улица джумалиева 13 А город Алматы. Телефон +77772398509. В этом боте вы можете занять очередь на прием онлайн и рассказать что вас беспокоит в автомобиле.".format(message.from_user, bot.get_me()),
parse_mode='html', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def zapis(message):
if message.chat.type == 'private':
if message.text == 'посмотреть запись':
bot.send_message(message.chat.id, 'за какой день?')
elif message.text == 'Записаться на приём':
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("Понедельник", callback_data='PN')
item2 = types.InlineKeyboardButton("Вторник", callback_data='VT')
item3 = types.InlineKeyboardButton("Среда", callback_data='SR')
item4 = types.InlineKeyboardButton("Четверг", callback_data='CHT')
item5 = types.InlineKeyboardButton("Пятница", callback_data='PT')
item6 = types.InlineKeyboardButton("Суббота", callback_data='SB')
item7 = types.InlineKeyboardButton("Воскресенье", callback_data='VS')
item8 = types.InlineKeyboardButton ("Назад", callback_data = 'nazad')
markup.add(item1, item2, item3, item4, item5, item6, item7, item8)
bot.send_message(message.chat.id, 'Выберите день', reply_markup=markup)
elif message.text == 'Отрыть адрес в 2гис' :
bot.send_message(message.chat.id, 'нажмите на ссылку, чтобы посмотреть адрес в 2гис https://go.2gis.com/691dk')
else:
bot.send_message(message.chat.id, 'Я не знаю что ответить ?')
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == 'PN':
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("9:00", callback_data='9')
item2 = types.InlineKeyboardButton("10:00", callback_data='10')
item3 = types.InlineKeyboardButton("11:00", callback_data='11')
item4 = types.InlineKeyboardButton("12:00", callback_data='12')
item5 = types.InlineKeyboardButton("13:00", callback_data='13')
item6 = types.InlineKeyboardButton("14:00", callback_data='14')
item7 = types.InlineKeyboardButton("15:00", callback_data='15')
item8 = types.InlineKeyboardButton ("Назад", callback_data = 'nazad')
markup.add(item1, item2, item3, item4, item5, item6, item7, item8)
bot.send_message(message.chat.id, 'Вы записаны', reply_markup=markup)
bot.send_message(call.message.chat.id, 'Вот и отличненько ?')
elif call.data == 'VT':
bot.send_message(call.message.chat.id, 'Бывает ?')
# remove inline buttons
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="? Как дела?",
reply_markup=None)
# show alert
bot.answer_callback_query(callback_query_id=call.id, show_alert=False,
text="ЭТО ТЕСТОВОЕ УВЕДОМЛЕНИЕ!!11")
except Exception as e:
print(repr(e))
# RUN
bot.polling(none_stop=True)
Ответы (1 шт):
Автор решения: Bot Worker
→ Ссылка
Исправленный код:
import telebot
import config
from telebot import types
bot = telebot.TeleBot(config.TOKEN)
@bot.message_handler(commands=['start'])
def welcome(message):
#sti = open('static/welcome.webp', 'rb')
#bot.send_sticker(message.chat.id, sti)
# keyboard
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
item1 = types.KeyboardButton("Записаться на приём")
item2 = types.KeyboardButton("посмотреть запись")
item3 = types.KeyboardButton("Отрыть адрес в 2гис")
markup.add(item1, item2, item3)
bot.send_message(message.chat.id, "Ассаляму алейкум, {0.first_name}!\nЯ - <b>{1.first_name}</b>, который осуществляет ремонт впрыска, электрик. Адрес улица ######## ## А город #####. Телефон ############. В этом боте вы можете занять очередь на прием онлайн и рассказать что вас беспокоит в автомобиле.".format(message.from_user, bot.get_me()),
parse_mode='html', reply_markup=markup)
@bot.message_handler(content_types=['text'])
def zapis(message):
if message.chat.type == 'private':
if message.text == 'посмотреть запись':
bot.send_message(message.chat.id, 'за какой день?')
elif message.text == 'Записаться на приём':
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("Понедельник", callback_data='PN')
item2 = types.InlineKeyboardButton("Вторник", callback_data='VT')
item3 = types.InlineKeyboardButton("Среда", callback_data='SR')
item4 = types.InlineKeyboardButton("Четверг", callback_data='CHT')
item5 = types.InlineKeyboardButton("Пятница", callback_data='PT')
item6 = types.InlineKeyboardButton("Суббота", callback_data='SB')
item7 = types.InlineKeyboardButton("Воскресенье", callback_data='VS')
item8 = types.InlineKeyboardButton ("Назад", callback_data = 'nazad')
markup.add(item1, item2, item3, item4, item5, item6, item7, item8)
bot.send_message(message.chat.id, 'Выберите день', reply_markup=markup)
elif message.text == 'Отрыть адрес в 2гис' :
bot.send_message(message.chat.id, 'нажмите на ссылку, чтобы посмотреть адрес в 2гис https://go.2gis.com/691dk')
else:
bot.send_message(message.chat.id, 'Я не знаю что ответить ?')
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
try:
if call.message:
if call.data == 'PN':
markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("9:00", callback_data='9')
item2 = types.InlineKeyboardButton("10:00", callback_data='10')
item3 = types.InlineKeyboardButton("11:00", callback_data='11')
item4 = types.InlineKeyboardButton("12:00", callback_data='12')
item5 = types.InlineKeyboardButton("13:00", callback_data='13')
item6 = types.InlineKeyboardButton("14:00", callback_data='14')
item7 = types.InlineKeyboardButton("15:00", callback_data='15')
item8 = types.InlineKeyboardButton ("Назад", callback_data = 'nazad')
markup.add(item1, item2, item3, item4, item5, item6, item7, item8)
bot.send_message(call.message.chat.id, 'Вы записаны', reply_markup=markup)
bot.send_message(call.message.chat.id, 'Вот и отличненько ?')
elif call.data == 'VT':
bot.send_message(call.message.chat.id, 'Бывает ?')
# remove inline buttons
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="? Как дела?",
reply_markup=None)
# show alert
bot.answer_callback_query(callback_query_id=call.id, show_alert=False,
text="ЭТО ТЕСТОВОЕ УВЕДОМЛЕНИЕ!!11")
except Exception as e:
print(repr(e))
# RUN
bot.polling(none_stop=True)
Ошибка заключалась в том что в строке
bot.send_message(message.chat.id, 'Вы записаны', reply_markup=markup)
не находило переменной message, так как в функцию было передано call, нужно было исправить на
bot.send_message(call.message.chat.id, 'Вы записаны', reply_markup=markup)