aboutsummaryrefslogtreecommitdiff
path: root/example/guess.scm
blob: fd200faf8a5639d21a55bec1ae1cd53e5b80be01 (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
(require-extension telebot
                   (prefix telebot telebot:))

(define (resolve-query query tree)
  (fold (lambda (x y) (alist-ref x y))
        tree
        query))

(define (make-sender token chat_id)
  (lambda (text)
    (print chat_id " <- \"" text "\"")
    (telebot:sendMessage token
                         chat_id: chat_id
                         text:    text)))

(define (make-conversation token chat_id)
  (let* ((chat_id chat_id)
         (send    (make-sender token chat_id))
         (answer  (random 100)))
    (send "Hi there! I just generated a random number for you to guess!")
    (lambda (update)
      (let* ((text  (resolve-query '(message text) update))
             (guess (string->number text)))
        (print chat_id " -> \"" text "\"")
        (if (number? guess)
          (cond ((= guess answer)
                 (begin (send "Correct! Feel free to guess the next number.")
                        (set! answer (random 100))))
                ((< guess answer) (send "Too small. Try again."))
                ((> guess answer) (send "Too large. Try again.")))
          (send "This is not a number - please provide your guess in base 10."))))))

(let* ((token    (car (command-line-arguments)))
       (converse (telebot:make-conversation-manager token
                                                    make-conversation)))
  (randomize)
  (telebot:poll-updates token converse))