forked from 2-3/teleracket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.rkt
More file actions
47 lines (40 loc) · 1.54 KB
/
example.rkt
File metadata and controls
47 lines (40 loc) · 1.54 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
#lang racket
(require "teleracket.rkt")
(struct ~matcher (id
match-proc
handle-proc))
(define-syntax <-
(syntax-rules ()
[(<- hs a) (hash-ref hs a)]
[(<- hs a b) (hash-ref (hash-ref hs a) b)]
[(<- hs a b c ...) (<- (hash-ref (hash-ref hs a) b) c ...)]))
(define bot-name (<- (cdr (api-get-botinfo)) 'username))
(define (run-bot proc matchers)
(define-values (status result get-updates-proc) (proc))
(case status
('err
(begin
(printf "ERROR: [~a] ~a" (second result) (first result))
(sleep 10)))
('ok
(map (λ (update)
(let
((m (<- update 'message)))
(printf "<~a> (~a) ~a ~%" (<- m 'from 'username) (<- m 'chat 'id) (<- m 'text))
(handle-message (hash-set m 'text (sanitize-command (<- m 'text))) matchers)))
result)))
(run-bot get-updates-proc matchers))
(define (sanitize-command t)
(cond
[(regexp-match? (regexp (string-append "/(.*?)@" bot-name)) t) (cadr (regexp-match #rx"/(.*?)@" t))]
[(char=? #\/ (string-ref t 0)) (substring t 1)]
[else t]))
(define (handle-message m matchers)
(map
(λ (matcher)
(when ((~matcher-match-proc matcher) m) ((~matcher-handle-proc matcher) m)))
matchers))
(run-bot (λ () (api-get-updates)) (list
(~matcher "coffee"
(λ (m) (string=? (<- m 'text) "coffee"))
(λ (m) (api-send-message (<- m 'chat 'id) "☕️")))))