random messages

This commit is contained in:
Mike Russo 2025-04-18 16:08:37 -06:00
parent 83a6c30351
commit f1a3f67533
2 changed files with 10 additions and 3 deletions

View file

@ -5,7 +5,7 @@
(in-package :birthday-boy) (in-package :birthday-boy)
(load (sb-ext:posix-getenv "ASDF")) (load (sb-ext:posix-getenv "ASDF"))
(asdf:load-systems :dexador :local-time :jonathan :cl-yaml) (asdf:load-systems :dexador :local-time :jonathan :cl-yaml :alexandria)
(defvar *birthdays* nil (defvar *birthdays* nil
"Hashtable of birthdays.") "Hashtable of birthdays.")
@ -95,14 +95,18 @@
(defun make-bday-message (person) (defun make-bday-message (person)
(format nil "Hey hey hey, its the birthday boy, and I am pleased to announce that it is ~A's today. Congratulation, I love you." person)) (format nil (alexandria:random-elt (gethash "bot_message_template" *config*)) person))
(defun seed-random-state ()
"Seed the random state with current time for true randomness between runs"
(setf *random-state* (make-random-state t)))
(defun main () (defun main ()
(dolist (person (collect-bdays)) (dolist (person (collect-bdays))
(message (make-bday-message (gethash "person" person)) "ROOM_ID"))) (message (make-bday-message (gethash "person" person)) "ROOM_ID")))
(defun check-required-env-vars () (defun check-required-env-vars ()
(let ((required-vars '("bot_name" "bot_pw" "bot_access_token" "homeserver_url" "room_id")) (let ((required-vars '("bot_name" "bot_pw" "bot_access_token" "homeserver_url" "room_id" "bot_message_template"))
(missing-vars '())) (missing-vars '()))
(dolist (var required-vars) (dolist (var required-vars)
@ -120,10 +124,12 @@
(let* ((args sb-ext:*posix-argv*) (let* ((args sb-ext:*posix-argv*)
(config-file (or (second args) "./config.yml"))) (config-file (or (second args) "./config.yml")))
(format t "Using config file: ~A~%" config-file) (format t "Using config file: ~A~%" config-file)
(unwind-protect (unwind-protect
(let* ((base-config (cl-yaml:parse (pathname config-file))) (let* ((base-config (cl-yaml:parse (pathname config-file)))
(*birthdays* (gethash "birthdays" base-config)) (*birthdays* (gethash "birthdays" base-config))
(*config* (gethash "config" base-config))) (*config* (gethash "config" base-config)))
(seed-random-state)
(check-required-env-vars) (check-required-env-vars)
(main)) (main))
(sb-ext:exit :code 0)))) (sb-ext:exit :code 0))))

View file

@ -29,6 +29,7 @@
local-time local-time
deploy deploy
cl-yaml cl-yaml
alexandria
]); ]);
in { in {
# Per-system attributes can be defined here. The self' and inputs' # Per-system attributes can be defined here. The self' and inputs'