1
0

chatikbot.lisp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. (defvar *telegram-last-update* nil "Telegram last update_id")
  14. ;; getUpdates handling
  15. (defun process-updates ()
  16. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  17. (1+ *telegram-last-update*))
  18. :timeout 300)
  19. do (setf *telegram-last-update*
  20. (max (or *telegram-last-update* 0)
  21. (aget "update_id" update)))
  22. do (handle-update update)))
  23. (defun handle-update (update)
  24. (log:info update)
  25. (loop for (key . value) in update
  26. unless (equal "update_id" key)
  27. do (run-hooks (string-to-event (format nil "update-~A" key)) value)))
  28. (defun send-dont-understand (chat-id &optional text reply-id)
  29. (let ((resp (eliza text)))
  30. (log:info text resp)
  31. (when resp
  32. (send-response chat-id resp reply-id))))
  33. (defun handle-unknown-message (message)
  34. (let ((chat-id (aget "id" (aget "chat" message)))
  35. (text (aget "text" message)))
  36. (log:info "handle-unknown-message" message)
  37. (send-dont-understand chat-id (preprocess-input text))
  38. t))
  39. (add-hook :update-message 'handle-unknown-message t)
  40. ;; Schedule
  41. (defmacro defcron (name (&rest schedule) &body body)
  42. (let ((schedule (or schedule '(:minute '* :hour '*))))
  43. `(progn
  44. (defun ,name ()
  45. (handler-case (progn ,@body)
  46. (error (e) (log:error e))))
  47. (add-hook :starting #'(lambda ()
  48. (clon:schedule-function
  49. ',name (clon:make-scheduler
  50. (clon:make-typed-cron-schedule
  51. ,@schedule)
  52. :allow-now-p t)
  53. :name ',name :thread t)
  54. (values))))))
  55. (defcron process-watchdog ()
  56. (close
  57. (open (merge-pathnames ".watchdog"
  58. (asdf:component-pathname
  59. (asdf:find-system '#:chatikbot)))
  60. :direction :output
  61. :if-exists :supersede
  62. :if-does-not-exist :create)))
  63. (defsetting *plugins* nil "List of enabled plugins")
  64. (defun start ()
  65. ;; Clear prev threads
  66. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  67. (let ((old-updates (find "process-updates"
  68. (bordeaux-threads:all-threads)
  69. :key #'bordeaux-threads:thread-name
  70. :test #'equal)))
  71. (when old-updates
  72. (bordeaux-threads:destroy-thread old-updates)))
  73. ;; Load plugins
  74. (dolist (plugin *plugins*)
  75. (handler-case
  76. (load (merge-pathnames (format nil "~A.lisp" plugin)
  77. (asdf:component-pathname
  78. (asdf:find-system '#:chatikbot))))
  79. (error (e) (log:error e))))
  80. ;; Run 'starting' hooks to set up schedules
  81. (run-hooks :starting)
  82. ;; Start getUpdates thread
  83. (bordeaux-threads:make-thread
  84. (lambda () (loop-with-error-backoff #'process-updates))
  85. :name "process-updates")
  86. ;; Notify admins
  87. (dolist (admin *admins*)
  88. (telegram-send-message admin (format nil "chatikbot started at ~A" (format-ts (local-time:now))))))