-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminder_utils.py
57 lines (39 loc) · 1.26 KB
/
reminder_utils.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
import sqlite3
def add_to_db(user_id, date_time, reminder_text) -> None:
"""Adds reminder from user to DB"""
db = sqlite3.connect('myassistant.db')
c = db.cursor()
query = f"""
INSERT INTO reminders
VALUES
({user_id}, datetime('{date_time}'), "{reminder_text}");
"""
c.execute(query)
db.commit()
db.close()
def get_reminders(bot):
"""Check if there is someone to remind and remind if there is"""
db = sqlite3.connect('myassistant.db')
c = db.cursor()
query_select = """
SELECT user_id, reminder_text
FROM reminders
WHERE DATE(reminder_date) <= DATE('now')
AND strftime('%H %M', datetime(reminder_date)) <= strftime('%H %M', datetime('now', 'localtime'));
"""
c.execute(query_select)
reminders = c.fetchall()
query_delete = """
DELETE FROM reminders
WHERE DATE(reminder_date) <= DATE('now')
AND strftime('%H %M', datetime(reminder_date)) <= strftime('%H %M', datetime('now', 'localtime'));
"""
c.execute(query_delete)
db.commit()
db.close()
remind_users(reminders, bot)
def remind_users(reminders, bot):
"""Send reminder messages to users"""
for reminder in reminders:
user_id, reminder_text = reminder
bot.send_message(user_id, reminder_text)