forked from bro-n-bro/bro-proposals-tracking-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifier.py
61 lines (52 loc) · 1.88 KB
/
notifier.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
from data.config import NOTIFIER_FREQUENCY, CHAT_ID, TG_BOT_API_TOKEN
from sql import get_all_rows, drop_rows, save_to_db, get_rows, drop_row_by_msg_id, get_outdated_props
from get_data import save_data
from utils import chose_next_notification, notify, get_time_left_s
import telegram
import time
import logging
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
def notifier(telegram_bot, _row):
if int(time.time()) >= _row[5]:
time_left_s = get_time_left_s(_row)
msg = notify(_row, time_left_s)
t_msg = telegram_bot.send_message(CHAT_ID, msg, parse_mode="markdown")
record = list(_row[:5])
record.extend([
chose_next_notification(time_left_s),
t_msg.message_id
])
save_to_db(record)
records = get_rows(_row[0], _row[1])
message_to_delete = min(x[6] for x in records)
try:
telegram_bot.delete_message(chat_id=CHAT_ID, message_id=message_to_delete)
except Exception as e:
print(e)
pass
drop_row_by_msg_id(_row[0], _row[1], message_to_delete)
else:
pass
def processor(telegram_bot):
outdated_props = get_outdated_props()
if outdated_props and outdated_props != []:
msgs_to_delete = [x[6] for x in outdated_props]
for msg in msgs_to_delete:
try:
telegram_bot.delete_message(chat_id=CHAT_ID, message_id=msg)
except Exception as e:
print(e)
pass
drop_rows()
save_data()
rows = get_all_rows()
for row in rows:
notifier(telegram_bot, row)
logging.info("Records was checked")
if __name__ == "__main__":
telegram_bot = telegram.Bot(token=TG_BOT_API_TOKEN)
save_data()
rows = get_all_rows()
while True:
processor(telegram_bot)
time.sleep(NOTIFIER_FREQUENCY)