Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
yfzhe committed Feb 24, 2024
1 parent a4393cb commit 3be6985
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions telebot/main.rkt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#lang racket/base
(require racket/contract/base
racket/match
web-server/servlet
web-server/servlet-env
json
"private/bot.rkt"
"private/error.rkt"
"private/schema.rkt"
"api.rkt")
"api.rkt"
"command.rkt")

(provide bot?
(contract-out (make-bot (-> string? bot?)))
bot-username ;; TODO: don't export this
exn:fail:bot? exn:fail:bot:api?
(all-from-out "api.rkt")
ref :
Expand All @@ -27,16 +28,37 @@
(define me (bot-get-me bot))
(set-bot-username! bot (ref (me : user) .username #f)))

(define (make-bot-update-handler bot commands handler)
(lambda (update)
(cond
[(cond
[(ref (update : update) .message .text #f)
=> (lambda (text) (parse-command text (bot-username bot)))]
[else #f])
=> dispatch-command]
[else (handler update)])))

(struct command (name handle))

(define (dispatch-command bot commands cmd-call)
(match-define (list name arg) cmd-call)
(cond
[(findf (lambda (cmd) (equal? (command-name cmd) name)) commands)
=> (lambda (cmd) ((command-handle cmd) bot arg))]
[else
(bot-send-message bot
#:chat-id (ref (message : message) .chat .id)
#:parse-mode "HTML"
#:text "Not supported command")]))

(define (bot-start/poll bot handle-update)
(bot-init! bot)
(let loop ([offset 0] [updates '()])
(cond
[(null? updates)
(loop offset (bot-get-updates bot #:offset offset))]
[else
(define update (car updates))
(define resp (handle-update update))
(bot-send-message bot resp)
(handle-update (car updates))
(loop (add1 (ref (update : update) .id)) (cdr updates))])))

(define (bot-start/webhook bot handle-update webhook-base port)
Expand Down

0 comments on commit 3be6985

Please sign in to comment.