Skip to content
MessageBuilder
Documentation menu
Toolkit

Toolkit

Static utilities for media, inline entities, uploads, and MP4 metadata.

API reference
v4.8/Baileys runtime
On this page

Overview

Toolkit does not need instantiation. Every method is called on the class and holds no state. Builders use it internally, but you can call it directly when you need more control.

Media

resolve, resize, upload

Text

hyperlinks, citations, latex

Video

duration, thumbnails

extractIE

static
Toolkit.extractIE(text, options)

Parses a custom markdown dialect to extract inline entities: hyperlinks, citations, and LaTeX images. Used internally by addText and addTable.

Parameter

ParameterTypeDefaultDescription
textrequiredstring—Source text.
options.extractbooleantrueWhen false, return the text unchanged without parsing.
options.hyperlinkbooleantrueEnable [label](url) parsing.
options.citationbooleantrueEnable [](url) parsing.
options.latexbooleantrueEnable [text|w|h|fh|pad]<url> parsing.
Return{ text: string, ie: object[], inline_entities: object[] }
  • Prefix a URL with ! to mark an untrusted hyperlink: [Name](!https://url).
  • Citations use an empty label: [](https://url).
example
const result = Toolkit.extractIE(
  'Read the [documentation](https://example.com) and [](https://source.com)'
);

console.log(result.text); // text with placeholders
console.log(result.inline_entities);

resize

asyncstatic
Toolkit.resize(buffer, x, y, fit)

Resizes an image buffer using sharp. Output is always PNG with a transparent background and center crop.

Parameter

ParameterTypeDefaultDescription
bufferrequiredBuffer—Source image buffer.
xrequirednumber—Target width.
yrequirednumber—Target height.
fitstring'cover'Sharp fit mode: cover, contain, fill, inside, outside.
ReturnPromise<Buffer>

waitAllPromises

asyncstatic
Toolkit.waitAllPromises(input)

Recursively resolves promises nested inside objects and arrays. Used during build to await resolveMedia results.

Parameter

ParameterTypeDefaultDescription
inputrequiredany—A value that may contain nested promises.
ReturnPromise<any>

fetchBuffer

asyncstatic
Toolkit.fetchBuffer(url, options, config)

Downloads URL content into a Buffer using native fetch.

Parameter

ParameterTypeDefaultDescription
urlrequiredstring—Source URL.
optionsobject{}Native fetch options.
config.silentbooleantrueWhen false, errors are thrown; when true, an empty buffer is returned.
ReturnPromise<Buffer>

toUrl

asyncstatic
Toolkit.toUrl(_client, path, mediaType)

Uploads a buffer or path to WhatsApp servers through prepareWAMessageMedia and returns the CDN URL.

Parameter

ParameterTypeDefaultDescription
_clientrequiredWASocket—Baileys socket.
pathrequiredstring | Buffer—Media source.
mediaTypestring'document'image, video, audio, document, or sticker.
ReturnPromise<string>

resolveMedia

asyncstatic
Toolkit.resolveMedia(_client, media, mediaType, options)

The core media normalizer: accepts a URL, Buffer, base64, or array; reuploads when requested; and returns a URL, buffer, or base64 value.

Parameter

ParameterTypeDefaultDescription
_clientrequiredWASocket—Baileys socket.
mediarequiredstring | Buffer | Array—Source media.
mediaTypestring'image'Media type.
options.resolveUrlbooleanfalseForce fetch and reupload for non-WhatsApp URLs.
options.resolveWAUrlbooleanfalseForce download and reupload for *.whatsapp.net URLs.
options.resultstring'url'url, buffer, or base64.
options.resizebooleanfalseResize before returning (requires result other than url).
options.widthnumber300Resize width.
options.heightnumber300Resize height.
ReturnPromise<string | Buffer | Array>
  • Arrays are processed in parallel with Promise.all, recursively.
  • *.whatsapp.net URLs are used directly unless resolveWAUrl is true.
  • Buffers are uploaded through toUrl unless result is buffer or base64.

getMp4Duration

static
Toolkit.getMp4Duration(buffer, options)

Reads video duration from the MP4 atom structure (moov → mvhd) without external dependencies.

Parameter

ParameterTypeDefaultDescription
bufferrequiredBuffer—MP4 video buffer.
options.silentbooleantrueWhen false, parsing errors are thrown; when true, return 0.
Returnnumber

getMp4Preview

asyncstatic
Toolkit.getMp4Preview(videoBuffer, options)

Extracts one video frame as a PNG thumbnail using fluent-ffmpeg.

Parameter

ParameterTypeDefaultDescription
videoBufferrequiredBuffer—Video buffer.
options.timenumber20% of duration (max 10s)Frame capture time.
options.resultstring'buffer'buffer or base64.
options.resizebooleantrueResize the result.
options.widthnumber300Target width.
options.heightnumber300Target height.
options.silentbooleantrueIgnore errors and return null.
ReturnPromise<Buffer | string>

stringifyEscaped

static
Toolkit.stringifyEscaped(obj)

JSON.stringify with unicode escaping for every non-ASCII character. Used when building the AIRich unifiedResponse payload.

Parameter

ParameterTypeDefaultDescription
objrequiredobject—Object to serialize.
Returnstring

Media example

An explicit flow from a source URL to a message header.

media.js
javascript
import { Toolkit } from './lib/MessageBuilder.js';

// 1. Fetch an image from a URL
const buffer = await Toolkit.fetchBuffer('https://example.com/cover.jpg');

// 2. Resize to 300x300 PNG
const resized = await Toolkit.resize(buffer, 300, 300, 'cover');

// 3. Upload to WhatsApp servers
const url = await Toolkit.toUrl(sock, resized, 'image');

// 4. Use it in a message
await new Button(sock)
  .setImage(url)
  .setBody('New cover')
  .addReply('OK', 'ok')
  .send(jid);

Read video metadata without uploading again:

video.js
javascript
const duration = Toolkit.getMp4Duration(videoBuffer); // seconds

const thumb = await Toolkit.getMp4Preview(videoBuffer, {
  time: Math.min(duration * 0.2, 10),
  width: 300,
  height: 300,
  result: 'buffer',
});

Notes

Static API

Every Toolkit method is called on the class, not an instance. There is no internal state.

sharp and ffmpeg

resize requires sharp; getMp4Preview requires fluent-ffmpeg. Make sure both are installed in your bot runtime.

Network failures

fetchBuffer is silent by default: network errors return an empty buffer. Set { silent: false } when you need to catch errors.