(in-package :cl-user) (defpackage chatikbot.bot (:use :cl :chatikbot.utils :chatikbot.macros) (:import-from :chatikbot.db :set-setting) (:import-from :chatikbot.telegram :telegram-get-updates :send-response) (:export :handle-update)) (in-package :chatikbot.bot) (defvar *telegram-last-update* 0 "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 *telegram-last-update* (aget "update_id" update))) do (handle-update update))) (defun handle-update (update) (log:info update) (let ((update-id (aget "update_id" update)) (reply-to (aget "id" (aget "from" (aget "reply_to_message" (aget "message" update)))))) (if (> update-id *telegram-last-update*) (progn (if (and reply-to (not (equal reply-to *bot-user-id*))) (log:info "Reply not to bot") (loop for (key . value) in update unless (equal "update_id" key) do (run-hooks (keyify (format nil "update-~A" key)) value))) (setf *telegram-last-update* update-id)) (log:warn "Out-of-order update" update-id)))) (defvar *start-message* "Hello" "Welcome message. Override it") (def-message-cmd-handler handle-start (:start) (send-response chat-id *start-message*)) (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* ((*package* (find-package :chatikbot)) (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)))))