cl-telegram-bot - Telegram Bot API
CL-TELEGRAM-BOT ASDF System Details
Description: Telegram Bot
API
, based on sovietspaceship's work but mostly rewritten.Licence:
MIT
Author: Alexander Artemenko <svetlyak.40wt@gmail.com>
Homepage: https://40ants.com/cl-telegram-bot/
Bug tracker: https://github.com/40ants/cl-telegram-bot/issues
Source control: GIT
Depends on: alexandria, anaphora, arrows, bordeaux-threads, cl-ppcre, cl-strings, closer-mop, dexador, jonathan, kebab, log4cl, serapeum, str, trivial-backtrace, yason
Installation
You can install this library from Quicklisp, but you want to receive updates quickly, then install it from Ultralisp.org:
(ql-dist:install-dist "http://dist.ultralisp.org/"
:prompt nil)
(ql:quickload :cl-telegram-bot)
Quickstart
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:
API
CL-TELEGRAM-BOT/BOT
Classes
BOT
Readers
This slot will be filled with cl-telegram-bot/user:user
object on first access using a call to cl-telegram-bot/user:get-me
function.
When debug mode is T, then interactive debugger will be called on each error.
HTTPS
file-endpoint
HTTPS
endpoint
Update id
Command processing code will use this cache to update commands list on the server
when a new method for cl-telegram-bot/entities/command:on-command
generic-function is defined.
This slot is for internal use.
Accessors
When debug mode is T, then interactive debugger will be called on each error.
HTTPS
file-endpoint
Update id
Command processing code will use this cache to update commands list on the server
when a new method for cl-telegram-bot/entities/command:on-command
generic-function is defined.
This slot is for internal use.
Macros
CL-TELEGRAM-BOT/CALLBACK
Classes
CALLBACK
Readers
Generics
Called when user clicks callback button. Should return an instance of callback
class.
Application may override this method to return objects of different callback classes depending on
callback-data string. This way it mab be easier to define more specific methods for
on-callback
generic-function.
Called when user clicks callback button. Second argument is an object of CALLBACK
type.
CL-TELEGRAM-BOT/CHAT
Classes
CHANNEL
CHAT
Readers
GROUP
PRIVATE-CHAT
Readers
SUPER-GROUP
Readers
Generics
Returns a chat associated with object.
Object could be a message, update, callback, etc. Should return an object of chat
class or NIL
.
Some types of updates aren't bound to a chat. In this case a method should return NIL
.
Functions
https://core.telegram.org/bots/api#deletechatphoto
https://core.telegram.org/bots/api#exportchatinvitelink
https://core.telegram.org/bots/api#getchatadministrators
https://core.telegram.org/bots/api#getchat
https://core.telegram.org/bots/api#getchatmember
https://core.telegram.org/bots/api#getchatmemberscount
https://core.telegram.org/bots/api#kickchatmember
https://core.telegram.org/bots/api#leavechat
https://core.telegram.org/bots/api#pinchatmessage
https://core.telegram.org/bots/api#promotechatmember
https://core.telegram.org/bots/api#restrictchatmember
https://core.telegram.org/bots/api#sendchataction
https://core.telegram.org/bots/api#setchatdescription
https://core.telegram.org/bots/api#setchatphoto
https://core.telegram.org/bots/api#setchattitle
https://core.telegram.org/bots/api#unbanchatmember
https://core.telegram.org/bots/api#unpinchatmessage
CL-TELEGRAM-BOT/CORE
Classes
REPLY
Generics
This method will be called for each command. First argument is a keyword. If user input was /save_note, then first argument will be :save-note.
By default, logs call and does nothing.
This method gets called with raw text from the message. By default it does nothing.
Functions
Works like a send-message
, but only when an incoming message is processed.
Automatically sends reply to a chat from where current message came from.
Macros
CL-TELEGRAM-BOT/ENTITIES/COMMAND
Classes
BOT-COMMAND
Readers
Generics
This method will be called for each command. First argument is a keyword. If user input was /save_note, then first argument will be :save-note.
By default, logs call and does nothing.
CL-TELEGRAM-BOT/ENTITIES/GENERIC
Generics
Extendable protocol to support entities of different kinds. First argument is a keyword, denoting a type of the entity. Payload is an object of type `message'. And data is a plist with data, describing the entity.
Functions
CL-TELEGRAM-BOT/ENVELOPE
Classes
CHANNEL-POST
This container wraps cl-telegram-bot/message:message
when somebody sends a message to a channel.
EDITED-CHANNEL-POST
This container wraps cl-telegram-bot/message:message
when somebody edits a message in a channel.
EDITED-MESSAGE
This container wraps cl-telegram-bot/message:message
when user edits a message.
ENVELOPE
This is the container for a message. From the type of container we can understand if this message was sent to a channel or maybe edited, etc.
Readers
Functions
Returns T if current message was posted to a channel.
Returns T if current message is an update for existing message in the channel of group chat.
CL-TELEGRAM-BOT/INLINE-KEYBOARD
Classes
CALLBACK-BUTTON
Readers
INLINE-KEYBOARD-BUTTON
Base class for all inline keyboard buttons.
API
: https://core.telegram.org/bots/api#inlinekeyboardbutton
Readers
INLINE-KEYBOARD
Represents an inline keyboard as specified in API
https://core.telegram.org/bots/api#inlinekeyboardmarkup.
Readers
URL-BUTTON
Readers
Functions
https://core.telegram.org/bots/api#answercallbackquery
Creates a button which will call a callback.
Returns an inline keyboard which can be passed
to cl-telegram-bot/response:reply
(1
2
) as REPLY-MARKUP
argument.
Each row should be a list of inline-keyboard-button
objects or a single
object of this class. In latter case, such row will have only one button.
Creates a button which will open an url.
CL-TELEGRAM-BOT/MARKUP
Generics
Transforms object into markup of Telegram API
.
Methods of this class should return a hash-table, representing OBJ
in terms of Telegram API
.
CL-TELEGRAM-BOT/MESSAGE
Classes
ANIMATION-MESSAGE
ANIMATION
AUDIO-MESSAGE
AUDIO
Readers
Performer of the audio as defined by sender or by audio tags.
DOCUMENT-MESSAGE
DOCUMENT
FILE-MESSAGE
Readers
FILE
Readers
Identifier for this file, which can be used to download or reuse the file.
Original filename as defined by sender.
File size in bytes.
Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file.
MIME
type of the file as defined by sender.
MESSAGE
Readers
Caption for the animation, audio, document, photo, video or voice.
For forwarded messages, sender of the original message.
For messages forwarded from channels or from anonymous administrators, information about the original sender chat.
For forwarded messages, sender of the original message.
Sender of the message, sent on behalf of a chat. For example, the channel itself for channel posts, the supergroup itself for messages from anonymous group administrators, the linked channel for messages automatically forwarded to the discussion group.
PHOTO-MESSAGE
Readers
PHOTO
REPLY
Readers
SPATIAL
Readers
File height as defined by sender.
STICKER-MESSAGE
STICKER
Readers
True if the sticker is animated.
True if the sticker is a video sticker.
Name of the sticker set to which the sticker belongs.
TEMPORAL
Readers
Duration of the file in seconds as defined by sender.
UNISPATIAL
Readers
VIDEO-MESSAGE
VIDEO-NOTE-MESSAGE
VIDEO-NOTE
VIDEO
VOICE-MESSAGE
VOICE
Generics
This method gets called with raw text from the message. By default it does nothing.
Sends animation to a chat.
A function to send sticker.
Functions
https://core.telegram.org/bots/api#deletemessage
https://core.telegram.org/bots/api#forwardmessage
Returns a bot to which message was addressed.
Returns a chat where currently processing message was received.
Returns currently processed message.
https://core.telegram.org/bots/api#sendmessage
CL-TELEGRAM-BOT/NETWORK
Classes
REQUEST-ERROR
Readers
Functions
Perform HTTP
request to 'name API
method with 'options JSON
-encoded object.
CL-TELEGRAM-BOT/PAYMENTS
Generics
Called when user enters payment method credentials and hit "Pay" button. Second argument is an object of PRE-CHECKOUT-QUERY
type.
A method should respond with with a call to answer-pre-checkout-query
function.
Functions
If ERROR-MESSAGE
argument was given, then response considered is not OK
and transaction will be cancelled.
https://core.telegram.org/bots/api#answerprecheckoutquery
https://core.telegram.org/bots/api#answershippingquery
https://core.telegram.org/bots/api#sendinvoice
CL-TELEGRAM-BOT/PIPELINE
Generics
This method is called by when processing a single update. It is called multiple times on different parts of an update. Whole pipeline looks like that:
For each update we call: process(update) process(update.payload) For each entity in payload: process(entity)
CL-TELEGRAM-BOT/RESPONSE
Classes
ALERT
NOTIFY
OPEN-URL
Readers
REPLY
RESPONSE-WITH-TEXT
Readers
RESPONSE
Readers
Functions
Works like a send-message
, but only when an incoming message is processed.
Automatically sends reply to a chat from where current message came from.
Works like a send-message
, but only when an incoming message is processed.
Automatically sends reply to a chat from where current message came from.
Works like a send-message
, but only when an incoming message is processed.
Automatically sends reply to a chat from where current message came from.
Works like a send-message
, but only when an incoming message is processed.
Automatically sends reply to a chat from where current message came from.
CL-TELEGRAM-BOT/RESPONSE-PROCESSING
Classes
INTERRUPT-PROCESSING
Generics
Processes immediate responses of different types.
Functions
CL-TELEGRAM-BOT/UPDATE
Classes
UPDATE
Readers
Generics
By default, this method starts an infinite loop and fetching new updates using long polling.
Functions
CL-TELEGRAM-BOT/USER
Classes
USER
Readers
Generics
Returns a user
object related to the object.
If object is not bound to a user, then NIL
should be returned.
Functions
CL-TELEGRAM-BOT/UTILS
Functions
Transforms a plist with keys like :|foo_bar| into a plist with keys like :foo-bar.
This can be useful to pass data into CL
object contructors.
Credits
Rei – initial version.
Alexander Artemenko – large refactoring, usage of
CLOS
classes, etc.