-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteleracket.rkt
71 lines (60 loc) · 2.51 KB
/
teleracket.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#lang racket
(require net/http-client
json)
(provide
api-get-updates
api-get-botinfo
api-send-message
api-forward-message)
; see https://core.telegram.org/bots/api for information on how to get a token
(define bot-token "")
(define api-endpoint-host "api.telegram.org")
(define api-endpoint-uri (string-append "/bot" bot-token "/"))
(define (make-request-options options)
(jsexpr->string options))
(define (api-request method action (options '()))
(define-values (status header json-encoded-response)
(http-sendrecv
api-endpoint-host
(string-append api-endpoint-uri action)
#:ssl? #t
#:data (make-request-options options)
#:headers (list "Content-Type: application/json")))
(define response (read-json json-encoded-response))
(if (hash-ref response 'ok)
(cons 'ok (hash-ref response 'result))
(cons 'err ((hash-ref response 'description) (hash-ref response 'error_code)))))
(define (api-get-request action (options '()))
(api-request "GET" action options))
(define (api-post-request action (options '()))
(api-request "POST" action options))
(define (api-get-updates (offset 0) (limit 100) (timeout 0) (allowed-updates '()))
(let* ((options (hash 'offset offset
'limit limit
'timeout timeout
'allowed-updates allowed-updates))
(response (api-get-request "getUpdates" options)))
(values
(car response)
(cdr response)
(make-get-updates-with-offset
(cond
((empty? (cdr response)) offset)
((eq? 'ok (car response)) (+ 1 (hash-ref (last (cdr response)) 'update_id)))
((eq? 'err (car response)) offset))))))
(define (api-get-botinfo)
(api-get-request "getMe"))
(define (make-get-updates-with-offset offset (default-limit 100) (default-timeout 0) (default-allowed-updates '()))
(λ ((limit default-limit) (timeout default-timeout) (allowed-updates default-allowed-updates))
(api-get-updates offset limit timeout allowed-updates)))
(define (api-send-message chat-id text)
(let* ((message-hash (hash 'chat_id chat-id
'text text))
(response (api-post-request "sendMessage" message-hash)))
response))
(define (api-forward-message to-chat-id from-chat-id message-id)
(let* ((message-hash (hash 'chat_id to-chat-id
'from_chat_id from-chat-id
'message_id message-id))
(response (api-post-request "forwardMessage" message-hash)))
response))