(in-package #:chatikbot) ;; Load config file (alexandria:when-let (file (probe-file (merge-pathnames "config.lisp" (asdf:component-pathname (asdf:find-system '#:chatikbot))))) (load file)) (defvar *telegram-last-update* nil "Telegram last update_id") (defun process-updates () (loop for update in (telegram-get-updates :offset (and *telegram-last-update* (1+ *telegram-last-update*)) :timeout 300) do (handle-message (aget "message" update)) do (setf *telegram-last-update* (max (or *telegram-last-update* 0) (aget "update_id" update))))) (defvar *responses* '("И чё?" "Сам-то понял?" "Ну хуй знает" "Бля..." "В душе не ебу" "Мне похуй") "Unknown command respond strings") (defun send-dont-understand (chat-id &optional text reply-id) (telegram-send-message chat-id (if (and text (zerop (random 5))) (format nil "Сам ~A" (replace-all text "@chatikbot" "")) (nth (random (length *responses*)) *responses*)) :reply-to reply-id)) (defun handle-message (message) (let ((id (aget "message_id" message)) (chat-id (aget "id" (aget "chat" message))) (text (aget "text" message))) (log:info "handle-message" message) (when text (if (equal #\/ (char text 0)) (let ((cmd (intern (string-upcase (subseq text 1 (position #\Space text))) "KEYWORD")) (args (when (position #\Space text) (split-sequence:split-sequence #\Space (subseq text (1+ (position #\Space text))))))) (case cmd (:postakb (handle-cmd-post-akb chat-id id args)) (:akb (handle-cmd-akb chat-id id args)) (otherwise (send-dont-understand chat-id text)))) (send-dont-understand chat-id text))))) (defparameter +akb-vk-domain+ "baneks" "VK.com username of 'B-category anekdotes'") (defvar *akb-send-to* nil "List of chat-id's to send AKBs to") (defun handle-cmd-post-akb (chat-id message-id args) (log:info "handle-cmd-post-akb" chat-id message-id args) (let ((message "Хуярим аники")) (if (member chat-id *akb-send-to*) (setf message "Не хуярим больше аники" *akb-send-to* (set-difference *akb-send-to* (list chat-id))) (setf *akb-send-to* (cons chat-id *akb-send-to*))) (telegram-send-message chat-id message))) (defvar *akb-max-count* 5 "Max number of tweets to return per run") (defvar *akb-last-id* 0 "id of last AKB tweet") (defun format-akb (post) (let* ((id (aget "id" post)) (url (format nil "https://vk.com/~A?w=wall~A_~A" +akb-vk-domain+ (aget "from_id" post) id))) (format nil "~A~%~A" (aget "text" post) url))) (defun process-latest-akb () (log:info "Getting latest AKBs") (dolist (post (reverse (aget "items" (vk-wall-get :domain +akb-vk-domain+ :count *akb-max-count*)))) (let ((id (aget "id" post))) (when (> id *akb-last-id*) (send-akb (format-akb post)) (setf *akb-last-id* id))))) (defun send-akb (text) (log:info "send-akb: ~A" text) (dolist (chat-id *akb-send-to*) (handler-case (telegram-send-message chat-id text :disable-web-preview 1) (error (e) (log:error e))))) (defun handle-cmd-akb (chat-id message-id args) (log:info "handle-cmd-akb" chat-id message-id args) (let ((total-aneks (aget "count" (vk-wall-get :domain +akb-vk-domain+ :count 1 :offset 10000000)))) (dolist (post (aget "items" (vk-wall-get :domain +akb-vk-domain+ :count (or (ignore-errors (parse-integer (car args))) 1) :offset (random total-aneks)))) (handler-case (telegram-send-message chat-id (format-akb post) :disable-web-preview 1) (error (e) (log:error e)))))) (defun start () (mapc #'sb-ext:unschedule-timer (trivial-timers:list-all-timers)) (clon:schedule-function (lambda () (process-latest-akb)) (clon:make-scheduler (clon:make-typed-cron-schedule :minute '* :hour '*) :allow-now-p t) :thread t) (bordeaux-threads:make-thread (lambda () (loop do (process-updates))) :name "process-updates"))