1
0

ledger.lisp 3.5 KB

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