diff --git a/eval-bot/main.rkt b/eval-bot/main.rkt index 24718dc..e87c085 100644 --- a/eval-bot/main.rkt +++ b/eval-bot/main.rkt @@ -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)])) diff --git a/telebot/command.rkt b/telebot/command.rkt index d508c2f..6cc4156 100644 --- a/telebot/command.rkt +++ b/telebot/command.rkt @@ -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)) diff --git a/telebot/main.rkt b/telebot/main.rkt index 34fadad..ed8cdf2 100644 --- a/telebot/main.rkt +++ b/telebot/main.rkt @@ -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 : diff --git a/telebot/private/bot.rkt b/telebot/private/bot.rkt index 60ae7c9..32bcd98 100644 --- a/telebot/private/bot.rkt +++ b/telebot/private/bot.rkt @@ -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]))