-
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.
- Loading branch information
1 parent
73feeeb
commit 054cc96
Showing
8 changed files
with
2,251 additions
and
600 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
(uiop:define-package #:cl-telegram-bot-docs/states | ||
(:use #:cl) | ||
(:import-from #:40ants-doc | ||
#:defsection) | ||
(:import-from #:cl-telegram-bot2/state | ||
#:state) | ||
(:import-from #:cl-telegram-bot2/actions/send-text | ||
#:send-text)) | ||
(in-package #:cl-telegram-bot-docs/states) | ||
|
||
|
||
(defsection @states-and-actions (:title "States and Actions") | ||
" | ||
This framework makes it possible to define bot with all allowed state. | ||
The state defines behaviour of the bot, the way it should respond to commands, updates and other events. | ||
" | ||
(@states section) | ||
(@actions section)) | ||
|
||
|
||
(defsection @states (:title "States") | ||
" | ||
There can be more than one handler for the event. We call these handlers \"Actions\". | ||
An action should return a NIL or a new state. In latter case, the current bot's state will be changed to the new one and handlers for `on-activation` event will be called. | ||
State is constructed using STATE function, which accepts handlers for different kinds of events. Here is simples state which greets a user when it start the chat and then reply with the same text: | ||
``` | ||
(defun reply-with-same-text (update) | ||
(reply (message-text | ||
(update-message update))) | ||
(values)) | ||
(state (send-text \"Hello, I'm the echo bot.\") | ||
:on-update 'reply-with-same-text) | ||
``` | ||
The first argument to STATE function is a handler for `on-activation` event. If you don't want to react on activation, you can pass NIL instead. The SEND-TEXT function returns an action instance. This way, we tell what bot should do, we use a declarative way to describe bot's behaviour. | ||
The :ON-UPDATE argument specifies a handler for `on-update` event. This is the most generic event which occur when bot receives an update which wasn't processed by other event handlers. For this handler we are using a custom function bound to the symbol `reply-with-same-text`. The function accepts a single argument - update object. Use generic functions from `cl-telegram-bot2/api` package to work with this update object. | ||
The reason why we only accept a special action object or a symbol but not a lambda function is because this way we'll be able to generate schemas of all states and transitions between them. Another reason is that it will be possible to redefine fbound function and use interactive approach to changing bot's behaviour. | ||
See other support events in STATE function documentation. | ||
") | ||
|
||
|
||
(defsection @actions (:title "Actions") | ||
" | ||
") |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
(uiop:define-package #:cl-telegram-bot2-examples/echo | ||
(:use #:cl) | ||
(:import-from #:cl-telegram-bot2/state | ||
#:state) | ||
(:import-from #:cl-telegram-bot2/actions/send-text | ||
#:send-text) | ||
(:import-from #:cl-telegram-bot2/bot | ||
#:defbot) | ||
(:import-from #:cl-telegram-bot2/server | ||
#:stop-polling | ||
#:start-polling) | ||
(:import-from #:cl-telegram-bot2/high | ||
#:reply | ||
#:chat-state) | ||
(:import-from #:serapeum | ||
#:dict | ||
#:fmt) | ||
(:import-from #:cl-telegram-bot2/pipeline | ||
#:back-to-nth-parent | ||
#:back-to | ||
#:back) | ||
(:import-from #:cl-telegram-bot2/api | ||
#:update-message | ||
#:message-text | ||
#:message-message-id) | ||
(:import-from #:cl-telegram-bot2/states/ask-for-number | ||
#:ask-for-number) | ||
(:import-from #:cl-telegram-bot2/states/base | ||
#:var) | ||
(:import-from #:cl-telegram-bot2/states/ask-for-choice | ||
#:ask-for-choice) | ||
(:import-from #:40ants-logging) | ||
(:import-from #:cl-telegram-bot2/term/back | ||
#:back-to-id)) | ||
(in-package #:cl-telegram-bot2-examples/echo) | ||
|
||
|
||
(defun reply-with-same-text (update) | ||
(reply (message-text | ||
(update-message update))) | ||
(values)) | ||
|
||
|
||
(defbot test-bot () | ||
() | ||
(:initial-state | ||
(state (send-text "Hello, I'm the echo bot.") | ||
:on-update 'reply-with-same-text))) | ||
|
||
|
||
(defvar *bot* nil) | ||
|
||
|
||
(defun stop () | ||
(when *bot* | ||
(stop-polling *bot*) | ||
(setf *bot* nil) | ||
|
||
(sleep 1) | ||
(bt:all-threads))) | ||
|
||
|
||
(defun start () | ||
(stop) | ||
|
||
(40ants-logging:setup-for-repl :level :warn) | ||
|
||
(unless *bot* | ||
(setf *bot* | ||
(make-test-bot (uiop:getenv "TELEGRAM_TOKEN")))) | ||
|
||
(start-polling *bot* :debug t)) | ||
|
||
|
||
(defun clean-threads () | ||
"TODO: надо разобраться почему треды не подчищаются. Возможно это происходит когда случаются ошибки?" | ||
(loop for tr in (bt:all-threads) | ||
when (or (str:starts-with? "message-thread" (bt:thread-name tr)) | ||
(str:starts-with? "timer-wheel" (bt:thread-name tr)) | ||
(str:starts-with? "telegram-bot" (bt:thread-name tr))) | ||
do (bt:destroy-thread tr))) |
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