Skip to content

Commit

Permalink
telebot: filter commands with bot's username
Browse files Browse the repository at this point in the history
  • Loading branch information
yfzhe committed Feb 4, 2024
1 parent 35d27ed commit 15db818
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
17 changes: 6 additions & 11 deletions eval-bot/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@
(equal? (ref (message : message) .chat .type) "private"))
(cond
[(not text) (void)]
[(parse-command text)
[(parse-command text (bot-username bot))
=>
(match-lambda
[(list "start" _)
(start message)]
[(list "help" _)
(help message)]
[(list "eval" code)
(eval message (string-trim code) 'racket)]
[(list "eval_chez" code)
(eval message (string-trim code) 'chez)]
[(list _ _)
(bad-request message)])]
[(list "start" _) (start message)]
[(list "help" _) (help message)]
[(list "eval" code) (eval message code 'racket)]
[(list "eval_chez" code) (eval message code 'chez)]
[(list _ _) (bad-request message)])]
[in-private-chat?
(eval message text 'racket)]
[else (void)]))
Expand Down
29 changes: 15 additions & 14 deletions telebot/command.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@

(provide parse-command)

;; TODO:
;; - compare bot's username
;; - how to process command arguments?
(define rx #px"^/([a-z0-9_]+)@?([a-zA-Z0-9_]+)?\\s?(.*)")

(define rx #px"^/([a-zA-Z_]+)(@[a-zA-Z_]+)?\\s?(.*)")

(define (parse-command text)
(define (parse-command text bot-username)
(match (regexp-match rx text)
[(list _ command username args)
[(list _ command (or #f (== bot-username)) args)
(list command args)]
[#f #f]))
[_ #f]))

(module+ test
(require rackunit)

(check-equal? (parse-command "hello world") #f)
(check-equal? (parse-command "/start")
(check-equal? (parse-command "hello world" "bot")
#f)
(check-equal? (parse-command "/start" "bot")
'("start" ""))
(check-equal? (parse-command "/help@racket_eval_bot")
(check-equal? (parse-command "/help@bot" "bot")
'("help" ""))
(check-equal? (parse-command "/eval (+ 1 2)")
(check-equal? (parse-command "/help@cot" "bot")
#f)
(check-equal? (parse-command "/eval (+ 1 2)" "bot")
'("eval" "(+ 1 2)"))
(check-equal? (parse-command "/eval_chez@racket_eval_bot\n(+ 1 2)")
'("eval_chez" "(+ 1 2)")))
(check-equal? (parse-command "/eval_chez@bot\n(+ 1 2)" "bot")
'("eval_chez" "(+ 1 2)"))
(check-equal? (parse-command "/plus12@answer42" "answer52")
#f))
1 change: 1 addition & 0 deletions telebot/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

(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 Down
2 changes: 1 addition & 1 deletion telebot/private/bot.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"error.rkt")

(provide bot? make-bot
set-bot-username!
bot-username set-bot-username!
bot-post)

(struct bot (token [username #:mutable]))
Expand Down

0 comments on commit 15db818

Please sign in to comment.