From b4ab1bc4d1f4979a8a7cd79bceb942769188c07a Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Mon, 13 Oct 2025 05:36:56 +0300 Subject: Move do-call &co to a transport package --- src/bot/impl.lisp | 39 +-------------------------------------- 1 file changed, 1 insertion(+), 38 deletions(-) (limited to 'src/bot/impl.lisp') 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 @@ ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/bot/impl (:use :c2cl :iterate :ukkoclot/config) - (:import-from :anaphora :aand :acond :it) - (:import-from :cl+ssl) - (:import-from :dex) - (:import-from :log) - (:import-from :ukkoclot/serializing :fixup-args :parse-value) - (:import-from :ukkoclot/strings :lisp->snake-case) - (:local-nicknames - (:jzon :com.inuoe.jzon)) (:export - :bot :bot-p :make-bot :do-call + :bot :bot-p :make-bot :bot-config :bot-db :bot-base-uri :bot-power-on :bot-username% :bot-id%)) (in-package :ukkoclot/bot/impl) @@ -29,32 +21,3 @@ "https://api.telegram.org/bot" (config-bot-token config) "/"))) (make-bot% :config config :db db :base-uri base-uri))) - -(defun req (uri method content) - (let ((retrier (dex:retry-request 5 :interval 1))) - (handler-case (dex:request uri :method method :content content) - (dex:http-request-too-many-requests (e) (dex:ignore-and-continue e)) ; We deal with too many reqs manually - (dex:http-request-failed (e) (funcall retrier e)) - (cl+ssl::ssl-error (e) (funcall retrier e))))) - -(defun do-call% (bot method uri type args-encoded) - (let ((body (req uri method args-encoded))) - (let ((hash (jzon:parse body))) - (acond - ((gethash "ok" hash) (parse-value type (gethash "result" hash))) - ((aand (gethash "parameters" hash) - (gethash "retry_after" it)) - (log:info "Should sleep for ~A seconds" it) - (sleep it) - (log:info "Good morning!") - (do-call% bot method uri type args-encoded)) - (t (error "TG error ~A: ~A ~:A" - (gethash "error_code" hash) - (gethash "description" hash) - (gethash "parameters" hash))))))) - -(defun do-call (bot method path type args) - (let ((uri (concatenate 'string (bot-base-uri bot) path)) - (args-encoded (fixup-args args))) - (log:debug "~A .../~A ~S" method path args-encoded) - (do-call% bot method uri type args-encoded))) -- cgit v1.2.3