-
Notifications
You must be signed in to change notification settings - Fork 11
/
example.py
74 lines (60 loc) · 2.28 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
import datetime
import telebot
from telebot_calendar import Calendar, CallbackData, RUSSIAN_LANGUAGE
from telebot.types import ReplyKeyboardRemove, CallbackQuery
API_TOKEN = "token"
logger = telebot.logger
telebot.logger.setLevel(logging.DEBUG)
bot = telebot.TeleBot(API_TOKEN)
# Creates a unique calendar
calendar = Calendar(language=RUSSIAN_LANGUAGE)
calendar_1_callback = CallbackData("calendar_1", "action", "year", "month", "day")
@bot.message_handler(commands=["start"])
def check_other_messages(message):
"""
Catches a message with the command "start" and sends the calendar
:param message:
:return:
"""
now = datetime.datetime.now() # Get the current date
bot.send_message(
message.chat.id,
"Selected date",
reply_markup=calendar.create_calendar(
name=calendar_1_callback.prefix,
year=now.year,
month=now.month, # Specify the NAME of your calendar
),
)
@bot.callback_query_handler(
func=lambda call: call.data.startswith(calendar_1_callback.prefix)
)
def callback_inline(call: CallbackQuery):
"""
Обработка inline callback запросов
:param call:
:return:
"""
# At this point, we are sure that this calendar is ours. So we cut the line by the separator of our calendar
name, action, year, month, day = call.data.split(calendar_1_callback.sep)
# Processing the calendar. Get either the date or None if the buttons are of a different type
date = calendar.calendar_query_handler(
bot=bot, call=call, name=name, action=action, year=year, month=month, day=day
)
# There are additional steps. Let's say if the date DAY is selected, you can execute your code. I sent a message.
if action == "DAY":
bot.send_message(
chat_id=call.from_user.id,
text=f"You have chosen {date.strftime('%d.%m.%Y')}",
reply_markup=ReplyKeyboardRemove(),
)
print(f"{calendar_1_callback}: Day: {date.strftime('%d.%m.%Y')}")
elif action == "CANCEL":
bot.send_message(
chat_id=call.from_user.id,
text="Cancellation",
reply_markup=ReplyKeyboardRemove(),
)
print(f"{calendar_1_callback}: Cancellation")
bot.polling(none_stop=True)