Overview
@kreisler/bot-tl-ts is a TypeScript framework for building Telegram bots with an elegant command system and event handling.
Package: @kreisler/bot-tl-tsVersion:
1.0.0License: MIT
Installation
Quick Start
Classes
ClientBot
ExtendsTelegramBot from node-telegram-bot-api with additional functionality for command management and media handling.
Constructor
The Telegram Bot API token. Defaults to
TELEGRAM_TOKEN_DEV or TELEGRAM_TOKEN_PROD based on NODE_ENV.Configuration options for the Telegram bot.
Properties
Map storing all registered bot commands with their regular expression patterns.
Array for storing slash commands.
Getter that returns the bot instance itself.
Methods
initialize()
Initializes the bot by loading events, handlers, and commands.Returns a promise that resolves when initialization is complete.
sendMediaGroupTenByTen()
Sends media in groups of 10 photos to a chat.The chat ID to send media to.
Array of media to send.
Optional parameters for sending media group.
Array of sent messages.
sendDocumentOnebyOne()
Sends multiple documents one by one to a chat.The chat ID to send documents to.
Array of documents to send.
Optional parameters for sending documents.
Additional file options.
Array of sent messages.
getCommands
Gets all registered commands as an array of tuples.Array of command patterns and their handlers.
findCommand()
Finds a command that matches the given string.The string to match against command patterns.
Tuple with match status and command data if found.
loadEvents()
Loads all event handlers for the bot.Promise that resolves when events are loaded.
loadCommands()
Loads all commands from the commands directory.Promise that resolves when commands are loaded.
loadCommandsSlash()
Loads slash commands and sets them using Telegram’s setMyCommands API.Promise that resolves when slash commands are loaded.
loadHandlers()
Loads all error handlers including anti-crash handlers.Promise that resolves when handlers are loaded.
dynamicImport()
Dynamically imports a module by path.The module path to import.
The imported module.
Interfaces
IClsBot.ICTX
Context object passed to command handlers.The raw Telegram message object.
The wrapped Message instance with helper methods.
IClsBot.IExportCMD
Command definition interface.Whether the command is active and should be loaded.
Regular expression pattern to match the command.
The command handler function.
IClsBot.TSendContent
Union type for sending different content types.Text content to send.
Document to send.
Photo to send.
Video to send.
Sticker to send.
Audio to send.
Message Class
Wrapper class for Telegram messages with helper methods.Constructor
The bot client instance.
The raw Telegram message data.
Properties
The chat ID where the message was sent.
The unique message identifier.
The text content of the message.
Whether this message is a reply to another message.
Whether the message was sent in a group chat.
Whether the message was sent in a channel.
Methods
send()
Sends a message to the same chat. Supports multiple overloads for different content types.content
{ text: string } | { doc: string | Stream | Buffer } | { photo: string | Stream | Buffer } | { video: string | Stream | Buffer } | { sticker: string | Stream | Buffer } | { audio: string | Stream | Buffer }
required
Content to send.
options
TelegramBot.SendMessageOptions | TelegramBot.SendDocumentOptions | TelegramBot.SendPhotoOptions | TelegramBot.SendVideoOptions | TelegramBot.SendStickerOptions | TelegramBot.SendAudioOptions
Options for sending the content.
Additional file options (for media types).
The sent message wrapped in the Message class.
reply()
Replies to the message.Content to send as reply.
Options for the reply.
The sent reply message.
delete()
Deletes the message.Result of the delete operation.
editText()
Edits the message text.New text for the message.
Options for editing the message.
The edited message or true on success.
getQuotedMsg()
Gets the quoted/replied message if this is a reply.The quoted message or undefined.
getData()
Gets the raw Telegram message data.The raw message object.
Types
IClsBot.TImportEvent
Event handler function type.EChatType
Enum for chat types.Regular group chat.
Supergroup chat.
Channel.
Usage Examples
Creating a Simple Command
Handling Media Messages
Sending Multiple Photos
Custom Command with Regex
Error Handling
The bot includes built-in anti-crash handlers that catch unhandled exceptions and rejections:Environment Variables
Telegram bot token for development environment.
Telegram bot token for production environment.
Environment mode.
Dependencies
node-telegram-bot-api- Telegram Bot API wrapper@kreisler/bot-services- Shared bot services@kreisler/createapi- API request utilities@kreisler/debounce- Debounce utilities@kreisler/js-helpers- JavaScript helper functionsfile-type- File type detection