| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- (in-package #:chatikbot)
- (defvar *admins* nil "Admins chat-ids")
- ;; Load config file
- (alexandria:when-let (file (probe-file
- (merge-pathnames "config.lisp"
- (asdf:component-pathname
- (asdf:find-system '#:chatikbot)))))
- (load file))
- ;; Init database
- (db-init)
- ;; Load settings
- (load-settings)
- ;; Load plugins
- (defsetting *plugins* nil "List of enabled plugins")
- (dolist (plugin *plugins*)
- (handler-case
- (load (merge-pathnames (format nil "plugins/~A.lisp" plugin)
- (asdf:component-pathname
- (asdf:find-system '#:chatikbot))))
- (error (e) (log:error e))))
- ;; Init plugin's database
- (with-db (db)
- (run-hooks :db-init))
- (defvar *telegram-last-update* nil "Telegram last update_id")
- ;; getUpdates handling
- (defun process-updates ()
- (loop for update in (telegram-get-updates :offset (and *telegram-last-update*
- (1+ *telegram-last-update*))
- :timeout 300)
- do (setf *telegram-last-update*
- (max (or *telegram-last-update* 0)
- (aget "update_id" update)))
- do (handle-update update)))
- (defun handle-update (update)
- (log:info update)
- (loop for (key . value) in update
- unless (equal "update_id" key)
- do (run-hooks (keyify (format nil "update-~A" key)) value)))
- (def-message-admin-cmd-handler handle-admin-settings (:settings)
- (send-response chat-id
- (format nil "~{~{~A~@[ (~A)~]: ~A~}~^~%~}"
- (loop for symbol in *settings*
- collect (list symbol (documentation symbol 'variable) (symbol-value symbol))))))
- (def-message-admin-cmd-handler handle-admin-setsetting (:setsetting)
- (let ((var (read-from-string (car args)))
- (val (read-from-string (format nil "~{~A~^ ~}" (rest args)))))
- (send-response chat-id (format nil "OK, ~A" (set-setting var val)))))
- (defcron process-watchdog ()
- (close
- (open (merge-pathnames ".watchdog"
- (asdf:component-pathname
- (asdf:find-system '#:chatikbot)))
- :direction :output
- :if-exists :supersede
- :if-does-not-exist :create)))
- (defun cleanup ()
- ;; Clear prev threads
- (mapc #'trivial-timers:unschedule-timer (trivial-timers:list-all-timers))
- (let ((old-updates (find "process-updates"
- (bordeaux-threads:all-threads)
- :key #'bordeaux-threads:thread-name
- :test #'equal)))
- (when old-updates
- (bordeaux-threads:destroy-thread old-updates))))
- (defun start ()
- ;; Test telegram token
- (setf *bot-name* (concatenate 'string "@" (aget "username" (telegram-get-me))))
- (cleanup)
- ;; Run 'starting' hooks to set up schedules
- (run-hooks :starting)
- (if *web-path*
- ;; If *web-path* is set, use webhooks
- (telegram-set-webhook (format nil "~A/~A" *web-path* *telegram-token*))
- ;; else start getUpdates thread
- (progn
- (telegram-set-webhook "") ;; Disable webhooks if present
- (bordeaux-threads:make-thread
- (lambda () (loop-with-error-backoff #'process-updates))
- :name "process-updates")))
- ;; Notify admins
- (dolist (admin *admins*)
- (telegram-send-message admin (format nil "chatikbot started at ~A" (format-ts (local-time:now))))))
|