-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rkt
More file actions
82 lines (63 loc) · 2.28 KB
/
main.rkt
File metadata and controls
82 lines (63 loc) · 2.28 KB
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
#lang braidbot/insta
(require db
braidbot/util
braidbot/uuid)
(define bot-id (or (getenv "BOT_ID") "5a565c47-3ab2-4366-bf7b-e98054893043"))
(define bot-token (or (getenv "BOT_TOKEN") "iNtVHA6wIe1_7fioAJ2IBqn_AmJcg6ts2FGDW3VM"))
(define braid-api-url (or (getenv "BRAID_API_URL") "http://localhost:5557"))
(define braid-frontend-url (or (getenv "BRAID_FRONTEND_URL") "http://localhost:5555"))
(listen-port 9191)
(on-init (λ () (println "Bot starting")))
;;; Helper functions
(define (thread-link msg)
(string-join
(list braid-frontend-url
"/groups/"
(uuid->string (hash-ref msg '#:group-id))
"/thread/"
(uuid->string (hash-ref msg '#:thread-id)))
""))
(define (new-message)
(make-immutable-hasheq
(list (cons '#:id (make-uuid))
(cons '#:content "")
(cons '#:thread-id (make-uuid))
(cons '#:mentioned-user-ids '())
(cons '#:mentioned-tag-ids '()))))
;;; Saving & retrieving bookmarks
(define user->saved (make-hash))
(define (save-bookmark user-id to-save)
(~>> (hash-ref! user->saved user-id '())
(cons to-save)
(hash-set! user->saved user-id)))
(define (bookmarks-for user-id)
(hash-ref! user->saved user-id '()))
;;; Parsing messages
(define msg-handlers
(list
(cons #rx"^/bookmark add (.*)$"
(λ (msg matches)
(~>> (if (string=? (car matches) "this")
(thread-link msg)
(car matches))
(save-bookmark (hash-ref msg '#:user-id)))))
(cons #rx"^/bookmark list$"
(λ (msg _)
(let* ([user-id (hash-ref msg '#:user-id)]
[saved (bookmarks-for user-id)])
(~> (new-message)
(hash-set '#:mentioned-user-ids (list user-id))
(hash-set '#:content (string-join saved "\n"))
(send-message
#:bot-id bot-id
#:bot-token bot-token
#:braid-url braid-api-url)))))))
(define (handle-message msg)
(let ([content (hash-ref msg '#:content)])
(for/first ([re-fn msg-handlers]
#:when (regexp-match (car re-fn) content))
((cdr re-fn) msg (cdr (regexp-match (car re-fn) content))))))
;;; Main
(define (act-on-message msg)
(println msg)
(handle-message msg))