diff options
Diffstat (limited to 'src/bot/impl.lisp')
| -rw-r--r-- | src/bot/impl.lisp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/bot/impl.lisp b/src/bot/impl.lisp new file mode 100644 index 0000000..b57e2d3 --- /dev/null +++ b/src/bot/impl.lisp | |||
| @@ -0,0 +1,96 @@ | |||
| 1 | ;; SPDX-License-Identifier: EUPL-1.2 | ||
| 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> | ||
| 3 | (defpackage :ukkoclot/bot/impl | ||
| 4 | (:use :c2cl :iterate :ukkoclot/config :ukkoclot/log) | ||
| 5 | (:import-from :anaphora :aand :acond :it) | ||
| 6 | (:import-from :dex) | ||
| 7 | (:import-from :ukkoclot/strings :lisp->snake-case) | ||
| 8 | (:local-nicknames | ||
| 9 | (:jzon :com.inuoe.jzon)) | ||
| 10 | (:export | ||
| 11 | :arg-encode :bot :bot-p :make-bot :do-call | ||
| 12 | |||
| 13 | :bot-config :bot-db :bot-base-uri :bot-power-on :bot-username% :bot-id%)) | ||
| 14 | (in-package :ukkoclot/bot/impl) | ||
| 15 | |||
| 16 | (defgeneric will-arg-encode (object) | ||
| 17 | (:documentation "Whether the OBJECT has any transformation done to it by arg-encode") | ||
| 18 | (:method (obj) | ||
| 19 | nil) | ||
| 20 | (:method ((obj cons)) | ||
| 21 | (or (will-arg-encode (car obj)) | ||
| 22 | (will-arg-encode (cdr obj))))) | ||
| 23 | |||
| 24 | (defgeneric arg-encode (object) | ||
| 25 | (:documentation "Encode the OBJECT to a form that our HTTP code is happy about.") | ||
| 26 | (:method (obj) | ||
| 27 | obj) | ||
| 28 | (:method ((obj cons)) | ||
| 29 | (if (not (will-arg-encode obj)) | ||
| 30 | obj | ||
| 31 | (cons (arg-encode (car obj)) | ||
| 32 | (arg-encode (cdr obj)))))) | ||
| 33 | |||
| 34 | (defgeneric fixup-arg (value) | ||
| 35 | (:documentation "Make sure Telegram & QURI & whatever like the arg") | ||
| 36 | (:method (value) | ||
| 37 | (error "Unsupported argument type ~S (~S)" (class-name (class-of value)) value)) | ||
| 38 | (:method ((value null)) | ||
| 39 | value) | ||
| 40 | (:method ((value number)) | ||
| 41 | value) | ||
| 42 | (:method ((value string)) | ||
| 43 | value) | ||
| 44 | (:method ((value hash-table)) | ||
| 45 | (jzon:stringify value))) | ||
| 46 | |||
| 47 | (defstruct (bot (:constructor make-bot%)) | ||
| 48 | (config (error "No value given for config") :read-only t) | ||
| 49 | (db (error "No value given for DB") :read-only t) | ||
| 50 | (base-uri (error "No value given for base-uri") :read-only t) | ||
| 51 | (power-on t :type boolean) | ||
| 52 | (username% nil :type (or string null)) | ||
| 53 | (id% nil :type (or integer null))) | ||
| 54 | |||
| 55 | (defun make-bot (config db) | ||
| 56 | (let ((base-uri (concatenate 'string | ||
| 57 | "https://api.telegram.org/bot" | ||
| 58 | (config-bot-token config) "/"))) | ||
| 59 | (make-bot% :config config :db db :base-uri base-uri))) | ||
| 60 | |||
| 61 | (defun args-plist->alist (args-plist) | ||
| 62 | (iter (for (old-key value) on args-plist by #'cddr) | ||
| 63 | (collect | ||
| 64 | (let ((new-key (string-downcase (lisp->snake-case (symbol-name old-key))))) | ||
| 65 | (cons new-key value))))) | ||
| 66 | |||
| 67 | (defun fixup-args (args-alist) | ||
| 68 | (iter (for (name . value) in args-alist) | ||
| 69 | (collecting (cons name (fixup-arg (arg-encode value)))))) | ||
| 70 | |||
| 71 | (defun req (uri method content) | ||
| 72 | ;; We deal with this manually | ||
| 73 | (handler-bind ((dex:http-request-too-many-requests #'dex:ignore-and-continue)) | ||
| 74 | (dex:request uri :method method :content content))) | ||
| 75 | |||
| 76 | (defun do-call% (bot method uri mapfn args-encoded) | ||
| 77 | (let ((body (req uri method args-encoded))) | ||
| 78 | (let ((hash (jzon:parse body))) | ||
| 79 | (acond | ||
| 80 | ((gethash "ok" hash) (funcall mapfn (gethash "result" hash))) | ||
| 81 | ((aand (gethash "parameters" hash) | ||
| 82 | (gethash "retry_after" it)) | ||
| 83 | (log-info "Should sleep for ~A seconds" it) | ||
| 84 | (sleep it) | ||
| 85 | (log-info "Good morning!") | ||
| 86 | (do-call% bot method uri mapfn args-encoded)) | ||
| 87 | (t (error "TG error ~A: ~A ~:A" | ||
| 88 | (gethash "error_code" hash) | ||
| 89 | (gethash "description" hash) | ||
| 90 | (gethash "parameters" hash))))))) | ||
| 91 | |||
| 92 | (defun do-call (bot method path mapfn args-plist) | ||
| 93 | (let ((uri (concatenate 'string (bot-base-uri bot) path)) | ||
| 94 | (args-encoded (fixup-args (args-plist->alist args-plist)))) | ||
| 95 | (log-debug "~A .../~A ~S" method path args-encoded) | ||
| 96 | (do-call% bot method uri mapfn args-encoded))) | ||