1
0

chatikbot.lisp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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* 0 "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 *telegram-last-update* (aget "update_id" update)))
  32. do (handle-update update)))
  33. (defun handle-update (update)
  34. (log:info update)
  35. (let ((update-id (aget "update_id" update))
  36. (reply-to (aget "id" (aget "from" (aget "reply_to_message" (aget "message" update))))))
  37. (if (> update-id *telegram-last-update*)
  38. (progn
  39. (if (and reply-to (not (equal reply-to (parse-integer *telegram-token* :end (position #\: *telegram-token*)))))
  40. (log:info "Reply not to bot")
  41. (loop for (key . value) in update
  42. unless (equal "update_id" key)
  43. do (run-hooks (keyify (format nil "update-~A" key)) value)))
  44. (setf *telegram-last-update* update-id))
  45. (log:warn "Out-of-order update" update-id))))
  46. (def-message-admin-cmd-handler handle-admin-settings (:settings)
  47. (send-response chat-id
  48. (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
  49. (loop for symbol in *settings*
  50. collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
  51. (def-message-admin-cmd-handler handle-admin-setsetting (:setsetting)
  52. (let* ((*package* (find-package :chatikbot))
  53. (var (read-from-string (car args)))
  54. (val (read-from-string (format nil "~{~A~^ ~}" (rest args)))))
  55. (send-response chat-id (format nil "OK, ~A" (set-setting var val)))))
  56. (defcron process-watchdog ()
  57. (close
  58. (open (merge-pathnames ".watchdog"
  59. (asdf:component-pathname
  60. (asdf:find-system '#:chatikbot)))
  61. :direction :output
  62. :if-exists :supersede
  63. :if-does-not-exist :create)))
  64. (defun cleanup ()
  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. (defun start ()
  74. ;; Test telegram token
  75. (setf *bot-name* (concatenate 'string "@" (aget "username" (telegram-get-me))))
  76. (cleanup)
  77. ;; Run 'starting' hooks to set up schedules
  78. (run-hooks :starting)
  79. (if *web-path*
  80. ;; If *web-path* is set, use webhooks
  81. (telegram-set-webhook (format nil "~A/~A" *web-path* *telegram-token*))
  82. ;; else start getUpdates thread
  83. (progn
  84. (telegram-set-webhook "") ;; Disable webhooks if present
  85. (bordeaux-threads:make-thread
  86. (lambda () (loop-with-error-backoff #'process-updates))
  87. :name "process-updates")))
  88. ;; Notify admins
  89. (dolist (admin *admins*)
  90. (telegram-send-message admin (format nil "chatikbot started at ~A" (format-ts (local-time:now))))))