-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored payments example to use states and actions.
- Loading branch information
1 parent
98b3ba3
commit d6cb7d2
Showing
11 changed files
with
338 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,34 @@ | ||
(uiop:define-package #:cl-telegram-bot2/action | ||
(:use #:cl) | ||
(:export #:action)) | ||
(:export #:action | ||
#:call-if-action)) | ||
(in-package #:cl-telegram-bot2/action) | ||
|
||
|
||
(defclass action () | ||
()) | ||
|
||
|
||
|
||
(defun call-if-action (obj func &rest args) | ||
"Useful in CL-TELEGRAM-BOT2/GENERICS:PROCESS handlers in case if | ||
state has additional handler stored in the slot and this | ||
slot can be either state or action. | ||
This function is recursive, because processing of an action | ||
could return another action and we should call FUNC until | ||
a new state or NIL will be returned." | ||
(typecase obj | ||
(list | ||
;; Some handlers may represent a list of actions | ||
;; and states, thus we need to call FUNC | ||
;; while a non-nil and non-action object will be returned. | ||
(loop for item in obj | ||
thereis (apply #'call-if-action | ||
item func args))) | ||
(action | ||
(apply #'call-if-action | ||
(apply func obj args) | ||
args)) | ||
(t | ||
obj))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
(uiop:define-package #:cl-telegram-bot2/actions/send-invoice | ||
(:use #:cl) | ||
(:import-from #:cl-telegram-bot2/action | ||
#:action) | ||
(:import-from #:cl-telegram-bot2/generics | ||
#:on-result | ||
#:process | ||
#:on-state-activation) | ||
(:import-from #:cl-telegram-bot2/high | ||
#:reply) | ||
(:import-from #:serapeum | ||
#:soft-list-of | ||
#:->) | ||
(:import-from #:cl-telegram-bot2/utils | ||
#:call-if-needed) | ||
(:import-from #:cl-telegram-bot2/workflow | ||
#:workflow-blocks | ||
#:workflow-block) | ||
(:import-from #:cl-telegram-bot2/states/wait-for-payment | ||
#:wait-for-payment) | ||
(:import-from #:cl-telegram-bot2/state-with-commands | ||
#:command) | ||
(:export #:send-invoice)) | ||
(in-package #:cl-telegram-bot2/actions/send-invoice) | ||
|
||
|
||
(deftype prices-list () | ||
'(soft-list-of hash-table)) | ||
|
||
|
||
(defclass send-invoice (action) | ||
((title :initarg :title | ||
:type (or string | ||
symbol) | ||
:reader title) | ||
(description :initarg :description | ||
:type (or string | ||
symbol) | ||
:reader description) | ||
(payload :initarg :payload | ||
:type (or string | ||
symbol) | ||
:reader payload) | ||
(provider-token :initarg :provider-token | ||
:type (or string | ||
symbol) | ||
:reader provider-token) | ||
(currency :initarg :currency | ||
:type (or string | ||
symbol) | ||
:reader currency) | ||
(prices :initarg :prices | ||
:type (or prices-list | ||
symbol) | ||
:reader prices) | ||
(on-success :initarg :on-success | ||
:type (or workflow-block | ||
workflow-blocks | ||
symbol) | ||
:reader on-success) | ||
(commands :initarg :commands | ||
:initform nil | ||
:type (soft-list-of command) | ||
:reader commands))) | ||
|
||
|
||
(-> send-invoice ((or string symbol) | ||
(or string symbol) | ||
(or string symbol) | ||
(or string symbol) | ||
(or string symbol) | ||
(or prices-list symbol) | ||
&key | ||
(:on-success (or workflow-block | ||
workflow-blocks | ||
symbol)) | ||
(:commands (soft-list-of command))) | ||
(values send-invoice &optional)) | ||
|
||
(defun send-invoice (title description payload provider-token currency prices &key on-success commands) | ||
(make-instance 'send-invoice | ||
:title title | ||
:description description | ||
:payload payload | ||
:provider-token provider-token | ||
:currency currency | ||
:prices prices | ||
:on-success on-success | ||
:commands commands)) | ||
|
||
|
||
(defmethod print-object ((obj send-invoice) stream) | ||
(print-unreadable-object (obj stream :type t) | ||
(format stream "~S" | ||
(title obj)))) | ||
|
||
|
||
(-> perform-action (send-invoice) | ||
(values wait-for-payment &optional)) | ||
|
||
(defun perform-action (action) | ||
(cl-telegram-bot2/api::send-invoice | ||
(cl-telegram-bot2/api::chat-id cl-telegram-bot2/vars::*current-chat*) | ||
;; title | ||
(call-if-needed | ||
(title action)) | ||
;; description | ||
(call-if-needed | ||
(description action)) | ||
;; payload | ||
(call-if-needed | ||
(payload action)) | ||
;; provider token | ||
(call-if-needed | ||
(provider-token action)) | ||
;; currency | ||
(call-if-needed | ||
(currency action)) | ||
;; prices | ||
(call-if-needed | ||
(prices action))) | ||
|
||
(wait-for-payment :on-success (on-success action) | ||
:commands (commands action))) | ||
|
||
|
||
(defmethod on-state-activation ((action send-invoice)) | ||
(perform-action action)) | ||
|
||
|
||
(defmethod process ((action send-invoice) update) | ||
(perform-action action)) | ||
|
||
|
||
(defmethod on-result ((action send-invoice) result) | ||
(perform-action action)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.