diff --git a/bot.lisp b/bot.lisp index be5f4e9..6c819f0 100644 --- a/bot.lisp +++ b/bot.lisp @@ -1,11 +1,14 @@ (in-package :cl-user) -;; (defpackage :birthday-boy -;; (:use :cl) -;; (:export main*)) -;; (in-package :birthday-boy) +(defpackage :birthday-boy + (:use :cl) + (:export main*)) +(in-package :birthday-boy) + (load (sb-ext:posix-getenv "ASDF")) (asdf:load-systems :dexador :local-time :jonathan :cl-yaml :cl-dotenv) ;; +(defvar *birthdays* nil) +(defvar *config* nil) ;; (.env:load-env (merge-pathnames ".env")) ;; (ql:quickload '(:dexador :cl-dotenv :local-time :jonathan)) @@ -28,7 +31,7 @@ (defun put-json-content-auth (uri json-content) (dex:put uri - :headers `((:|Authorization| . ,(format nil "Bearer ~A" (uiop:getenv "BOT_ACCESS_TOKEN")) ) + :headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) ) (:content-type . "application/json")) :content (jojo:to-json json-content))) @@ -38,45 +41,36 @@ :content (jojo:to-json json-content))) (defun post-json-content-auth (uri json-content) (dex:post uri - :headers `((:|Authorization| . ,(format nil "Bearer ~A" (uiop:getenv "BOT_ACCESS_TOKEN")) ) + :headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) ) (:content-type . "application/json")) :content (jojo:to-json json-content))) (defun make-homeserver-endpoint (endpoint) - (format nil "~a~a" (uiop:getenv "HOMESERVER_URL") endpoint)) + (format nil "~a~a" (gethash "homeserver_url" *config*) endpoint)) (defun get-access-token () (let ((res (jojo:parse (post-json-content (make-homeserver-endpoint "/_matrix/client/v3/login" ) - (list :|identifier| (list :|type| "m.id.user" :|user| (uiop:getenv "BOT_NAME")) + (list :|identifier| (list :|type| "m.id.user" :|user| (gethash "bot_name" *config*)) :|type| "m.login.password" - :|password| (uiop:getenv "BOT_PW")))))) + :|password| (gethash "bot_pw" *config*)))))) (values (getf res :|access_token|) (getf res :|device_id|)))) (defun message (msg &optional (room-id "GENERAL2_ROOM_ID")) (put-json-content-auth - ;; (format nil "https://matrix.groupchattt.page/_matrix/client/v3/rooms/~a/send/m.room.message/~a" - ;; (uiop:getenv room-id) - ;; (make-txn-id)) (make-homeserver-endpoint (format nil "/_matrix/client/v3/rooms/~a/send/m.room.message/~a" - (uiop:getenv room-id) + (gethash room-id *config*) (make-txn-id))) (list :|msgtype| "m.text" :|body| msg))) -;; (defun request-join (room) -;; (post-json-content-auth -;; (make-homeserver-endpoint (format nil "/_matrix/client/v3/knock/~A" room)) -;; (list -;; :|reason| "for birthday bot"))) -(defvar *birthdays* nil) (defun is-today (tstamp) @@ -93,7 +87,7 @@ (is-today (local-time:encode-timestamp 0 0 0 0 day month 1900))) (defun collect-bdays () - (loop for person in (gethash "birthdays" *birthdays*) + (loop for person in *birthdays* when (is-today* (gethash "day" person) (gethash "month" person)) collect person)) @@ -102,16 +96,15 @@ (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)) (defun main () - ;; (.env:load-env env-file) (dolist (person (collect-bdays)) (message (make-bday-message (gethash "person" person)) "ROOM_ID"))) (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")) (missing-vars '())) (dolist (var required-vars) - (unless (uiop:getenv var) + (unless (gethash var *config*) (push var missing-vars))) (when missing-vars @@ -123,13 +116,12 @@ t)) (defun main* () (let* ((args sb-ext:*posix-argv*) - (env-file (or (second args) #P".env")) - (birthdays-file (or (third args) "./birthdays.yml"))) - (format t "Using env file: ~A~%" env-file) - (format t "Using birthdays file: ~A~%" birthdays-file) - (.env:load-env env-file) - (check-required-env-vars) + (config-file (or (second args) "./config.yml"))) + (format t "Using config file: ~A~%" config-file) (unwind-protect - (let ((*birthdays* (cl-yaml:parse (pathname birthdays-file)))) + (let* ((base-config (cl-yaml:parse (pathname config-file))) + (*birthdays* (gethash "birthdays" base-config)) + (*config* (gethash "config" base-config))) + (check-required-env-vars) (main)) (sb-ext:exit :code 0)))) diff --git a/flake.nix b/flake.nix index 667c522..8a01110 100644 --- a/flake.nix +++ b/flake.nix @@ -47,7 +47,7 @@ buildPhase = '' ${sbcl'}/bin/sbcl --load ./bot.lisp \ - --eval '(sb-ext:save-lisp-and-die #P"birthday-boy" :toplevel #'"'"'main* :executable t)' + --eval '(sb-ext:save-lisp-and-die #P"birthday-boy" :toplevel #'"'"'birthday-boy:main* :executable t)' ''; installPhase = '' @@ -60,7 +60,7 @@ echo "#!/usr/bin/env sh" >> $out/bin/birthday-boy echo "LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${ pkgs.lib.makeLibraryPath [ pkgs.openssl pkgs.libyaml ] - } $out/bin/_birthday-boy \$1 \$2" >> $out/bin/birthday-boy + } $out/bin/_birthday-boy \$1" >> $out/bin/birthday-boy chmod +x $out/bin/birthday-boy '';