D
D
Dmitry2022-04-05 20:39:46
Python
Dmitry, 2022-04-05 20:39:46

How to make telebot-calendar button response work?

Hello everyone, please tell me how to use telebot-calendar. Added a calendar when clicking on the button, but there is no response when clicking on the date. I would like to make it so that when the user clicks on the date, the contact is shared and a notification is sent to the person in telegrams. Can't figure out where the error is

import telebot
from telebot import types
from telebot_calendar import Calendar, CallbackData, RUSSIAN_LANGUAGE
import datetime

bot = telebot.TeleBot(token)
calendar = Calendar(language=RUSSIAN_LANGUAGE)
calendar_1 = CallbackData('calendar_1', 'action', 'year', 'month', 'day')
now = datetime.datetime.now()

@bot.message_handler(commands=['start'])
def start(message):
    keyboard = types.ReplyKeyboardMarkup(True)
    button1 = types.KeyboardButton('ЗАПИСЬ НА СЪЕМКУ')
    keyboard.add(button1)
    bot.send_message(message.chat.id, 'Добро пожаловать, ' + message.from_user.first_name + '!', reply_markup=keyboard)

@bot.message_handler(content_types=['text'])
def call(message):
    #высвечиваем календарь при нажатии на кнопку ЗАПИСЬ НА СЪЕМКУ
    if message.text == 'ЗАПИСЬ НА СЪЕМКУ':
        bot.send_message(message.chat.id, 'Выберите дату', reply_markup=calendar.create_calendar(
            name=calendar_1.prefix,
            year=now.year,
            month=now.month)
            )

@bot.message_handler(func=lambda call: call.data.startswith(calendar_1.prefix))
def callback_inline(call: types.CallbackQuery):
    name, action, year, month, day = call.data.split(calendar_1.sep)
    date = calendar.calendar_query_handler(bot=bot, call=call, name=name, action=action, year=year, month=month, day=day)

    if action == 'DAY':
        bot.send_message(chat_id=call.from_user.id, text=f'Вы выбрали {date.strftime("%d.%m.%Y")}', reply_markup=types.ReplyKeyboardRemove())

    elif action == 'CANCEL':
        bot.send_message(chat_id=call.from_user.id, text='Отмена', reply_markup=types.ReplyKeyboardRemove())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Krostelev, 2022-04-06
@dimonrzhev

@bot. callback_query_handler (func=lambda call: call.data.startswith(calendar_1.prefix))

D
Dmitry, 2022-04-06
@dimonrzhev

Guys, I found my own mistake, which I accidentally saw. I have a calendar handler
@bot.message_handler(func=lambda call: call.data.startswith(calendar_1.prefix))
and it should be @bot.callback_query_handler(func=lambda call: call.data.startswith
(calendar_1.prefix))
be careful!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question