1
0

zsd.lisp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. (in-package :cl-user)
  2. (defpackage chatikbot.plugins.zsd
  3. (:use :cl :chatikbot.common))
  4. (in-package :chatikbot.plugins.zsd)
  5. (defparameter +api-uri+ "https://mcabinet.nch-spb.com/onyma/system/api/json")
  6. (defparameter +auth-uri+ "https://mcabinet.nch-spb.com/onyma/system/api/jsonex?function=open_session")
  7. (defparameter +method-prefix+ "onm_api_toll_api_")
  8. ;; poller methods
  9. (defmethod poller-request ((module (eql :zsd)) method &rest params)
  10. (agets (json-request +api-uri+
  11. :parameters (append
  12. `(("function" . ,(concatenate 'string +method-prefix+ method))
  13. ("auth_token" . ,*poller-token*))
  14. (rest-parameters params)))
  15. "return"))
  16. (defmethod poller-validate ((module (eql :zsd)) response)
  17. response)
  18. (defmethod poller-authenticate ((module (eql :zsd)) secret)
  19. (destructuring-bind (username . password) secret
  20. (agets (json-request +auth-uri+ :method :post
  21. :content (plist-json (list :realm "WHSD"
  22. :user username
  23. :pass password)))
  24. "return")))
  25. ;; API
  26. (defun pan ()
  27. (poller-call :zsd "mobile_pan"))
  28. (defun contract ()
  29. (poller-call :zsd "contract_info"))
  30. (defun wall (&optional (offset 0) (limit 5))
  31. (poller-call :zsd "mobile_wall" :rows-skip offset :rows-limit limit))
  32. (defun format-wall (item pans)
  33. (let* ((pan (aget "pan" item))
  34. (alias (aget "alias" (find pan pans :test #'equal :key (lambda (el) (aget "pan" el)))))
  35. (event (parse-integer (aget "event_type" item)))
  36. (amount (aget "amount" item))
  37. (entry (aget "entry_place" item))
  38. (place (aget "place" item))
  39. (cdt (aget "cdt" item)))
  40. (case event
  41. (1 (format nil "~A *~aр.*: 🚗 ~a, _~a → ~a_" cdt amount (or alias pan) entry place))
  42. (101 (format nil "~A *~aр.*: 💶 _~a_" cdt amount place)))))
  43. (defun format-changes (wall-diff contract pans)
  44. (format nil "ЗСД остаток: *~$р.*~%~%~{~A~^~%~}"
  45. (parse-float (aget "remainder" (car contract)))
  46. (loop for item in wall-diff
  47. collect (format-wall item pans))))
  48. (defcron process-zsd (:minute '(member 0 5 10 15 20 25 30 35 40 45 50 55))
  49. (poller-poll-lists :zsd
  50. #'wall
  51. #'(lambda (diff)
  52. (bot-send-message (format-changes diff (contract) (pan))
  53. :parse-mode "markdown"))
  54. :key #'(lambda (w) (local-time:timestamp-to-universal
  55. (local-time:parse-timestring (aget "dt" w))))))
  56. (defun handle-set-cron (enable)
  57. (lists-set-entry :zsd *chat-id* enable)
  58. (bot-send-message (if enable
  59. "Включил рассылку. '/zsd off' чтобы выключить, /zsd - показать последние."
  60. "Без рассылки. '/zsd on' - включить, /zsd - последние.")))
  61. (defun handle-auth (login pass)
  62. (let ((secret (cons login pass)))
  63. (if (poller-authenticate :zsd secret)
  64. (progn
  65. (log:info secret *chat-id*)
  66. (secret-set `(:zsd ,*chat-id*) secret)
  67. (handle-set-cron t))
  68. (bot-send-message "Чот не смог, пропробуй другие."))))
  69. (defun handle-recent ()
  70. (bot-send-message
  71. (handler-case
  72. (format-changes (wall) (contract) (pan))
  73. (poller-error ()
  74. "Нужен логин-пароль. /zsd <login> <pass>"))
  75. :parse-mode "markdown"))
  76. (def-message-cmd-handler handle-cmd-zsd (:zsd)
  77. (cond
  78. ((= 1 (length *args*))
  79. (handle-set-cron (equal "on" (car *args*))))
  80. ((= 2 (length *args*)) (apply 'handle-auth *args*))
  81. (:otherwise (handle-recent))))