1
0

chatikbot.lisp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. (in-package #:chatikbot)
  2. (defvar *admins* nil "Admins chat-ids")
  3. (defvar *bot-user-id* nil "Bot user_id")
  4. ;; Load config file
  5. (alexandria:when-let (file (probe-file
  6. (merge-pathnames "config.lisp"
  7. (asdf:component-pathname
  8. (asdf:find-system '#:chatikbot)))))
  9. (load file))
  10. ;; Init database
  11. (db-init)
  12. ;; Load settings
  13. (load-settings)
  14. ;; Load plugins
  15. (defsetting *plugins* nil "List of enabled plugins")
  16. (dolist (plugin *plugins*)
  17. (handler-case
  18. (load (merge-pathnames (format nil "plugins/~A.lisp" plugin)
  19. (asdf:component-pathname
  20. (asdf:find-system '#:chatikbot))))
  21. (error (e) (log:error e))))
  22. ;; Init plugin's database
  23. (with-db (db)
  24. (run-hooks :db-init))
  25. (defvar *telegram-last-update* 0 "Telegram last update_id")
  26. ;; getUpdates handling
  27. (defun process-updates ()
  28. (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
  29. (1+ *telegram-last-update*))
  30. :timeout 300)
  31. ;; do (setf *telegram-last-update*
  32. ;; (max *telegram-last-update* (aget "update_id" update)))
  33. do (handle-update update)))
  34. (defun handle-update (update)
  35. (log:info update)
  36. (let ((update-id (aget "update_id" update))
  37. (reply-to (aget "id" (aget "from" (aget "reply_to_message" (aget "message" update))))))
  38. (if (> update-id *telegram-last-update*)
  39. (progn
  40. (if (and reply-to (not (equal reply-to *bot-user-id*)))
  41. (log:info "Reply not to bot")
  42. (loop for (key . value) in update
  43. unless (equal "update_id" key)
  44. do (run-hooks (keyify (format nil "update-~A" key)) value)))
  45. (setf *telegram-last-update* update-id))
  46. (log:warn "Out-of-order update" update-id))))
  47. (defvar *start-message* "Hello" "Welcome message. Override it")
  48. (def-message-cmd-handler handle-start (:start)
  49. (send-response chat-id *start-message*))
  50. (def-message-admin-cmd-handler handle-admin-settings (:settings)
  51. (send-response chat-id
  52. (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
  53. (loop for symbol in *settings*
  54. collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
  55. (def-message-admin-cmd-handler handle-admin-setsetting (:setsetting)
  56. (let* ((*package* (find-package :chatikbot))
  57. (var (read-from-string (car args)))
  58. (val (read-from-string (format nil "~{~A~^ ~}" (rest args)))))
  59. (send-response chat-id (format nil "OK, ~A" (set-setting var val)))))
  60. (defcron process-watchdog ()
  61. (close
  62. (open (merge-pathnames ".watchdog"
  63. (asdf:component-pathname
  64. (asdf:find-system '#:chatikbot)))
  65. :direction :output
  66. :if-exists :supersede
  67. :if-does-not-exist :create)))
  68. (defun cleanup ()
  69. ;; Clear prev threads
  70. (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
  71. (let ((old-updates (find "process-updates"
  72. (bordeaux-threads:all-threads)
  73. :key #'bordeaux-threads:thread-name
  74. :test #'equal)))
  75. (when old-updates
  76. (bordeaux-threads:destroy-thread old-updates))))
  77. (defun start ()
  78. ;; Test telegram token
  79. (setf *bot-name* (concatenate 'string "@" (aget "username" (telegram-get-me)))
  80. *bot-user-id* (parse-integer *telegram-token* :end (position #\: *telegram-token*)))
  81. (cleanup)
  82. ;; Run 'starting' hooks to set up schedules
  83. (run-hooks :starting)
  84. (if *web-path*
  85. ;; If *web-path* is set, use webhooks
  86. (telegram-set-webhook (format nil "~A/~A" *web-path* *telegram-token*))
  87. ;; else start getUpdates thread
  88. (progn
  89. (telegram-set-webhook "") ;; Disable webhooks if present
  90. (bordeaux-threads:make-thread
  91. (lambda () (loop-with-error-backoff #'process-updates))
  92. :name "process-updates")))
  93. ;; Notify admins
  94. (dolist (admin *admins*)
  95. (ignore-errors
  96. (telegram-send-message admin (format nil "~A started at ~A" *bot-name* (format-ts (local-time:now)))))))