chatikbot.lisp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (keyify (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. ;; Test telegram token
  67. (setf *bot-name* (concatenate 'string "@" (aget "username" (telegram-get-me))))
  68. (cleanup)
  69. ;; Run 'starting' hooks to set up schedules
  70. (run-hooks :starting)
  71. (if *web-path*
  72. ;; If *web-path* is set, use webhooks
  73. (telegram-set-webhook (format nil "~A/~A" *web-path* *telegram-token*))
  74. ;; else start getUpdates thread
  75. (progn
  76. (telegram-set-webhook "") ;; Disable webhooks if present
  77. (bordeaux-threads:make-thread
  78. (lambda () (loop-with-error-backoff #'process-updates))
  79. :name "process-updates")))
  80. ;; Notify admins
  81. (dolist (admin *admins*)
  82. (telegram-send-message admin (format nil "chatikbot started at ~A" (format-ts (local-time:now))))))