-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rkt
58 lines (50 loc) · 2.16 KB
/
server.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
56
57
58
#lang racket
;;; Slack todo bot
(require (planet dmac/spin))
(require db
json
net/url)
(require "config.rkt"
"db.rkt"
"slack-trigger.rkt"
"todo.rkt")
(post "/add"
(lambda (req)
(match (add-todo (slack-trigger-from req))
[(simple-result _) "Ok, я записал"]
[_ "Что-то пошло не так :("])))
(post "/complete"
(lambda (req)
(match (complete-todo (slack-trigger-from req))
[(simple-result _) "Окей, как скажешь"]
[_ "Что-то пошло не так :("])))
(define (rich-slack-message todos)
(define (todo->hash todo)
(make-hash (list (cons 'title (todo-author todo))
(cons 'value (string-append (~a (todo-id todo))
": "
(let ([text (todo-text todo)])
(if (> (string-length text) 512)
(substring text 0 512)
text)))))))
(make-hash (list (cons 'attachments
(list
(make-hash
(list (cons 'pretext "Вы зачем-то хотели сделать это, жалкие людишки:")
(cons 'color "#D00000")
(cons 'fields
(map (lambda (todo) (todo->hash todo))
todos)))))))))
(post "/broadcast"
(lambda (req)
(let ([todos (get-uncompleted-todos)])
(define json-port (open-output-string))
(define hook (write-json (rich-slack-message todos) json-port))
(http-sendrecv/url (string->url hook-url)
#:method "POST"
#:headers (list "Content-Type: application/json")
#:data (get-output-string json-port)))
"ok"))
(init-database!)
(writeln "Starting bot...")
(run #:listen-ip #f)