| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- (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 ()
- (handler-case
- (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))))
- (error (e)
- (log:error e))))
- (defvar *responses*
- '("И чё?" "Сам-то понял?" "Ну хуй знает" "Бля..." "В душе не ебу" "Мне похуй")
- "Unknown command respond strings")
- (defun random-choice (messages)
- (nth (random (length messages)) messages))
- (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" ""))
- (random-choice *responses*))
- :reply-to reply-id))
- (defvar *chat-locations* nil "ALIST of chat->location")
- (defun handle-message (message)
- (let ((id (aget "message_id" message))
- (chat-id (aget "id" (aget "chat" message)))
- (text (aget "text" message))
- (location (aget "location" 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))
- (:weather (handle-cmd-weather chat-id id args))
- (:help (handle-cmd-help chat-id id args))
- (otherwise (send-dont-understand chat-id text))))
- (send-dont-understand chat-id text)))
- (when location
- (push (cons chat-id location) *chat-locations*)
- (telegram-send-message chat-id "Взял на карандаш"))))
- (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))))))
- (defvar *help-responses*
- (list "Сам себе помоги, нахуй!" "Вот заняться мне больше нечем" "Нахуй пошел!"
- "Хэлп, ай нид самбади, хелп нот джаст энибади."))
- (defun handle-cmd-help (chat-id message-id args)
- (log:info "handle-cmd-help" chat-id message-id args)
- (telegram-send-message chat-id (random-choice *help-responses*)))
- (defun handle-cmd-weather (chat-id message-id args)
- (log:info "handle-cmd-weather" chat-id message-id args)
- (let ((location (cdr (assoc chat-id *chat-locations*))))
- (telegram-send-message
- chat-id
- (if location
- (forecast-format (forecast
- (aget "latitude" location)
- (aget "longitude" location)
- :hourly (find "hourly" args :key #'string-downcase :test #'equal)
- :daily (find "daily" args :key #'string-downcase :test #'equal)))
- "Так а ты чьих будешь?"))))
- (defun start ()
- (mapc #'sb-ext: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)))
- (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"))
|