-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminderbot.rkt
55 lines (47 loc) · 1.94 KB
/
reminderbot.rkt
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
#lang braidbot/insta
(require racket/match
racket/string
tasks
braidbot/util
braidbot/uuid)
(define bot-id (or (getenv "BOT_ID") "5a47c63e-8d23-4bba-92ef-53c3c0f7e398"))
(define bot-token (or (getenv "BOT_TOKEN") "4S3EzYM-exfyBibpYxEggiX7ahcWrRh3AIKoKbms"))
(define braid-url (or (getenv "BRAID_URL") "http://localhost:5557"))
(listen-port 8899)
(define reminder-re #rx"^/reminderbot (.*) in ([0-9]+) minutes?$")
(on-init (λ () (println "Launching reminderbot")))
(define (parse-reminder str)
(if-let [matches (regexp-match reminder-re str)]
(let ([secs (-> matches caddr string->number (* 60))]
[txt (cadr matches)])
(list secs txt))
#f))
(define (act-on-message msg)
(->> (if-let [rem (parse-reminder (hash-ref msg '#:content))]
(let ([secs (car rem)]
[txt (cadr rem)])
(thread
(λ ()
(with-task-server
(delayed-task
secs
(-> (make-immutable-hash)
(hash-set '#:id (make-uuid))
(hash-set '#:thread-id (make-uuid))
(hash-set '#:group-id (hash-ref msg '#:group-id))
(hash-set '#:content txt)
(hash-set '#:mentioned-user-ids
(list (hash-ref msg '#:user-id)))
(hash-set '#:mentioned-tag-ids '())
(send-message #:bot-id bot-id #:bot-token bot-token
#:braid-url braid-url)))
(run-tasks))))
(-> (list "Okay, I'll remind you in" (number->string secs) "seconds")
string-join))
(-> (list "Sorry, I don't know what that means."
"Try something like "
"`/reminderbot check laundry in 5 minutes`.")
(string-join "\n")))
(reply-to msg
#:bot-id bot-id #:bot-token bot-token
#:braid-url braid-url)))