zsd.lisp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. (in-package #:chatikbot)
  2. (defparameter +zsd-api-url+ "https://mcabinet.nch-spb.com/onyma/system/api/json")
  3. (defparameter +zsd-auth-url+ "https://mcabinet.nch-spb.com/onyma/system/api/jsonex?function=open_session")
  4. (defun %zsd/api (token method &optional args)
  5. (let* ((f (concatenate 'string "onm_api_toll_api_" method))
  6. (params (loop for (k . v) in args when v
  7. collect (cons (princ-to-string k) (princ-to-string v))))
  8. (response
  9. (json-request
  10. +zsd-api-url+
  11. :parameters (append params `(("function" . ,f)
  12. ("auth_token" . ,token))))))
  13. (values (aget "return" response) (aget "more_rows" response))))
  14. (defun zsd/auth (login password)
  15. (aget "return"
  16. (json-request +zsd-auth-url+ :method :post
  17. :content (plist-json (list :realm "WHSD"
  18. :user login
  19. :pass password)))))
  20. (defun zsd/pan (token)
  21. (%zsd/api token "mobile_pan"))
  22. (defun zsd/contract (token)
  23. (%zsd/api token "contract_info"))
  24. (defun zsd/wall (token &optional (offset 0) (limit 5))
  25. (%zsd/api token "mobile_wall"
  26. `(("rows_skip" . ,offset) ("rows_limit" . ,limit))))
  27. (defun zsd/load-data (token &optional (limit 5))
  28. `(("contract" . ,(zsd/contract token))
  29. ("pan" . ,(zsd/pan token))
  30. ("wall" . ,(zsd/wall token 0 limit))))
  31. (defun %zsd/format-wall (item pans)
  32. (let* ((pan (aget "pan" item))
  33. (alias (aget "alias" (find pan pans :test #'equal :key (lambda (el) (aget "pan" el)))))
  34. (event (parse-integer (aget "event_type" item)))
  35. (amount (aget "amount" item))
  36. (entry (aget "entry_place" item))
  37. (place (aget "place" item))
  38. (cdt (aget "cdt" item)))
  39. (case event
  40. (1 (format nil "~A *~aр.*: 🚗 ~a, _~a → ~a_" cdt amount (or alias pan) entry place))
  41. (101 (format nil "~A *~aр.*: 💶 _~a_" cdt amount place)))))
  42. (defun zsd/format-changes (old new)
  43. (let ((wall-diff (set-difference (aget "wall" new) (aget "wall" old) :test #'equal)))
  44. (when wall-diff
  45. (format nil "ЗСД остаток: *~$р.*~%~%~{~A~^~%~}"
  46. (parse-float (aget "remainder" (car (aget "contract" new))))
  47. (loop for item in (reverse wall-diff)
  48. collect (%zsd/format-wall item (aget "pan" new)))))))
  49. (defun zsd/handle-set-cron (chat-id enable)
  50. (lists-set-entry :zsd chat-id enable)
  51. (bot-send-message chat-id
  52. (if enable
  53. "Включил рассылку. '/zsd off' чтобы выключить, /zsd - показать последние."
  54. "Без рассылки. '/zsd on' - включить, /zsd - последние.")))
  55. (defun zsd/handle-auth (chat-id login pass)
  56. (let ((token (zsd/auth login pass)))
  57. (if token
  58. (progn
  59. (secret/set `(:zsd ,chat-id) token)
  60. (zsd/handle-set-cron chat-id t))
  61. (bot-send-message chat-id "Чот не смог, пропробуй другие."))))
  62. (defun zsd/handle-recent (chat-id)
  63. (secret/with (token (list :zsd chat-id))
  64. (bot-send-message chat-id
  65. (if token
  66. (let ((data (zsd/load-data token)))
  67. (if data
  68. (zsd/format-changes nil data)
  69. "Не смог получить данные. Попробуй перелогинься. /zsd <login> <pass>"))
  70. "Нужен логин-пароль. /zsd <login> <pass>")
  71. :parse-mode "markdown")))
  72. (def-message-cmd-handler handle-cmd-zsd (:zsd)
  73. (cond
  74. ((= 1 (length args))
  75. (zsd/handle-set-cron chat-id (equal "on" (car args))))
  76. ((= 2 (length args)) (apply 'zsd/handle-auth chat-id args))
  77. (:otherwise (zsd/handle-recent chat-id))))
  78. (defvar *zsd/last-results* (make-hash-table) "Last check results")
  79. (defcron process-zsd (:minute '(member 0 10 20 30 40 50))
  80. (dolist (chat-id (lists-get :zsd))
  81. (secret/with (token (list :zsd chat-id))
  82. (if token
  83. (let ((old (gethash chat-id *zsd/last-results*))
  84. (new (zsd/load-data token)))
  85. (when new
  86. (when old
  87. (alexandria:when-let ((changes (zsd/format-changes old new)))
  88. (bot-send-message chat-id changes :parse-mode "markdown")))
  89. (setf (gethash chat-id *zsd/last-results*) new)))
  90. (progn
  91. (log:warn "zsd no token for" chat-id)
  92. ;;(lists-set-entry :zsd chat-id nil)
  93. )))))