summaryrefslogtreecommitdiff
path: root/src/bot/impl.lisp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bot/impl.lisp')
-rw-r--r--src/bot/impl.lisp39
1 files changed, 1 insertions, 38 deletions
diff --git a/src/bot/impl.lisp b/src/bot/impl.lisp
index 93e63f5..ea1aa03 100644
--- a/src/bot/impl.lisp
+++ b/src/bot/impl.lisp
@@ -2,16 +2,8 @@
2;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> 2;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com>
3(defpackage :ukkoclot/bot/impl 3(defpackage :ukkoclot/bot/impl
4 (:use :c2cl :iterate :ukkoclot/config) 4 (:use :c2cl :iterate :ukkoclot/config)
5 (:import-from :anaphora :aand :acond :it)
6 (:import-from :cl+ssl)
7 (:import-from :dex)
8 (:import-from :log)
9 (:import-from :ukkoclot/serializing :fixup-args :parse-value)
10 (:import-from :ukkoclot/strings :lisp->snake-case)
11 (:local-nicknames
12 (:jzon :com.inuoe.jzon))
13 (:export 5 (:export
14 :bot :bot-p :make-bot :do-call 6 :bot :bot-p :make-bot
15 7
16 :bot-config :bot-db :bot-base-uri :bot-power-on :bot-username% :bot-id%)) 8 :bot-config :bot-db :bot-base-uri :bot-power-on :bot-username% :bot-id%))
17(in-package :ukkoclot/bot/impl) 9(in-package :ukkoclot/bot/impl)
@@ -29,32 +21,3 @@
29 "https://api.telegram.org/bot" 21 "https://api.telegram.org/bot"
30 (config-bot-token config) "/"))) 22 (config-bot-token config) "/")))
31 (make-bot% :config config :db db :base-uri base-uri))) 23 (make-bot% :config config :db db :base-uri base-uri)))
32
33(defun req (uri method content)
34 (let ((retrier (dex:retry-request 5 :interval 1)))
35 (handler-case (dex:request uri :method method :content content)
36 (dex:http-request-too-many-requests (e) (dex:ignore-and-continue e)) ; We deal with too many reqs manually
37 (dex:http-request-failed (e) (funcall retrier e))
38 (cl+ssl::ssl-error (e) (funcall retrier e)))))
39
40(defun do-call% (bot method uri type args-encoded)
41 (let ((body (req uri method args-encoded)))
42 (let ((hash (jzon:parse body)))
43 (acond
44 ((gethash "ok" hash) (parse-value type (gethash "result" hash)))
45 ((aand (gethash "parameters" hash)
46 (gethash "retry_after" it))
47 (log:info "Should sleep for ~A seconds" it)
48 (sleep it)
49 (log:info "Good morning!")
50 (do-call% bot method uri type args-encoded))
51 (t (error "TG error ~A: ~A ~:A"
52 (gethash "error_code" hash)
53 (gethash "description" hash)
54 (gethash "parameters" hash)))))))
55
56(defun do-call (bot method path type args)
57 (let ((uri (concatenate 'string (bot-base-uri bot) path))
58 (args-encoded (fixup-args args)))
59 (log:debug "~A .../~A ~S" method path args-encoded)
60 (do-call% bot method uri type args-encoded)))