chatikbot.lisp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. (in-package #:chatikbot)
  2. (defvar *admins* nil "Admins chat-ids")
  3. ;; Load config file
  4. (alexandria:when-let (file (probe-file
  5. (merge-pathnames "config.lisp"
  6. (asdf:component-pathname
  7. (asdf:find-system '#:chatikbot)))))
  8. (load file))
  9. ;; Init database
  10. (db-init)
  11. ;; Load settings
  12. (load-settings)
  13. ;; Load plugins
  14. (defsetting *plugins* nil "List of enabled plugins")
  15. (dolist (plugin *plugins*)
  16. (handler-case
  17. (load (merge-pathnames (format nil "plugins/~A.lisp" plugin)
  18. (asdf:component-pathname
  19. (asdf:find-system '#:chatikbot))))
  20. (error (e) (log:error e))))
  21. ;; Init plugin's database
  22. (with-db (db)
  23. (run-hooks :db-init))
  24. (defvar *telegram-last-update* nil "Telegram last update_id")
  25. ;; getUpdates handling
  26. (defun process-updates ()
  27. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  28. (1+ *telegram-last-update*))
  29. :timeout 300)
  30. do (setf *telegram-last-update*
  31. (max (or *telegram-last-update* 0)
  32. (aget "update_id" update)))
  33. do (handle-update update)))
  34. (defun handle-update (update)
  35. (log:info update)
  36. (loop for (key . value) in update
  37. unless (equal "update_id" key)
  38. do (run-hooks (string-to-event (format nil "update-~A" key)) value)))
  39. (def-message-admin-cmd-handler handle-admin-settings (:settings)
  40. (send-response chat-id
  41. (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
  42. (loop for symbol in *settings*
  43. collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
  44. (def-message-admin-cmd-handler handle-admin-setsetting (:setsetting)
  45. (let ((var (read-from-string (car args)))
  46. (val (read-from-string (format nil "~{~A~^ ~}" (rest args)))))
  47. (send-response chat-id (format nil "OK, ~A" (set-setting var val)))))
  48. (defcron process-watchdog ()
  49. (close
  50. (open (merge-pathnames ".watchdog"
  51. (asdf:component-pathname
  52. (asdf:find-system '#:chatikbot)))
  53. :direction :output
  54. :if-exists :supersede
  55. :if-does-not-exist :create)))
  56. (defun cleanup ()
  57. ;; Clear prev threads
  58. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  59. (let ((old-updates (find "process-updates"
  60. (bordeaux-threads:all-threads)
  61. :key #'bordeaux-threads:thread-name
  62. :test #'equal)))
  63. (when old-updates
  64. (bordeaux-threads:destroy-thread old-updates))))
  65. (defun start ()
  66. (cleanup)
  67. ;; Run 'starting' hooks to set up schedules
  68. (run-hooks :starting)
  69. ;; Start getUpdates thread
  70. (bordeaux-threads:make-thread
  71. (lambda () (loop-with-error-backoff #'process-updates))
  72. :name "process-updates")
  73. ;; Notify admins
  74. (dolist (admin *admins*)
  75. (telegram-send-message admin (format nil "chatikbot started at ~A" (format-ts (local-time:now))))))