Compare commits

..

No commits in common. "c7afa8eaab0a61781cafdd7adc6105bb0b70118b" and "a1a26e5df41f218bf74a8a1ce241dfdd215c7bde" have entirely different histories.

4 changed files with 36 additions and 27 deletions

3
.gitignore vendored
View file

@ -173,6 +173,3 @@ dist
# Finder (MacOS) folder config # Finder (MacOS) folder config
.DS_Store .DS_Store
.direnv
.envrc

View file

@ -1,14 +1,11 @@
(in-package :cl-user) (in-package :cl-user)
(defpackage :birthday-boy ;; (defpackage :birthday-boy
(:use :cl) ;; (:use :cl)
(:export main*)) ;; (:export main*))
(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 :cl-dotenv) (asdf:load-systems :dexador :local-time :jonathan :cl-yaml :cl-dotenv)
;; ;;
(defvar *birthdays* nil)
(defvar *config* nil)
;; (.env:load-env (merge-pathnames ".env")) ;; (.env:load-env (merge-pathnames ".env"))
;; (ql:quickload '(:dexador :cl-dotenv :local-time :jonathan)) ;; (ql:quickload '(:dexador :cl-dotenv :local-time :jonathan))
@ -31,7 +28,7 @@
(defun put-json-content-auth (uri json-content) (defun put-json-content-auth (uri json-content)
(dex:put uri (dex:put uri
:headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) ) :headers `((:|Authorization| . ,(format nil "Bearer ~A" (uiop:getenv "BOT_ACCESS_TOKEN")) )
(:content-type . "application/json")) (:content-type . "application/json"))
:content (jojo:to-json json-content))) :content (jojo:to-json json-content)))
@ -41,36 +38,45 @@
:content (jojo:to-json json-content))) :content (jojo:to-json json-content)))
(defun post-json-content-auth (uri json-content) (defun post-json-content-auth (uri json-content)
(dex:post uri (dex:post uri
:headers `((:|Authorization| . ,(format nil "Bearer ~A" (gethash "bot_access_token" *config*)) ) :headers `((:|Authorization| . ,(format nil "Bearer ~A" (uiop:getenv "BOT_ACCESS_TOKEN")) )
(:content-type . "application/json")) (:content-type . "application/json"))
:content (jojo:to-json json-content))) :content (jojo:to-json json-content)))
(defun make-homeserver-endpoint (endpoint) (defun make-homeserver-endpoint (endpoint)
(format nil "~a~a" (gethash "homeserver_url" *config*) endpoint)) (format nil "~a~a" (uiop:getenv "HOMESERVER_URL") endpoint))
(defun get-access-token () (defun get-access-token ()
(let ((res (let ((res
(jojo:parse (jojo:parse
(post-json-content (make-homeserver-endpoint "/_matrix/client/v3/login" ) (post-json-content (make-homeserver-endpoint "/_matrix/client/v3/login" )
(list :|identifier| (list :|type| "m.id.user" :|user| (gethash "bot_name" *config*)) (list :|identifier| (list :|type| "m.id.user" :|user| (uiop:getenv "BOT_NAME"))
:|type| "m.login.password" :|type| "m.login.password"
:|password| (gethash "bot_pw" *config*)))))) :|password| (uiop:getenv "BOT_PW"))))))
(values (values
(getf res :|access_token|) (getf res :|access_token|)
(getf res :|device_id|)))) (getf res :|device_id|))))
(defun message (msg &optional (room-id "GENERAL2_ROOM_ID")) (defun message (msg &optional (room-id "GENERAL2_ROOM_ID"))
(put-json-content-auth (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" (make-homeserver-endpoint (format nil "/_matrix/client/v3/rooms/~a/send/m.room.message/~a"
(gethash room-id *config*) (uiop:getenv room-id)
(make-txn-id))) (make-txn-id)))
(list (list
:|msgtype| "m.text" :|msgtype| "m.text"
:|body| msg))) :|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) (defun is-today (tstamp)
@ -87,7 +93,7 @@
(is-today (local-time:encode-timestamp 0 0 0 0 day month 1900))) (is-today (local-time:encode-timestamp 0 0 0 0 day month 1900)))
(defun collect-bdays () (defun collect-bdays ()
(loop for person in *birthdays* (loop for person in (gethash "birthdays" *birthdays*)
when (is-today* (gethash "day" person) (gethash "month" person)) when (is-today* (gethash "day" person) (gethash "month" person))
collect person)) collect person))
@ -96,15 +102,16 @@
(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 "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 () (defun main ()
;; (.env:load-env env-file)
(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"))
(missing-vars '())) (missing-vars '()))
(dolist (var required-vars) (dolist (var required-vars)
(unless (gethash var *config*) (unless (uiop:getenv var)
(push var missing-vars))) (push var missing-vars)))
(when missing-vars (when missing-vars
@ -116,12 +123,13 @@
t)) t))
(defun main* () (defun main* ()
(let* ((args sb-ext:*posix-argv*) (let* ((args sb-ext:*posix-argv*)
(config-file (or (second args) "./config.yml"))) (env-file (or (second args) #P".env"))
(format t "Using config file: ~A~%" config-file) (birthdays-file (or (third args) "./birthdays.yml")))
(unwind-protect (format t "Using env file: ~A~%" env-file)
(let* ((base-config (cl-yaml:parse (pathname config-file))) (format t "Using birthdays file: ~A~%" birthdays-file)
(*birthdays* (gethash "birthdays" base-config)) (.env:load-env env-file)
(*config* (gethash "config" base-config)))
(check-required-env-vars) (check-required-env-vars)
(unwind-protect
(let ((*birthdays* (cl-yaml:parse (pathname birthdays-file))))
(main)) (main))
(sb-ext:exit :code 0)))) (sb-ext:exit :code 0))))

View file

@ -47,7 +47,7 @@
buildPhase = '' buildPhase = ''
${sbcl'}/bin/sbcl --load ./bot.lisp \ ${sbcl'}/bin/sbcl --load ./bot.lisp \
--eval '(sb-ext:save-lisp-and-die #P"birthday-boy" :toplevel #'"'"'birthday-boy:main* :executable t)' --eval '(sb-ext:save-lisp-and-die #P"birthday-boy" :toplevel #'"'"'main* :executable t)'
''; '';
installPhase = '' installPhase = ''
@ -60,7 +60,7 @@
echo "#!/usr/bin/env sh" >> $out/bin/birthday-boy echo "#!/usr/bin/env sh" >> $out/bin/birthday-boy
echo "LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${ echo "LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:${
pkgs.lib.makeLibraryPath [ pkgs.openssl pkgs.libyaml ] pkgs.lib.makeLibraryPath [ pkgs.openssl pkgs.libyaml ]
} $out/bin/_birthday-boy \$1" >> $out/bin/birthday-boy } $out/bin/_birthday-boy \$1 \$2" >> $out/bin/birthday-boy
chmod +x $out/bin/birthday-boy chmod +x $out/bin/birthday-boy
''; '';

4
run.sh Executable file
View file

@ -0,0 +1,4 @@
#!/usr/bin/env sh
export $(grep -v '^#' ${1:-.env} | xargs -d '\n')
$out/bin/_birthday-boy