|
|
@@ -45,30 +45,63 @@
|
|
|
(place (aget "place" item))
|
|
|
(cdt (aget "cdt" item)))
|
|
|
(case event
|
|
|
- (1 (format nil "_~A_ *~a*: 🚗 ~a, ~a → ~a" cdt amount (or alias pan) entry place))
|
|
|
- (101 (format nil "_~A_ *~a*: 💶 ~a" cdt amount place)))))
|
|
|
+ (1 (format nil "~A *~aр.*: 🚗 ~a, _~a → ~a_" cdt amount (or alias pan) entry place))
|
|
|
+ (101 (format nil "~A *~aр.*: 💶 _~a_" cdt amount place)))))
|
|
|
|
|
|
(defun zsd/format-changes (old new)
|
|
|
(let ((wall-diff (set-difference (aget "wall" new) (aget "wall" old) :test #'equal)))
|
|
|
(when wall-diff
|
|
|
- (format nil "ZSD balance: *~A*~%~%~{~A~^~%~}"
|
|
|
- (aget "remainder" (car (aget "contract" new)))
|
|
|
+ (format nil "ЗСД остаток: *~$р.*~%~%~{~A~^~%~}"
|
|
|
+ (parse-float (aget "remainder" (car (aget "contract" new))))
|
|
|
(loop for item in (reverse wall-diff)
|
|
|
collect (%zsd/format-wall item (aget "pan" new)))))))
|
|
|
|
|
|
+(defun zsd/handle-set-cron (chat-id enable)
|
|
|
+ (lists-set-entry :zsd chat-id enable)
|
|
|
+ (bot-send-message chat-id
|
|
|
+ (if enable
|
|
|
+ "Включил рассылку. '/zsd off' чтобы выключить, /zsd - показать последние."
|
|
|
+ "Без рассылки. '/zsd on' - включить, /zsd - последние.")))
|
|
|
+
|
|
|
+(defun zsd/handle-auth (chat-id login pass)
|
|
|
+ (bot-send-message chat-id
|
|
|
+ (let ((token (zsd/auth login pass)))
|
|
|
+ (if token
|
|
|
+ (progn
|
|
|
+ (secret/set `(:zsd ,chat-id) token)
|
|
|
+ (zsd/handle-set-cron chat-id t))
|
|
|
+ "Чот не смог, пропробуй другие."))))
|
|
|
+
|
|
|
+(defun zsd/handle-recent (chat-id)
|
|
|
+ (secret/with (token (list :zsd chat-id))
|
|
|
+ (bot-send-message chat-id
|
|
|
+ (if token
|
|
|
+ (let ((data (zsd/load-data token)))
|
|
|
+ (if data
|
|
|
+ (zsd/format-changes nil data)
|
|
|
+ "Не смог получить данные. Попробуй перелогинься. /zsd <login> <pass>"))
|
|
|
+ "Нужен логин-пароль. /zsd <login> <pass>")
|
|
|
+ :parse-mode "markdown")))
|
|
|
+
|
|
|
(def-message-cmd-handler handle-cmd-zsd (:zsd)
|
|
|
- (secret/with (token `(:zsd ,chat-id))
|
|
|
- (if token
|
|
|
- (bot-send-message chat-id (zsd/format-changes nil (zsd/load-data token)) :parse-mode "markdown")
|
|
|
- (send-response chat-id "/zsd-auth"))))
|
|
|
+ (cond
|
|
|
+ ((= 1 (length args))
|
|
|
+ (zsd/handle-set-cron chat-id (equal "on" (car args))))
|
|
|
+ ((= 2 (length args)) (apply 'zsd/handle-auth chat-id args))
|
|
|
+ (:otherwise (zsd/handle-recent chat-id))))
|
|
|
|
|
|
-(def-message-cmd-handler handle-cmd-zsd-auth (:zsd-auth)
|
|
|
- (destructuring-bind (login password) args
|
|
|
- (if (and login password)
|
|
|
- (let ((token (zsd/auth login password)))
|
|
|
- (if token
|
|
|
- (progn
|
|
|
- (secret/set `(:zsd ,chat-id) token)
|
|
|
- (bot-send-message chat-id (zsd/format-changes nil (zsd/load-data token)) :parse-mode "markdown"))
|
|
|
- (send-response chat-id "Can't auth")))
|
|
|
- (send-response chat-id "Usage: /zsd-auth <username> <password>"))))
|
|
|
+(defvar *zsd/last-results* (make-hash-table) "Last check results")
|
|
|
+(defcron process-zsd (:minute '(member 0 10 20 30 40 50))
|
|
|
+ (dolist (chat-id (lists-get :zsd))
|
|
|
+ (secret/with (token (list :zsd chat-id))
|
|
|
+ (if token
|
|
|
+ (let ((old (gethash chat-id *zsd/last-results*))
|
|
|
+ (new (zsd/load-data token)))
|
|
|
+ (when new
|
|
|
+ (when old
|
|
|
+ (alexandria:when-let ((changes (zsd/format-changes old new)))
|
|
|
+ (bot-send-message chat-id changes :parse-mode "markdown")))
|
|
|
+ (setf (gethash chat-id *zsd/last-results*) new)))
|
|
|
+ (progn
|
|
|
+ (log:warn "zsd no token for" chat-id)
|
|
|
+ (lists-set-entry :zsd chat-id nil))))))
|