1
0

ledger.lisp 3.3 KB

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