-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
84 lines (67 loc) · 3.51 KB
/
main.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
75
76
77
78
79
80
81
82
83
from datetime import datetime, timedelta
import telebot
# функция для поиска дат с учетом дня недели
def find_weekday_dates(start_date, end_date, target_weekday):
try:
start_date_converted = datetime.strptime(start_date, '%d.%m.%Y')
end_date_converted = datetime.strptime(end_date, '%d.%m.%Y')
#отладка
except ValueError:
print("Ошибка при конвертации даты")
print("start_date:", start_date)
print("end_date:", end_date)
return [] # Возвращаем пустой список, если даты введены некорректно
result_dates = []
current_date = start_date_converted
print("start_date_converted:", start_date_converted)
print("end_date_converted:", end_date_converted)
print("target_weekday:", target_weekday)
while current_date <= end_date_converted:
if current_date.weekday() == target_weekday:
result_dates.append(current_date.strftime('%d.%m.%Y'))
current_date += timedelta(days=1)
return result_dates
bot = telebot.TeleBot("Yor token")
# хранение данных
user_data = {}
@bot.message_handler(commands=['start'])
def start(message):
bot.send_message(message.chat.id, "Приветствую!"
"\nЯ могу вывести все определенные дни в заданном промежутке. "
"\nНапример, тебе нужны все субботы месяца, просто введи начальную дату и конечную, а затем номер дня!"
"\nДля начала работы набери команду /setup")
# Обработчик команды setup
@bot.message_handler(commands=['setup'])
def setup_message(message):
chat_id = message.chat.id
user_data[chat_id] = {}
bot.send_message(chat_id, "Введите start_date (в формате DD.MM.YYYY):")
@bot.message_handler(commands=['target_weekday'])
def target_weekday_command(message):
chat_id = message.chat.id
user_data[chat_id] = {}
bot.send_message(chat_id, "Введите номер дня недели (0 - Пн, 1 - Вт, ..., 6 - Вс):")
@bot.message_handler(func=lambda message: True)
def handle_message(message):
chat_id = message.chat.id
if chat_id in user_data:
user_info = user_data[chat_id]
if "start_date" not in user_info:
user_info["start_date"] = message.text
bot.send_message(chat_id, "Введите end_date (в формате DD.MM.YYYY):")
elif "end_date" not in user_info:
user_info["end_date"] = message.text
bot.send_message(chat_id, "Введите номер дня недели (0 - Пн, 1 - Вт, ..., 6 - Вс):")
elif "target_weekday" not in user_info:
target_weekday = message.text.strip()
if target_weekday in ("0", "1", "2", "3", "4", "5", "6"):
target_weekday = int(target_weekday)
start_date = user_info["start_date"]
end_date = user_info["end_date"]
result_dates = find_weekday_dates(start_date, end_date, target_weekday)
bot.send_message(chat_id, f"Найденные даты: {', '.join(result_dates)}")
user_data.pop(chat_id, None)
else:
bot.send_message(chat_id, "я даун")
# Запуск бота
bot.polling()