cl-telegram-bot - Telegram Bot API

CL-TELEGRAM-BOT ASDF System Details

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

reader
(:API-URI = "https://api.telegram.org/")

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.

reader
(:debug-mode = nil)

When debug mode is T, then interactive debugger will be called on each error.

reader
(:file-endpoint = nil)

HTTPS file-endpoint

HTTPS endpoint

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.

reader
(:token = nil)

Bot token given by BotFather

Accessors

accessor
(:API-URI = "https://api.telegram.org/")
accessor
(:debug-mode = nil)

When debug mode is T, then interactive debugger will be called on each error.

accessor
(:file-endpoint = nil)

HTTPS file-endpoint

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.

accessor
(:token = nil)

Bot token given by BotFather

Macros

macro
name &optional slots options

Use this macro to define a class of your Telegram bot.

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

reader
(:has-protected-content)
reader
(:message-auto-delete-time)
GROUP
PRIVATE-CHAT

Readers

reader
(:has-private-forwards)
SUPER-GROUP

Readers

reader
(:can-set-sticker-set)
reader
(:join-by-request)
reader
(:join-to-send-messages)
reader
(:slow-mode-delay)
reader
(:sticker-set-name)

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

function
bot-var1 chat-id

https://core.telegram.org/bots/api#getchat

function
bot-var1 chat user-id

https://core.telegram.org/bots/api#getchatmember

https://core.telegram.org/bots/api#getchatmemberscount

function
bot-var1 chat user-id until-date

https://core.telegram.org/bots/api#kickchatmember

function
bot-var1 chat

https://core.telegram.org/bots/api#leavechat

function
bot-var1 chat message-id disable-notification

https://core.telegram.org/bots/api#pinchatmessage

function
bot-var1 chat user-id can-change-info can-post-messages can-edit-messages can-delete-messages can-invite-users can-restrict-members can-pin-messages can-promote-members

https://core.telegram.org/bots/api#promotechatmember

function
bot-var1 chat user-id until-date can-send-messages can-send-media-messages can-send-other-messages can-add-web-page-previews

https://core.telegram.org/bots/api#restrictchatmember

function
bot-var1 chat action

https://core.telegram.org/bots/api#sendchataction

function
bot-var1 chat description

https://core.telegram.org/bots/api#setchatdescription

function
bot-var1 chat photo

https://core.telegram.org/bots/api#setchatphoto

function
bot-var1 chat title

https://core.telegram.org/bots/api#setchattitle

function
bot-var1 chat user-id

https://core.telegram.org/bots/api#unbanchatmember

https://core.telegram.org/bots/api#unpinchatmessage

CL-TELEGRAM-BOT/CORE

Classes

REPLY

Generics

generic-function
bot command rest-text

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

function
text &rest args &key parse-mode disable-web-page-preview disable-notification reply-to-message-id reply-markup (immediately \*reply-immediately\*)

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.

function
BOT &KEY DEBUG (DELAY-BETWEEN-RETRIES 10) (THREAD-NAME "telegram-bot")

Macros

macro
name &optional slots options

Use this macro to define a class of your Telegram bot.

CL-TELEGRAM-BOT/ENTITIES/COMMAND

Classes

BOT-COMMAND

Readers

reader
(:bot-username)

Generics

generic-function
bot command rest-text

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

generic-function
entity-type payload data

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

reader
(:rows = nil)
URL-BUTTON

Readers

Functions

function
bot callback &key text show-alert url

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.

Title 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.

reader
(:file-unique-id)

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.

reader
(:entities = nil)
reader
(:forward-from)

For forwarded messages, sender of the original message.

reader
(:forward-from-chat)

For messages forwarded from channels or from anonymous administrators, information about the original sender chat.

reader
(:forward-sender-name)

For forwarded messages, sender of the original message.

reader
(:sender-chat)

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

reader
(:photo-options)
PHOTO
REPLY

Readers

reader
(:reply-to-message)
SPATIAL

Readers

File height as defined by sender.

File width as defined by sender.

STICKER-MESSAGE
STICKER

Readers

Emoji associated with the sticker

reader
(:is-animated)

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.

generic-function
bot chat animation &rest options &key caption parse-mode caption-entities duration width height thumb disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup

Sends animation to a chat.

generic-function
bot chat audio &rest options &key caption parse-mode caption-entities duration performer title thumb disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup
generic-function
bot chat document &rest options &key caption parse-mode caption-entities disable-content-type-detection thumb disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup
generic-function
bot chat photo &rest options &key caption parse-mode caption-entities disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup
generic-function
bot chat sticker &rest options &key disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup

A function to send sticker.

generic-function
bot chat video &rest options &key caption parse-mode caption-entities duration width height thumb disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup
generic-function
bot chat video-note &rest options &key caption parse-mode caption-entities duration length thumb disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup
generic-function
bot chat voice &rest options &key caption parse-mode caption-entities duration disable-notification protect-content reply-to-message-id allow-sending-without-reply reply-markup

Functions

function
bot chat message

https://core.telegram.org/bots/api#deletemessage

function
bot chat from-chat message &key disable-notification

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.

function
bot chat text &rest options &key parse-mode disable-web-page-preview disable-notification reply-to-message-id (autosplit nil) reply-markup

https://core.telegram.org/bots/api#sendmessage

CL-TELEGRAM-BOT/NETWORK

Classes

REQUEST-ERROR

Readers

reader
(:what)

Functions

function
bot name &rest options &key (streamp nil) (timeout 3) &allow-other-keys

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

function
bot pre-checkout-query &key error-message

If ERROR-MESSAGE argument was given, then response considered is not OK and transaction will be cancelled.

https://core.telegram.org/bots/api#answerprecheckoutquery

function
b shipping-query-id ok &key shipping-options error-message

https://core.telegram.org/bots/api#answershippingquery

function
b chat-id title description payload provider-token start-parameter currency prices &key photo-url photo-size photo-width photo-height need-name need-phone-number need-email need-shipping-address is-flexible disable-notification reply-to-message-id reply-markup

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.

function
text &rest args &key parse-mode disable-web-page-preview disable-notification reply-to-message-id reply-markup (immediately \*reply-immediately\*)

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

reader
(:is-bot)
reader
(:can-connect-to-business = nil)
reader
(:can-join-groups = nil)
reader
(:can-read-all-group-messages = nil)
reader
(:first-name)
reader
(:is-premium = nil)
reader
(:language-code = nil)
reader
(:last-name = nil)
reader
(:raw-data)
reader
(:supports-inline-queries = nil)
reader
(:username = nil)

Generics

Returns a user object related to the object.

If object is not bound to a user, then NIL should be returned.

Functions

https://core.telegram.org/bots/api#getme

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.

function
text &key (max-size 4096) (trim-whitespaces-p t)

Credits