aboutsummaryrefslogtreecommitdiff
path: root/telebot.scm
blob: 8d300131d7fb599b349242680e684f979a4d082c (plain)
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
(module telebot (get-me
                 get-updates
                 send-message
                 forward-message)
  (import chicken scheme)
  (use srfi-1)
  (use openssl)
  (use http-client)
  (use medea)

  (define api-base "https://api.telegram.org/bot")

  ;;; helper functions

  (define (get-query-url token method)
    (string-append api-base token "/" method))

  (define (clean-query-parameters parameters)
    (let ((cleaned-parameters (remove (lambda (p) (equal? #f (cdr p)))
                                      parameters)))
      (if (null-list? cleaned-parameters)
        #f
        cleaned-parameters)))

  ;;; plain API wrappers, returning deserialized JSON

  (define (get-me token)
    (with-input-from-request (get-query-url token "getMe")
                             #f
                             read-json))

  (define (get-updates token
                       #!key offset
                             limit
                             timeout)
    (with-input-from-request
      (get-query-url token "getUpdates")
      (clean-query-parameters
        (list (cons 'offset  offset)
              (cons 'limit   limit)
              (cons 'timeout timeout)))
      read-json))

  (define (send-message token
                        #!key chat-id
                              text
                              parse-mode
                              disable-web-page-preview
                              disable-notification
                              reply-to-message-id
                              reply-markup)
    (with-input-from-request
      (get-query-url token "sendMessage")
      (clean-query-parameters
        (list (cons 'chat_id                  chat-id)
              (cons 'text                     text)
              (cons 'parse_mode               parse-mode)
              (cons 'disable_web_page_preview disable-web-page-preview)
              (cons 'disable_notification     disable-notification)
              (cons 'reply_to_message_id      reply-to-message-id)
              (cons 'reply_markup             reply-markup)))
      read-json))

  (define (forward-message token
                           #!key chat-id
                                 from-chat-id
                                 message-id
                                 disable-notification)
    (with-input-from-request
      (get-query-url token "forwardMessage")
      (list (cons 'chat_id              chat-id)
            (cons 'from_chat_id         from-chat-id)
            (cons 'message_id           message-id)
            (cons 'disable_notification disable-notification))
      read-json))
)