-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminder_recovery.py
447 lines (381 loc) · 13.9 KB
/
reminder_recovery.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
from base_handler import *
import json
import re
import parsedatetime
import datetime
import logging
from utils import get_affirmation
PATTERN = re.compile('^remind(?:\s+me)?\s+(.+?)\s*(to|:|that|about)\s+(.+?)\s*$', flags=re.I)
params = {}
key = 'rem'
name = "Reminders"
calendar = parsedatetime.Calendar()
UNITS = [
'min',
'h',
'day',
'week',
'month',
'year',
]
REMOVE_BUTTONS = 'rb'
REMOVE_REMINDER = 'rm'
REMOVE_PERIODIC_REMINDER = 'rmp'
SNOOZE_REMINDER_HOUR = 'snh'
SNOOZE_REMINDER_6_HOURS = 'sn6h'
SNOOZE_REMINDER_DAY = 'sn'
SNOOZE_REMINDER_WEEK = 'snw'
SNOOZE_REMINDERS = {
SNOOZE_REMINDER_HOUR: {
'button': "+1h",
'amount': "1 hour",
'delta': datetime.timedelta(hours=1),
},
SNOOZE_REMINDER_6_HOURS: {
'button': "+6h",
'amount': "6 hours",
'delta': datetime.timedelta(hours=6),
},
SNOOZE_REMINDER_DAY: {
'button': "+1d",
'amount': "1 day",
'delta': datetime.timedelta(days=4),
},
SNOOZE_REMINDER_WEEK: {
'button': "+1w",
'amount': "1 week",
'delta': datetime.timedelta(weeks=1),
},
}
DELETE_MESSAGE = 'dm'
def help(permission):
return {
'summary': "Makes sure you never forget things again.",
'examples': [
"Remind me <time> to <thing>",
"Remind me tomorrow to do laundry",
"Remind me in three days to take out trash",
"Remind me on July 15 at 13:40 to call mom",
"Remind me each Wednesday to water the plants",
"Remind me every year on August 18 that it's my birthday",
],
}
def matches_message(message):
return PATTERN.match(message) is not None
def handle(message, **kwargs):
time = datetime.datetime.fromisoformat(kwargs['time'])
actor_id = kwargs['actor_id']
match = PATTERN.match(message)
if not match:
return "This wasn't supposed to happen."
groups = match.groups()
time_string = groups[0]
separator_word = groups[1]
if separator_word == 'about':
separator_word = ''
subject = groups[2]
if 'every' in time_string or 'each' in time_string:
reminder = create_periodic_reminder(time_string, subject, separator_word, actor_id, time)
if not isinstance(reminder, dict):
return reminder
msg = "I set up your reminder for every {}{}. The first one will happen on ".format(
"{} ".format(reminder['interval']) if reminder['interval'] > 1 else "",
unit_to_readable(reminder['unit'], reminder['interval'] == 1),
) + '{}'
else:
reminder = create_onetime_reminder(time_string, subject, separator_word, actor_id, time)
msg = "I set up your reminder for {}"
return reminder
def handle_button(data, **kwargs):
db = kwargs['db']
parts = data.split(':')
reminder_id = int(parts[0])
method = parts[1]
table = db['reminders']
reminder = table.find_one(id=reminder_id)
answer = {}
if method == REMOVE_REMINDER:
table.delete(id=reminder_id)
answer = {
'answer': "Reminder deleted",
'delete': True,
}
if method == REMOVE_BUTTONS:
answer = {
'answer': get_affirmation(),
'message': reminder_to_string(reminder),
}
if method == DELETE_MESSAGE:
answer = {
'answer': "You will be reminded.",
'delete': True
}
if method == REMOVE_PERIODIC_REMINDER:
table.delete(id=reminder_id)
answer = {
'answer': "Reminder deleted",
'message': "{}\n\nThis periodic reminder was deleted.".format(reminder_to_string(reminder)),
}
if method in SNOOZE_REMINDERS:
snooze = SNOOZE_REMINDERS[method]
now = datetime.datetime.now()
amount = snooze['amount']
if reminder['periodic']:
reminder = copy_periodic_to_onetime_and_rewind(reminder)
while reminder['next'] < now:
reminder['next'] = reminder['next'] + snooze['delta']
reminder['active'] = True
if 'id' in reminder:
table.update(reminder, ['id'])
else:
table.insert(reminder)
answer = {
'answer': "Reminder snoozed for {}".format(amount),
'delete': True,
}
return answer
def create_periodic_reminder(time_string, subject, separator_word, actor_id, time):
now = time
split_every = time_string.split("every")
if len(split_every) <= 1:
split_every = time_string.split("each")
part_before = split_every[0]
part_after = split_every[1]
time_string_parts = part_after.split()
interval = 0
unit = ""
rest = ""
for i, part in enumerate(time_string_parts):
if unit:
break
if part.isnumeric():
interval = int(part)
if part == 'other':
interval = 2
for potential_unit in UNITS:
if part.lower().startswith(potential_unit):
unit = potential_unit
rest = " ".join(time_string_parts[i+1:])
if not unit:
unit = 'week'
rest = " ".join(time_string_parts[1:])
if not interval:
interval = 1
contains_date = None
if rest:
contains_date = rest
elif part_before:
contains_date = part_before
if contains_date:
date_time, parsed = calendar.parseDT(contains_date, sourceTime=time)
if parsed == 0:
if unit == 'month':
day = int(re.sub('\D', '', contains_date))
if 0 < day < 29: # sorry we can't handle february otherwise.
date_time = datetime.datetime(year=time.year, month=time.month, day=day, hour=7, minute=0)
else:
return "Oh no, I couldn't understand what you mean by \"{}\". Note that you can only use " \
"dates (days of month) between 1 and 28, unfortunately.".format(contains_date)
else:
return "Oh no, I couldn't understand what you mean by \"{}\".".format(contains_date)
if parsed == 1:
date_time = datetime.datetime.combine(date_time.date(), datetime.time(hour=7, minute=0))
if parsed == 2:
# time without date - only makes sense if unit is day or hour:
if unit != 'day' and unit != 'h':
return "Oh no, I couldn't understand what you mean by \"{}\". Note that you can only set " \
"a time (without a weekday or date) if your reminder is every X days or hours".format(contains_date)
else:
if unit == 'min':
date_time = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=now.hour,
minute=now.minute)
elif unit == 'h':
date_time = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=now.hour)
elif unit == 'day' or unit == 'week':
date_time = datetime.datetime(year=now.year, month=now.month, day=now.day, hour=7)
elif unit == 'month':
date_time = datetime.datetime(year=now.year, month=now.month, day=1, hour=7)
elif unit == 'year':
date_time = datetime.datetime(year=now.year + 1, month=1, day=1, hour=7)
# phew.
reminder = {
'next': date_time,
'subject': subject,
'active': True,
'periodic': True,
'interval': interval,
'unit': unit,
'separator': separator_word,
'actor': actor_id,
}
present = datetime.datetime.now()
while reminder['next'] < present:
advance_periodic_reminder(reminder)
return reminder
def create_onetime_reminder(time_string, subject, separator_word, actor_id, time):
now = time
date_time, parsed = calendar.parseDT(time_string, sourceTime=time)
if parsed == 0:
return "Sorry, I don't understand what you mean by \"{}\".".format(time_string)
if parsed == 1:
# date without time - assume morning
date_time = datetime.datetime.combine(date_time.date(), datetime.time(hour=7, minute=0))
if parsed == 2:
# time without date - assume next time that time comes up
if now > date_time:
date_time += datetime.timedelta(days=1)
reminder = {
'next': date_time,
'subject': subject,
'active': True,
'periodic': False,
'separator': separator_word,
'actor': actor_id,
}
return reminder
def copy_periodic_to_onetime_and_rewind(periodic_reminder):
if not periodic_reminder['periodic']:
return
unit = periodic_reminder['unit']
interval = periodic_reminder['interval']
onetime_reminder = {
'next': periodic_reminder['next'],
'subject': periodic_reminder['subject'],
'active': periodic_reminder['active'],
'periodic': False,
'separator': periodic_reminder['separator'],
'actor': periodic_reminder['actor'],
}
if unit == 'min':
onetime_reminder['next'] -= datetime.timedelta(minutes=interval)
if unit == 'h':
onetime_reminder['next'] -= datetime.timedelta(hours=interval)
if unit == 'day':
onetime_reminder['next'] -= datetime.timedelta(days=interval)
if unit == 'week':
onetime_reminder['next'] -= datetime.timedelta(days=7*interval)
if unit == 'month':
onetime_reminder['next'] = advance_by_a_month(onetime_reminder['next'], -interval)
if unit == 'year':
dt = onetime_reminder['next']
onetime_reminder['next'] = datetime.datetime(dt.year - interval, dt.month, dt.day, dt.hour, dt.minute)
return onetime_reminder
def advance_periodic_reminder(reminder):
if not reminder['periodic']:
return
unit = reminder['unit']
interval = reminder['interval']
if unit == 'min':
reminder['next'] += datetime.timedelta(minutes=interval)
if unit == 'h':
reminder['next'] += datetime.timedelta(hours=interval)
if unit == 'day':
reminder['next'] += datetime.timedelta(days=interval)
if unit == 'week':
reminder['next'] += datetime.timedelta(days=7*interval)
if unit == 'month':
reminder['next'] = advance_by_a_month(reminder['next'], interval)
if unit == 'year':
dt = reminder['next']
reminder['next'] = datetime.datetime(dt.year + interval, dt.month, dt.day, dt.hour, dt.minute)
if params['debug']:
logger.info("Periodic reminder advanced by {} {}, new date is {}".format(
interval,
unit,
reminder['next']
))
def advance_by_a_month(date_time, months):
new_month = date_time.month + months
add_year = 0
while new_month > 12:
new_month -= 12
add_year += 1
while new_month < 1:
new_month += 12
add_year -= 1
day = date_time.day
if day > 30 and new_month not in [1, 3, 5, 7, 8, 10, 12]:
day = 30
if day > 28 and new_month == 2:
day = 28 # fuck leap years
return datetime.datetime(
date_time.year + add_year,
new_month,
day,
date_time.hour,
date_time.minute
)
def unit_to_readable(unit, singular=False):
if unit == 'h':
return 'hour' if singular else 'hours'
if unit == 'min':
return 'minute' if singular else 'minutes'
return unit if singular else unit + 's'
def reminder_to_string(reminder):
return "Remember{} {}".format(
' to' if 'separator' not in reminder or reminder['separator'] is None else
reminder['separator'] if len(reminder['separator']) <= 1 else
" {}".format(reminder['separator']),
reminder['subject'])
def run_periodically(db):
debug = params['debug']
table = db['reminders']
send = params['sendmsg']
if debug:
logger.info("Querying reminders...")
now = datetime.datetime.now()
reminders = db.query('SELECT * FROM reminders WHERE active IS TRUE AND next < :now', now=now)
count = 0
for reminder in reminders:
count += 1
if debug:
logger.info("Sending reminder {}".format(count))
# this is so stupid I can't even
# dataset returns dates as string but only accepts them as datetime
reminder['next'] = datetime.datetime.strptime(reminder['next'], '%Y-%m-%d %H:%M:%S.%f')
msg = reminder_to_string(reminder)
buttons = [
[{
'text': 'Got it!',
'data': '{}:{}'.format(reminder['id'], REMOVE_BUTTONS),
}],
]
if reminder['periodic']:
advance_periodic_reminder(reminder)
buttons.append(
[{
'text': "Remove this reminder",
'data': '{}:{}'.format(reminder['id'], REMOVE_PERIODIC_REMINDER),
}],
)
else:
reminder['active'] = False
buttons.append([
{
'text': snooze['button'],
'data': '{}:{}'.format(reminder['id'], snooze_type),
} for snooze_type, snooze in SNOOZE_REMINDERS.items()
])
send({
'message': msg,
'buttons': buttons,
}, key=key, recipient_id=reminder['actor'] if 'actor' in reminder else None)
if debug:
logger.info("Updating reminder {}".format(count))
table.update(reminder, ['id'])
if debug:
logger.info("Finished reminder {}".format(count))
if debug:
logger.info("Sent out {} reminders".format(count))
with open('result.json') as file:
cont = json.load(file)
cenow = datetime.datetime.now()
params['debug'] = False
for msg in cont['messages']:
if 'text' in msg and isinstance(msg['text'], str):
if matches_message(msg['text']):
reminder = handle(msg['text'], actor_id=msg['from_id'], time=msg['date'])
if isinstance(reminder, dict):
if reminder['next'] > cenow:
print(msg['date'])
print(msg['text'])