cl-telegram-bot

2023-02-15

Telegram Bot API, based on sovietspaceship's work but mostly rewritten.

Upstream URL

github.com/40ants/cl-telegram-bot

Author

Alexander Artemenko <svetlyak.40wt@gmail.com>

License

MIT
README

Telegram Bot API for Common Lisp

See the Telegram Bot API.

This library uses CLOS to build a processing pipeline.

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:

Credentials

Dependencies (12)

  • alexandria
  • arrows
  • bordeaux-threads
  • closer-mop
  • cl-ppcre
  • cl-strings
  • dexador
  • jonathan
  • kebab
  • log4cl
  • serapeum
  • trivial-backtrace

Dependents (0)

    • GitHub
    • Quicklisp