See the Telegram Bot API.
This library uses CLOS to build a processing pipeline.
The system uses CLOS to add new methods to process incoming messages.
To create a simple bot, all you need is to define on-message
method.
If you want to match on a particular command, like /help
or /make-me-happy 7 times
,
then you better to define a on-command
method.
During messages processing, function (reply "some text")
is available, which will send
given text into the right chat. Also, there is send-message
and other function exists
which allow your bot to post messages, images and other media into the any chat.
Here is example of a simple bot which reacts on the text message and /echo
command:
CL-USER> (defpackage the-bot (:use :cl :cl-telegram-bot))
#<Package "THE-BOT">
CL-USER> (in-package the-bot)
#<Package "THE-BOT">
THE-BOT> (defbot echo-bot)
MAKE-ECHO-BOT
THE-BOT> (defmethod on-message ((bot echo-bot)
text)
(reply text))
#<STANDARD-METHOD ON-MESSAGE (ECHO-BOT T)>
THE-BOT> (defmethod on-command ((bot echo-bot)
(command (eql :help))
text)
(declare (ignorable text))
(reply "Just send me any text and I'll reply with the same text."))
#<STANDARD-METHOD ON-COMMAND (ECHO-BOT (EQL :HELP) T)>
THE-BOT> (defmethod on-command ((bot echo-bot)
(command (eql :start))
text)
(declare (ignorable text))
(reply "Welcome Lisper! Have a fun, playing with cl-telegram-bot!"))
#<STANDARD-METHOD ON-COMMAND (ECHO-BOT (EQL :START) T)>
Now, stop for the minute, open your Telegram client, and create a new bot using the BotFather bot:
When you've got token, return to the REPL and start our bot:
THE-BOT> (start-processing (make-echo-bot "5205125**********************************")
:debug t)
<INFO> [08:31:09] cl-telegram-bot core.lisp (start-processing) - Starting thread to process updates for CL-TELEGRAM-BOT/CORE::BOT: #<ECHO-BOT id=0>
#<PROCESS telegram-bot(33) [Reset] #x30200709246D>
THE-BOT>
This will start a new thread for processing incoming messages.
Now, find your bot in the Telegram client:
And start communicating with him:
- Rei – initial version.
- Alexander Artemenko – large refactoring, usage of CLOS classes, etc.