-
Notifications
You must be signed in to change notification settings - Fork 0
/
reminderbotsource.hy
86 lines (69 loc) · 2.65 KB
/
reminderbotsource.hy
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
(import sys traceback threading time math)
(import syslogng)
(import timerdb telegramfetcher persiststate)
(import logging)
(logging.basicConfig)
(setv logger (logging.getLogger))
(.setLevel logger logging.DEBUG)
(defclass ReminderBotSource [syslogng.LogSource]
(defn init [self options]
(setv api-token (get options "api_token")
allowed-users (.get options "allowed_users"))
(setv self.wait (threading.Event)
self.exit False
self.fetcher (telegramfetcher.TelegramFetcher api-token :allowed-users allowed_users)
self.persist-state (persiststate.PersistState "persist-dir")
self.unhandled-timers (self.persist-state.load-all))
True)
(defn add-to-persist [self timer]
(assoc timer "creation-time" (math.ceil (time.time)))
(self.persist-state.save (get timer "id") timer))
(defn fetch-logs [self timer-db]
(while (not self.exit)
(try
(setv [instant-responses timers] (self.fetcher.fetch))
(for [timer timers]
(self.add-to-persist timer)
(self.add-notification timer-db timer))
(for [instant instant-responses]
(self.send-response timer-db instant))
(except [e Exception]
(traceback.print_exc)))
(self.wait.wait 1)))
(defn re-add-persist-timer [self timer-db timer]
(setv current-time (math.ceil (time.time))
creation-time (get timer "creation-time")
original-timeout (get timer "timeout")
timeout (- original-timeout (- current-time creation-time)))
(assoc timer "timeout" timeout)
(self.add-notification timer-db timer))
(defn run [self]
(setv self.timer-db (timerdb.TimerDB))
(for [timer self.unhandled-timers]
(self.re-add-persist-timer self.timer-db timer))
(setv self.unhandled-timers (list))
(.start (threading.Thread :target self.fetch-logs :args (, self.timer-db)))
(self.timer-db.start))
(defn request-exit [self]
(self.fetcher.request-exit)
(setv self.exit True)
(self.timer-db.stop)
(self.wait.set))
(defn create-logmessage [self message]
(setv text (get message "message")
chat_id (get message "CHAT_ID"))
(setv logmessage (syslogng.LogMessage text))
(assoc logmessage "CHAT_ID" (str chat_id))
logmessage)
(defn add-notification [self timer-db timer]
(timer-db.add-timer
(get timer "timeout")
(fn []
(self.post_message
(self.create-logmessage timer))
(self.persist-state.remove (get timer "id")))))
(defn send-response [self timerdb instant]
(timerdb.call-immediately
(fn []
(self.post_message
(self.create-logmessage instant))))))