| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- (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")
- (defvar *admins* nil "Admins chat-ids")
- (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-message (aget "message" update))))
- (defun send-response (chat-id response &optional reply-id)
- (if (consp response)
- (if (keywordp (car response))
- (case (car response)
- (:text (telegram-send-message chat-id (cdr response) :reply-to reply-id))
- (:sticker (telegram-send-sticker chat-id (cdr response) :reply-to reply-id)))
- (mapc #'(lambda (r) (send-response chat-id r reply-id)) response))
- (telegram-send-message chat-id response :reply-to reply-id)))
- (defun send-dont-understand (chat-id &optional text reply-id)
- (let ((resp (eliza text)))
- (log:info text resp)
- (when resp
- (send-response chat-id resp reply-id))))
- (defvar *chat-locations* nil "ALIST of chat->location")
- (defun preprocess-input (text)
- (when text
- (let ((first-word (subseq text 0 (position #\Space text))))
- (if (equal first-word "@chatikbot")
- (preprocess-input (subseq text 11))
- (replace-all text "@chatikbot" "ты")))))
- (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))
- (sticker (aget "file_id" (aget "sticker" 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))
- (:hourly (handle-cmd-weather chat-id id '("hourly")))
- (:daily (handle-cmd-weather chat-id id '("daily")))
- (:rates (handle-cmd-rates chat-id id args))
- (:charts (handle-cmd-charts chat-id id args))
- (:postcheckins (handle-cmd-post-checkins chat-id id args))
- (:friends (handle-cmd-fsq-friends chat-id id args))
- (:checkins (handle-cmd-checkins chat-id id args))
- (:rss (handle-cmd-rss chat-id id args))
- (otherwise (handle-admin-cmd chat-id text cmd args))))
- (send-dont-understand chat-id (preprocess-input text))))
- (when location
- (push (cons chat-id location) *chat-locations*)
- (telegram-send-message chat-id "Взял на карандаш")
- (save-settings))
- (when sticker
- (send-dont-understand chat-id))))
- (defmacro handling-errors (&body body)
- `(handler-case (progn ,@body)
- (simple-condition (err)
- (format *error-output* "~&~A: ~%" (class-name (class-of err)))
- (apply (function format) *error-output*
- (simple-condition-format-control err)
- (simple-condition-format-arguments err))
- (format *error-output* "~&"))
- (condition (err)
- (format *error-output* "~&~A: ~% ~S~%"
- (class-name (class-of err)) err))))
- (defun rep (input)
- (when input
- (with-output-to-string (*standard-output*)
- (let ((*package* (find-package 'chatikbot))
- (*error-output* *standard-output*))
- (handling-errors
- (format t "~{~S~^ ;~% ~}~%"
- (multiple-value-list (eval (read-from-string input)))))))))
- (defun handle-admin-cmd (chat-id text cmd args)
- (if (find chat-id *admins*)
- (case cmd
- (:eval (telegram-send-message chat-id (rep (format nil "~{~A~^ ~}" args))))
- (otherwise (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
- (send-dont-understand chat-id (preprocess-input (subseq text 1)))))
- ;; AKB
- (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)
- (save-settings)))
- (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 ()
- (handler-case
- (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)
- (save-settings))))
- (error (e) (log:error e))))
- (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)
- (handler-case
- (progn
- (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))))))
- (error (e)
- (log:error e)
- (telegram-send-message chat-id "Ошибочка вышла"))))
- ;; Finance
- (defvar *per-minute-rates* (make-circular (make-list 1440))
- "Circular list for 24h per minute rates")
- (defun process-rates ()
- (handler-case
- (push-circular (cons (local-time:timestamp-to-unix (local-time:now))
- (get-rates))
- *per-minute-rates*)
- (error (e) (log:error e))))
- (defun handle-cmd-rates (chat-id message-id args)
- (log:info "handle-cmd-rates" chat-id message-id args)
- (let ((rates (rest (peek-circular *per-minute-rates*))))
- (telegram-send-message chat-id
- (format nil "Зеленый ~A, гейро ~A, британец ~A"
- (cdar rates) (cdadr rates) (cdaddr rates)))))
- (defun handle-cmd-charts (chat-id message-id args)
- (log:info "handle-cmd-charts" chat-id message-id args)
- (telegram-send-chat-action chat-id "upload_photo")
- (handler-case
- (let* ((usd (or (null args) (find "usd" args :test #'equal)))
- (eur (or (null args) (find "eur" args :test #'equal)))
- (gbp (or (null args) (find "gbp" args :test #'equal)))
- (rates (rest (peek-circular *per-minute-rates*))))
- (if (or usd eur gbp)
- (telegram-send-photo chat-id
- (make-chart *per-minute-rates*
- :usd usd :eur eur :gbp gbp)
- :caption (format nil "Зеленый ~A, гейро ~A, британец ~A"
- (cdar rates) (cdadr rates) (cdaddr rates)))
- (telegram-send-message chat-id "Хуй тебе")))
- (error (e)
- (log:error e)
- (telegram-send-message chat-id "Хуйня какая-то"))))
- ;; Weather
- (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
- (handler-case
- (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)))
- (error (e)
- (log:error e)
- "Ошибочка вышла"))
- "Так а ты чьих будешь?"))))
- ;; Foursquare
- (defvar *fsq-send-to* (make-hash-table)
- "Hash of chat-id's to fsq users list to send checkings to")
- (defun fsq-user-name (user)
- (when user
- (format nil "~@[~A~]~@[ ~A~]"
- (aget "firstName" user)
- (aget "lastName" user))))
- (defun handle-cmd-post-checkins (chat-id message-id args)
- (log:info "handle-cmd-post-checkins" chat-id message-id args)
- (handler-case
- (let ((users (gethash chat-id *fsq-send-to*))
- (friends (fsq-fetch-friends)))
- (if (null args)
- (telegram-send-message chat-id
- (if (null users)
- "Пока никого не палим"
- (format nil "Палим ~{~A~^, ~}"
- (loop for user in friends
- when (member (aget "id" user)
- users :test #'equal)
- collect (fsq-user-name user)))))
- (progn
- (dolist (user args)
- (let ((username (fsq-user-name
- (find user friends
- :test #'equal
- :key #'(lambda (f) (aget "id" f))))))
- (when username
- (if (member user users :test #'equal)
- (progn
- (setf users (remove user users :test #'equal))
- (telegram-send-message chat-id
- (format nil "Больше не палим ~A" username)))
- (progn
- (push user users)
- (telegram-send-message chat-id (format nil "Теперь палим ~A" username)))))))
- (setf (gethash chat-id *fsq-send-to*) users)
- (save-settings))))
- (error (e)
- (log:error e)
- (telegram-send-message chat-id "Ошибочка вышла"))))
- (defun handle-cmd-fsq-friends (chat-id message-id args)
- (log:info "handle-cmd-fsq-friends" chat-id message-id args)
- (handler-case
- (let ((users (gethash chat-id *fsq-send-to*))
- (friends (fsq-fetch-friends)))
- (telegram-send-message
- chat-id
- (format
- nil "~{~A: ~:[~;📍 ~]~A~^~%~}"
- (loop for user in friends
- append (list
- (aget "id" user)
- (member (aget "id" user) users :test #'equal)
- (fsq-user-name user))))))
- (error (e) (log:error e))))
- (defun handle-cmd-checkins (chat-id message-id args)
- (log:info "handle-cmd-checkins" chat-id message-id args)
- (handler-case
- (let ((users (gethash chat-id *fsq-send-to*)))
- (when users
- (telegram-send-message
- chat-id
- (format nil "~{~A~^~%~}"
- (loop for checkin in (fsq-fetch-checkins)
- if (member (aget "id" (aget "user" checkin)) users :test #'equal)
- collect (fsq-format-checkin checkin t))))))
- (error (e) (log:error e))))
- (defvar *fsq-seen-ids* nil "Ids of seen checkins")
- (defun process-latest-checkins ()
- (handler-case
- (let ((checkins (make-hash-table)))
- (dolist (checkin (fsq-fetch-new-checkins))
- (let ((id (aget "id" checkin))
- (user (aget "id" (aget "user" checkin))))
- (unless (find id *fsq-seen-ids* :test #'equal)
- (loop for chat-id being the hash-keys in *fsq-send-to* using (hash-value users)
- do (when (member user users :test #'equal)
- (push (fsq-format-checkin checkin)
- (gethash chat-id checkins))))
- (push id *fsq-seen-ids*))))
- (loop for chat-id being the hash-keys in checkins using (hash-value texts)
- do (log:info "Sending checkins" chat-id texts)
- (telegram-send-message chat-id (format nil "~{~A~^~%~}" texts))))
- (error (e) (log:error e))))
- ;; RSS
- (defvar *rss-feeds* nil "All aggragated RSS feeds")
- (defvar *rss-chat-feeds* (make-hash-table) "Chat->Feeds mapping")
- (defun %send-feeds (chat-id feeds)
- (telegram-send-message
- chat-id
- (if (null feeds)
- "Пока ничего не постим"
- (format nil "Постим:~%~{~A (~A)~^~%~}"
- (loop for feed in feeds
- append (list (feed-title feed) (feed-url feed)))))
- :disable-web-preview 1))
- (defun %get-feed (url)
- (when url
- (or (find url *rss-feeds* :key #'feed-url :test #'equal)
- (let ((feed (build-feed url)))
- (fetch-new-items feed)
- (push feed *rss-feeds*)
- feed))))
- (defun %used-feed-p (feed)
- (loop for feeds being the hash-values in *rss-chat-feeds*
- when (member feed feeds)
- do (return t)))
- (defun %refresh-feeds ()
- (setf *rss-feeds*
- (remove-if-not #'%used-feed-p *rss-feeds*)))
- (defun handle-cmd-rss (chat-id message-id args)
- (log:info "handle-cmd-rss" chat-id message-id args)
- (handler-case
- (let ((feeds (gethash chat-id *rss-chat-feeds*)))
- (if (null args)
- (%send-feeds chat-id feeds)
- (progn
- (dolist (url args)
- (alexandria:when-let ((feed (%get-feed (find-rss-url url))))
- (if (member feed feeds)
- (setf feeds (remove feed feeds))
- (push feed feeds))))
- (setf (gethash chat-id *rss-chat-feeds*) feeds)
- (%refresh-feeds)
- (save-settings)
- (%send-feeds chat-id feeds))))
- (error (e)
- (log:error e)
- (telegram-send-message chat-id "Ошибочка вышла"))))
- (defun %feed-send-to (feed)
- (loop for chat-id being the hash-keys in *rss-chat-feeds* using (hash-value feeds)
- when (member feed feeds)
- collect chat-id))
- (defun process-feeds ()
- (handler-case
- (dolist (feed (remove-if-not #'need-fetch-p *rss-feeds*))
- (log:info "Fetching new items" (feed-url feed))
- (dolist (item (fetch-new-items feed))
- (dolist (chat-id (%feed-send-to feed))
- (telegram-send-message chat-id
- (format-feed-item feed item)
- :disable-web-preview 1))))
- (error (e) (log:error e))))
- (defun %load-rss-feeds (alist)
- (alexandria:alist-hash-table
- (loop for (chat-id . urls) in alist
- collect (cons chat-id (mapcar #'%get-feed urls)))))
- (defun %save-rss-feeds ()
- (loop for chat-id being the hash-keys in *rss-chat-feeds* using (hash-value feeds)
- collect (cons chat-id (mapcar #'feed-url feeds))))
- (defun save-settings()
- (with-open-file (s (merge-pathnames "settings.lisp"
- (asdf:component-pathname
- (asdf:find-system '#:chatikbot)))
- :direction :output
- :if-exists :supersede
- :if-does-not-exist :create)
- (write '(in-package #:chatikbot) :stream s)
- (write
- `(setf *fsq-send-to* (alexandria:alist-hash-table ',(alexandria:hash-table-alist *fsq-send-to*))
- *chat-locations* ',*chat-locations*
- *akb-send-to* ',*akb-send-to*
- *akb-last-id* ,*akb-last-id*
- *rss-chat-feeds* (%load-rss-feeds ',(%save-rss-feeds)))
- :stream s)))
- (defun start ()
- ;; 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)))
- ;; Load settings file
- (alexandria:when-let (file (probe-file
- (merge-pathnames "settings.lisp"
- (asdf:component-pathname
- (asdf:find-system '#:chatikbot)))))
- (load file))
- ;; Start timers
- (clon:schedule-function
- (lambda () (process-latest-akb))
- (clon:make-scheduler
- (clon:make-typed-cron-schedule :minute '* :hour '*)
- :allow-now-p t)
- :thread t)
- (clon:schedule-function
- (lambda () (process-latest-checkins))
- (clon:make-scheduler
- (clon:make-typed-cron-schedule :minute '* :hour '*)
- :allow-now-p t)
- :thread t)
- (clon:schedule-function
- (lambda () (process-rates))
- (clon:make-scheduler
- (clon:make-typed-cron-schedule :minute '* :hour '*)
- :allow-now-p t)
- :thread t)
- (clon:schedule-function
- (lambda () (process-feeds))
- (clon:make-scheduler
- (clon:make-typed-cron-schedule :minute '* :hour '*)
- :allow-now-p t)
- :thread t)
- ;; Start getUpdates thread
- (bordeaux-threads:make-thread
- (lambda ()
- (in-package #:chatikbot)
- (loop-with-error-backoff #'process-updates))
- :name "process-updates"))
|