Skip to content

Commit

Permalink
Support all kinds of keyboard buttons.
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlyak40wt committed Dec 6, 2024
1 parent 9709342 commit 2dc346e
Show file tree
Hide file tree
Showing 8 changed files with 9,576 additions and 6,486 deletions.
23 changes: 12 additions & 11 deletions examples/mini-app.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@
(:import-from #:alexandria
#:assoc-value)
(:import-from #:yason
#:with-output-to-string*))
#:with-output-to-string*)
(:import-from #:cl-telegram-bot2/high/keyboard
#:remove-keyboard
#:open-web-app
#:keyboard))
(in-package #:cl-telegram-bot2-examples/mini-app)


Expand Down Expand Up @@ -146,24 +150,21 @@ We will send the verification link to `~A`."


(let ((keyboard
(list (make-instance 'cl-telegram-bot2/api:keyboard-button
:text "Open Mini App"
:web-app
(make-instance 'cl-telegram-bot2/api:web-app-info
:url "https://cl-echo-bot.dev.40ants.com/")))))
(keyboard (open-web-app "Open Mini App2"
"https://cl-echo-bot.dev.40ants.com/")
:one-time-keyboard t)))
(defbot test-bot ()
()
(:initial-state
(state (send-text "Initial state. Give /app command to open the mini-app or press this button:"
:buttons keyboard
:keyboard-type :main)
:reply-markup keyboard)
:on-web-app-data (list 'save-form-data
(send-text 'format-response-text
:parse-mode "Markdown"))
:parse-mode "Markdown"
:reply-markup (remove-keyboard)))
:on-result (send-text "Welcome back!")
:on-update (send-text "Give /app command to open the mini-app or press this button:"
:buttons keyboard
:keyboard-type :main)
:reply-markup keyboard)
:commands (list
(command "/app"
(send-text "App opening is not implemented yet.")
Expand Down
2 changes: 1 addition & 1 deletion v2/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Command to donwload api spec:


curl 'https://raw.githubusercontent.com/rockneurotiko/telegram_api_json/refs/tags/0.7.2/exports/tg_api_pretty.json' > spec.json
curl 'https://raw.githubusercontent.com/rockneurotiko/telegram_api_json/refs/tags/0.8.0/exports/tg_api_pretty.json' > spec.json
69 changes: 21 additions & 48 deletions v2/actions/send-text.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,51 @@
(:import-from #:cl-telegram-bot2/utils
#:call-if-needed)
(:import-from #:cl-telegram-bot2/types
#:reply-markup-type
#:inline-keyboard-buttons)
(:export #:send-text))
(in-package #:cl-telegram-bot2/actions/send-text)


(defparameter *default-keyboad-type*
:inline)


(defclass send-text (action)
((text :initarg :text
:type (or string
symbol)
:type (or symbol
string)
:reader text)
(buttons :initarg :buttons
:initform nil
:type list
:reader buttons)
(keyboard-type :initarg :keyboard-type
:initform *default-keyboad-type*
:type (member :main :inline)
:reader keyboard-type)
(reply-markup :initarg :reply-markup
:initform nil
:type (or null
symbol
reply-markup-type)
:reader reply-markup)
(parse-mode :initarg :parse-mode
:initform nil
:type (or null string)
:type (or null
symbol
string)
:documentation "Supported values are: \"Markdown\", \"MarkdownV2\" or \"HTML\". Read more about formatting options in the Telegram documentaion: https://core.telegram.org/bots/api#formatting-options"
:reader parse-mode)))


(-> send-text ((or string symbol)
&key
(:buttons inline-keyboard-buttons)
(:keyboard-type (member :main :inline))
(:reply-markup reply-markup-type)
(:parse-mode (or null string)))
(values send-text &optional))

(defun send-text (text-or-func-name &key buttons (keyboard-type *default-keyboad-type*) parse-mode)

(defun send-text (text-or-func-name
&key
reply-markup
parse-mode)
(when (and (symbolp text-or-func-name)
(not (fboundp text-or-func-name)))
(error "SEND-TEXT waits a text or fbound symbol. ~S is not fbound."
text-or-func-name))

(make-instance 'send-text
:text text-or-func-name
:buttons buttons
:keyboard-type keyboard-type
:reply-markup reply-markup
:parse-mode parse-mode))


Expand All @@ -75,41 +74,15 @@
(defun do-action (action)
(let* ((text (call-if-needed
(text action)))
(buttons (call-if-needed (buttons action)))
(parse-mode (call-if-needed (parse-mode action)))
(reply-markup
(when buttons
(ecase (keyboard-type action)
(:inline
(make-instance 'cl-telegram-bot2/api:inline-keyboard-markup
:inline-keyboard
(list
(loop for button in buttons
collect (etypecase button
(cl-telegram-bot2/api:inline-keyboard-button
button)
(string
(make-instance 'cl-telegram-bot2/api:inline-keyboard-button
:text button
:callback-data button)))))))
(:main
(make-instance 'cl-telegram-bot2/api:reply-keyboard-markup
:keyboard
(list
(loop for button in buttons
collect (etypecase button
(cl-telegram-bot2/api:keyboard-button
button)
(string
(make-instance 'cl-telegram-bot2/api:keyboard-button
:text button)))))))))))
(reply-markup (call-if-needed (reply-markup action))))
(apply #'reply
text
(append
(when parse-mode
(list :parse-mode parse-mode))
(when reply-markup
(list :reply-markup reply-markup)))))
(list :reply-markup (reply-markup action))))))
(values))


Expand Down
Loading

0 comments on commit 2dc346e

Please sign in to comment.