diff options
author | Ben Sima <ben@bsima.me> | 2021-04-02 21:39:47 -0400 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2021-04-02 21:39:47 -0400 |
commit | e3a813d72449e738bd95323511ad0696719bf396 (patch) | |
tree | dee2ff6b3be3037559b703bbf15a1769c00a88a6 /Biz/Bot.scm | |
parent | 574da8a4d081d19fd7b8447dc616d43786ef5f51 (diff) |
Init bizbot
Diffstat (limited to 'Biz/Bot.scm')
-rw-r--r-- | Biz/Bot.scm | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/Biz/Bot.scm b/Biz/Bot.scm index 7ed413a..dec654c 100644 --- a/Biz/Bot.scm +++ b/Biz/Bot.scm @@ -1,4 +1,55 @@ +;; Usage with ii: +;; +;; tail -f \#biz/out | guile -L $BIZ_ROOT -s Biz/Bot.scm +(import (ice-9 rdelim)) +(import (ice-9 match)) +(import (ice-9 regex)) +(import (ice-9 receive)) +(import (bs core)) +(import (prefix (bs string) string.)) -(display "hello") +(define (log msg) + (display msg (current-error-port))) + +(define (is-command? msg) + (string.prefix? msg "bizbot:")) + +(define (parse-line line) + (if (eof-object? line) + (exit) + (let ([matches (regexp-exec + (make-regexp "<(\\S*)>(.*)" regexp/extended) + (string-drop line 11))]) + (if matches + `(user + ,(match:substring matches 1) + ,(string.lstrip (match:substring matches 2) #\space)) + `(system ,(string-drop line 11)))))) + +(define (dispatch user msg) + (let ([msg (-> msg + (string-drop (string-length "bizbot:")) + (string.lstrip #\space))]) + (cond + ((equal? msg "hi") + (display (fmt "~a: well, hello!" user))) + + (else + (display (fmt "command not understood: ~a" msg)))))) + +(while #t + (match (parse-line (read-line)) + [('user user msg) + (if (is-command? msg) + (dispatch user msg) + (begin + (log (fmt "user: ~a " user)) + (log (fmt "message: ~a" msg))))] + + [('system msg) + (log (fmt "system: ~a" msg))]) + + (newline) + (force-output)) |