summaryrefslogtreecommitdiff
path: root/src/bot
diff options
context:
space:
mode:
authorGravatar Uko Kokņevičs2025-10-09 21:58:43 +0300
committerGravatar Uko Kokņevičs2025-10-09 21:58:43 +0300
commit4da3ad1f569832845b58c3ce35149633a2bb665c (patch)
tree5a09a0de66df7ec2e77f0fc9cc68ccbabc190934 /src/bot
downloadukkoclot-4da3ad1f569832845b58c3ce35149633a2bb665c.tar.gz
ukkoclot-4da3ad1f569832845b58c3ce35149633a2bb665c.tar.xz
ukkoclot-4da3ad1f569832845b58c3ce35149633a2bb665c.zip
Initial commit
Diffstat (limited to 'src/bot')
-rw-r--r--src/bot/impl.lisp96
-rw-r--r--src/bot/methods.lisp88
2 files changed, 184 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)))
diff --git a/src/bot/methods.lisp b/src/bot/methods.lisp
new file mode 100644
index 0000000..b0eca5c
--- /dev/null
+++ b/src/bot/methods.lisp
@@ -0,0 +1,88 @@
1;; SPDX-License-Identifier: EUPL-1.2
2;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com>
3(defpackage :ukkoclot/bot/methods
4 (:use :c2cl :ukkoclot/bot/impl :ukkoclot/tg-types :ukkoclot/tg-types/macros)
5 (:export :answer-callback-query :bot-id :bot-username :delete-message :edit-message-text :get-me :get-updates :send-message :set-my-name))
6(in-package :ukkoclot/bot/methods)
7
8(define-tg-method (answer-callback-query boolean "answerCallbackQuery" #'identity)
9 (callback-query-id string)
10 (text (or string null) nil)
11 (show-alert boolean nil)
12 (url (or string null) nil)
13 (cache-time (or integer null) nil))
14
15(defun bot-id (bot)
16 (or (bot-id% bot)
17 (progn
18 (get-me bot)
19 (bot-id% bot))))
20
21(defun bot-username (bot)
22 (or (bot-username% bot)
23 (progn
24 (get-me bot)
25 (bot-username% bot))))
26
27(define-tg-method (delete-message boolean "deleteMessage" #'identity)
28 (chat-id (or integer string))
29 (message-id integer))
30
31(define-tg-method (edit-message-text message "editMessageText" #'hash->message)
32 (business-connection-id (or string null) nil)
33 (chat-id (or integer string null) nil)
34 (message-id (or integer null) nil)
35 (inline-message-id (or string null) nil)
36 (text string)
37 (parse-mode (or string null) nil)
38 (entities (or (array message-entity) null) nil)
39 (link-preview-options (or link-preview-options null) nil)
40 (reply-markup (or inline-keyboard-markup null) nil))
41
42(define-tg-method (get-me% user "getMe" #'hash->user :GET))
43
44(defun get-me (bot)
45 (let ((res (get-me% bot)))
46 (setf (bot-id% bot) (user-id res))
47 (setf (bot-username% bot) (user-username res))
48 res))
49
50(define-tg-method (get-my-name bot-name "getMyName" #'hash->bot-name :GET)
51 (language-code (or string null) nil))
52
53(define-tg-method (get-updates (array update) "getUpdates" #'parse-update-array)
54 (offset (or integer null) nil)
55 (limit (or integer null) nil)
56 (timeout (or integer null) nil)
57 (allowed-updates (or string null) nil))
58
59(define-tg-method (send-message message "sendMessage" #'hash->message)
60 (business-connection-id (or string null) nil)
61 (chat-id (or integer string))
62 (message-thread-id (or integer null) nil)
63 (text string)
64 ;; TODO: parse-mode should maybe be keywords?
65 (parse-mode (or string null) nil)
66 (entities (or (array message-entity) null) nil)
67 (link-preview-options (or link-preview-options null) nil)
68 (disable-notification (or boolean null) nil)
69 (protect-content (or boolean null) nil)
70 (message-effect-id (or string null) nil)
71 (reply-parameters (or reply-parameters null) nil)
72 (reply-markup (or inline-keyboard-markup
73 ;; TODO: reply-keyboard-markup
74 ;; TODO: reply-keyboard-remove
75 force-reply null) nil))
76
77(define-tg-method (set-my-name% boolean "setMyName" #'identity)
78 (name (or string null) nil)
79 (language-code (or string null) nil))
80
81(defun set-my-name (bot &key (name nil) (language-code nil))
82 (block nil
83 (when name
84 (let ((curr-name (get-my-name bot :language-code language-code)))
85 (when (string= name (bot-name-name curr-name))
86 (return))))
87 (unless (set-my-name% bot :name name :language-code language-code)
88 (error "Failed to set name"))))