1
0

ledger.lisp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. (in-package #:chatikbot)
  2. (ql:quickload :pta-ledger)
  3. (defsetting *ledger/default-timezone* -3 "Default timezone for time display. GMT+3")
  4. (defvar *ledger/chat-journals* (make-hash-table))
  5. (defun ledger/get-hook-url (chat-id url)
  6. (declare (ignore url))
  7. (get-webhook-url "ledger" chat-id (token-hmac (format nil "~A" chat-id))))
  8. (defun ledger/parse-uri (chat-id uri)
  9. (setf (gethash chat-id *ledger/chat-journals*)
  10. (cons (pta-ledger:parse-journal (http-request uri))
  11. (get-universal-time))))
  12. (defun ledger/handle-set-uri (chat-id uri)
  13. (handler-case
  14. (destructuring-bind (journal . ut)
  15. (ledger/parse-uri chat-id uri)
  16. (declare (ignore ut))
  17. (secret/set (list :ledger chat-id) uri)
  18. (send-response chat-id (format nil "Добавил журнал с ~D записями. Веб-хук для обновления: ~A"
  19. (length journal)
  20. (ledger/get-hook-url chat-id uri))))
  21. (pta-ledger:journal-failed (e)
  22. (send-response chat-id (format nil "Не смог спарсить: ~A" e)))
  23. (dex:http-request-failed (e)
  24. (send-response chat-id (format nil "Не смог в урл: ~A" (dex:response-body e))))))
  25. (defun ledger/format-time (universal-time)
  26. (when universal-time
  27. (multiple-value-bind (sec min hour day month year dow dst-p tz)
  28. (decode-universal-time universal-time *ledger/default-timezone*)
  29. (declare (ignore dow dst-p tz))
  30. (format nil "~4,'0D-~2,'0D-~2,'0D ~2,'0D:~2,'0D:~2,'0D"
  31. year month day hour min sec))))
  32. (defun ledger/handle-info (chat-id)
  33. (secret/with (uri (list :ledger chat-id))
  34. (if uri
  35. (destructuring-bind (journal . ut)
  36. (or (gethash chat-id *ledger/chat-journals*)
  37. (ledger/parse-uri chat-id uri))
  38. (send-response chat-id (format nil "Журнал с ~D записями, из ~A, обновлён ~A.~%Веб-хук: ~A"
  39. (length journal)
  40. (quri:render-uri (quri:make-uri :userinfo nil
  41. :defaults uri))
  42. (ledger/format-time ut)
  43. (ledger/get-hook-url chat-id uri))))
  44. (send-response chat-id "Добавь урл журнала, /ledger <url>"))))
  45. (def-message-cmd-handler handler-ledger (:ledger)
  46. (cond
  47. ((= 1 (length args)) (ledger/handle-set-uri chat-id (car args)))
  48. (:otherwise (ledger/handle-info chat-id))))
  49. (defun ledger/handle-balance (chat-id query)
  50. (destructuring-bind (journal . ut)
  51. (gethash chat-id *ledger/chat-journals*)
  52. (if journal
  53. (bot-send-message chat-id (format nil "```~%~A~%```~%Обновлено: ~A"
  54. (pta-ledger:journal-balance journal query)
  55. (ledger/format-time ut))
  56. :parse-mode "markdown")
  57. (secret/with (uri (list :ledger chat-id))
  58. (if uri
  59. (progn (ledger/parse-uri chat-id uri)
  60. (ledger/handle-balance chat-id query))
  61. (send-response chat-id "Добавь урл журнала, /ledger <url>"))))))
  62. (def-message-cmd-handler handler-balance (:balance :bal)
  63. (cond
  64. ((null args) (ledger/handle-balance chat-id "assets"))
  65. (:otherwise (ledger/handle-balance chat-id (spaced args)))))
  66. (def-webhook-handler ledger/handle-webhook ("ledger")
  67. (cond
  68. ((= 2 (length paths))
  69. (destructuring-bind (chat-id hmac) paths
  70. (let ((true-hmac (token-hmac chat-id)))
  71. (if (string= true-hmac hmac)
  72. (secret/with (uri (list :ledger chat-id))
  73. (if uri
  74. (progn (ledger/parse-uri (parse-integer chat-id) uri)
  75. "OK")
  76. "FAIL"))
  77. "FAIL"))))
  78. (:otherwise "FAIL")))