zsd.lisp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "ZSD balance: *~A*~%~%~{~A~^~%~}"
  46. (aget "remainder" (car (aget "contract" new)))
  47. (loop for item in wall-diff
  48. collect (%zsd/format-wall item (aget "pan" new)))))))
  49. (def-message-cmd-handler handle-cmd-zsd (:zsd)
  50. (secret/with (token `(:zsd ,chat-id))
  51. (if token
  52. (send-response chat-id (zsd/format-changes nil (zsd/load-data token)))
  53. (send-response chat-id "/zsd-auth"))))
  54. (def-message-cmd-handler handle-cmd-zsd-auth (:zsd-auth)
  55. (destructuring-bind (login password) args
  56. (if (and login password)
  57. (let ((token (zsd/auth login password)))
  58. (if token
  59. (progn
  60. (secret/set `(:zsd ,chat-id) token)
  61. (send-response chat-id (zsd/format-changes nil (zsd/load-data token))))
  62. (send-response chat-id "Can't auth")))
  63. (send-response chat-id "Usage: /zsd-auth <username> <password>"))))