Overview
@kreisler/bot-wa-ts is a powerful TypeScript framework for building WhatsApp bots using the Baileys library. It provides a clean API for handling messages, media, stickers, and more.
Package: @kreisler/bot-wa-tsVersion:
1.0.0License: MIT
Installation
Quick Start
Classes
Constructor
Properties
Timestamp when the bot started (in milliseconds).
Pino logger instance for debugging (set to silent by default).
The Baileys WhatsApp socket connection.
Current connection status (0 = disconnected).
QR code string for WhatsApp authentication.
Map of registered commands with their RegExp patterns.
Folder name for storing authentication credentials.
Methods
WAConnect()
Establishes connection to WhatsApp servers and handles authentication.Promise that resolves when connection is established.
getMeInfo()
Retrieves the bot’s WhatsApp profile information.Bot user information.
sendMsgGroup()
Sends multiple messages to one or more chats with batch processing.WhatsApp JID(s) to send messages to (e.g., “[email protected]”).
Array of message content to send.
Additional message options.
Number of messages to send per batch.
Array of sent message info objects.
sendMsgGroupBatch()
Sends messages in controlled batches with delays to avoid rate limits.WhatsApp JID(s) to send messages to.
Array of message content to send.
Additional message options.
Number of messages per batch.
Array of sent message info objects.
getTextMessage()
Extracts text content and message type from a WhatsApp message.The message object to parse.
Object containing body text and message type.
getMessageBody()
Extracts full message body including quoted messages.The message object to parse.
Object with message body and quoted message body if present.
sendText()
Sends a text message to a chat.WhatsApp JID of the recipient.
Message content to send.
Additional options for the message.
Promise that resolves when message is sent.
imageUrl2Base64()
Converts an image URL to base64 buffer with MIME type validation.URL of the image to download.
Tuple containing the image buffer and MIME type.
buffer2base64()
Converts a buffer to base64 data URL.The buffer to convert.
MIME type of the image (e.g., ‘image/png’).
Base64 data URL string.
stickerGenerator()
Generates a WhatsApp sticker from image data.Image data (path or buffer) to convert to sticker.
WebP sticker buffer.
stickerGeneratorFromPath()
Generates a sticker from an image path or buffer and returns it ready to send.Path to image file or image buffer.
Message content object ready to send as sticker.
getMedia()
Downloads media from a WhatsApp message.The downloadable message object.
Type of media to download (‘image’, ‘video’, ‘audio’, ‘document’, ‘sticker’).
Options for downloading the media.
Buffer containing the downloaded media.
hasOwnProp()
Checks if an object has a nested property using dot notation.The object to check.
Property path in dot notation (e.g., ‘user.profile.name’).
True if the property exists, false otherwise.
getNestedProp()
Retrieves a nested property value from an object using dot notation.The object to retrieve from.
Property path in dot notation.
The property value or undefined if not found.
loadEvents()
Loads all WhatsApp event handlers (connection updates, message upsert, credentials).Promise that resolves when events are loaded.
loadCommands()
Loads all bot commands from the commands directory.Promise that resolves when commands are loaded.
loadHandlers()
Loads error handlers, anti-crash handlers, and command registration.Promise that resolves when handlers are loaded.
getCommands()
Returns all registered commands as an array of tuples.Array of [pattern, command] tuples.
findCommand()
Finds a command that matches the given string.The string to test against command patterns.
Tuple with match status and command if found.
send()
Sends various types of content to a chat.WhatsApp JID of the recipient.
Content to send (text, Media object, or Baileys message content).
Additional message options.
The sent message wrapped in Message class.
Message Class
Wrapper class for WhatsApp messages with helper methods.Constructor
The Whatsapp bot instance.
The raw WhatsApp message data.
Properties
Unique message identifier.
The message sender.
Text content of the message.
Whether this message is a reply to another message.
Whether the message was sent in a group.
Whether the message was sent by the bot.
Whether the message contains media (image, video, document, etc.).
Whether the message is a view-once message.
The type of the message (conversation, imageMessage, etc.).
Methods
send()
Sends a message to the same chat.Content to send.
Additional options.
The sent message info.
reply()
Replies to the message.Content to send as reply.
Additional options.
The sent reply message info.
react()
Reacts to the message with an emoji.Emoji to react with.
The reaction message info.
delete()
Deletes the message.Result of the delete operation.
getChat()
Gets the chat where this message was sent.Chat or Group instance.
downloadMediaV2()
Downloads media from the message with file type detection.Object containing buffer and detected file type.
downloadMedia()
Downloads media and wraps it in a Media class instance.Media instance with buffer and metadata.
readMessage()
Marks the message as read.Promise that resolves when message is marked as read.
getQuotedMsg()
Gets the quoted/replied message if this is a reply.The quoted message or undefined.
getMentions()
Gets all users mentioned in the message.Array of mentioned users.
Media Class
Represents media content with metadata.Constructor
The media buffer.
Properties
The media buffer data.
MIME type (e.g., ‘image/jpeg’).
Size in bytes.
Caption text.
True if media is an image.
True if media is a video.
True if media is audio.
True if media is a document.
Whether the media is view-once.
Static Methods
Media.create()
Creates a Media instance from a file path.Path to the media file.
Media instance.
User Class
Represents a WhatsApp user.Properties
User’s display name.
User’s phone number.
WhatsApp JID.
Country code.
Methods
getProfile()
Retrieves the user’s WhatsApp business profile.Business profile information.
GroupUser Class
Extends User for group chat members.Additional Properties
The group’s JID.
Whether the user is a group admin.
Methods
sendDM()
Sends a direct message to the user.Content to send.
Additional options.
getGroup()
Gets the Group instance for this user’s group.The Group instance.
Chat Class
Represents a WhatsApp chat.Properties
Chat JID.
Methods
send()
Sends a message to the chat.Content to send.
The sent message.
isGroup()
Checks if the chat is a group.True if the chat is a group.
Group Class
Extends Chat for group chats.Additional Properties
Group name.
Group description.
Methods
isAdmin()
Checks if a user is a group admin.User to check.
True if the user is an admin.
Interfaces
CommandImport
Command definition interface.Whether the command is active.
Regular expression pattern for matching.
Command handler function.
ContextMsg
Context passed to command handlers.Raw WhatsApp message.
Wrapped Message instance.
Message text content.
Message type.
Quoted message body if present.
WaMessageTypes
Enum of WhatsApp message types.Usage Examples
Basic Bot Setup
Creating a Command
Handling Media Messages
Creating Stickers
Batch Sending Messages
Checking Nested Properties
Helper Functions
printLog()
Colored console logging utility.Message to log.
Console color.
Dependencies
baileys- WhatsApp Web API librarypino- Fast JSON loggerqrcode-terminal- QR code display in terminalwa-sticker-formatter- WhatsApp sticker creation@kreisler/bot-services- Shared bot services@kreisler/createapi- API utilitiescrypto-js- Cryptographic functionsfile-type- File type detection