-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (91 loc) · 3.6 KB
/
app.py
File metadata and controls
137 lines (91 loc) · 3.6 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import datetime as dt
import logging
import os
import dotenv
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
from telegram.ext import Updater
from exact_time import extractor
dotenv.load_dotenv(dotenv.find_dotenv())
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
updater = Updater(token=os.environ["TOKEN"])
dispatcher = updater.dispatcher
unrecognized_phrases = set()
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"' % (update, error))
def start(bot, update):
bot.send_message(
chat_id=update.message.chat_id,
text="""
Привет! Я напоминалка! Напиши мне, о чём тебе стоит напомнить простой строкой, например,
"сходить в магазин завтра в 10:30"
""",
)
def is_today(moment):
return dt.datetime.today().date() == moment.date()
def is_tomorrow(moment):
return dt.datetime.today().date() + dt.timedelta(days=1) == moment.date()
def is_day_after_tomorrow(moment):
return dt.datetime.today().date() + dt.timedelta(days=2) == moment.date()
def is_on_this_week(moment):
start = moment - dt.timedelta(days=moment.weekday())
return start <= moment <= start + dt.timedelta(days=7)
def human_format_dayofweek(moment):
return moment.strftime("%A")
def human_format_date(moment):
return moment.strftime("%d %B %Y")
def human_format(moment):
if moment.minute:
time = f"в {moment.hour}:{moment.minute}"
else:
time = f"в {moment.hour}"
if is_today(moment):
date = "сегодня"
elif is_tomorrow(moment):
date = "завтра"
elif is_day_after_tomorrow(moment):
date = f"послезавтра ({human_format_dayofweek(moment)})"
elif is_on_this_week(moment):
date = f"в эту {human_format_dayofweek(moment)}"
else:
date = human_format_date(moment)
return f"{date} {time}"
def print_exact_time(bot, update):
global unrecognized_phrases
extract = extractor(update.message.text)
if extract is None:
unrecognized_phrases.add(update.message.text)
text = "Я ничего не поняла."
else:
when = human_format(extract.time)
text = f"""
"{extract.task}" — напомню {when} ({extract.time.strftime('%Y-%m-%d %H:%M')})
"""
bot.send_message(chat_id=update.message.chat_id, text=text)
def print_unrecognized_phrases(bot, update):
global unrecognized_phrases
text = ", ".join(unrecognized_phrases)
bot.send_message(chat_id=update.message.chat_id, text=text)
def print_timezone(bot, update):
tz = dt.datetime.now(dt.timezone.utc).astimezone().tzname()
bot.send_message(chat_id=update.message.chat_id, text=tz)
def unknown(bot, update):
bot.send_message(
chat_id=update.message.chat_id, text="Sorry, I didn't understand that command."
)
exact_time_handler = MessageHandler(Filters.text, print_exact_time)
dispatcher.add_handler(exact_time_handler)
unrecognized_handler = CommandHandler("print", print_unrecognized_phrases)
dispatcher.add_handler(unrecognized_handler)
tz_handler = CommandHandler("timezone", print_timezone)
dispatcher.add_handler(tz_handler)
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)
unknown_handler = MessageHandler(Filters.command, unknown)
dispatcher.add_handler(unknown_handler)
dispatcher.add_error_handler(error)
updater.start_polling()
updater.idle()