Message utility functions help you work with WhatsApp messages, including generating message content, extracting URLs, normalizing messages, and creating forwarded messages.
generateMessageIDV2
Generates a unique message ID v2 compatible with WhatsApp’s format.
export const generateMessageIDV2 = (userId?: string): string
The user’s JID to include in the message ID generation
A unique message ID in the format 3EB0 followed by 18 hex characters
Example:
import { generateMessageIDV2 } from '@whiskeysockets/baileys'
const messageId = generateMessageIDV2()
console.log(messageId) // "3EB01A2B3C4D5E6F7G8H9I"
// With user ID
const messageIdWithUser = generateMessageIDV2('[email protected]')
When to use:
- When you need to generate a custom message ID
- For creating messages programmatically
- The ID includes timestamp and random data for uniqueness
getContentType
Extracts the content type key from a message object.
export const getContentType = (
content: proto.IMessage | undefined
): keyof proto.IMessage | undefined
content
proto.IMessage | undefined
The message content object to analyze
return
keyof proto.IMessage | undefined
The message content type (e.g., ‘conversation’, ‘imageMessage’, ‘videoMessage’)
Example:
import { getContentType } from '@whiskeysockets/baileys'
const message = {
conversation: 'Hello World'
}
const contentType = getContentType(message)
console.log(contentType) // "conversation"
const imageMessage = {
imageMessage: {
url: 'https://...',
mimetype: 'image/jpeg'
}
}
const imageType = getContentType(imageMessage)
console.log(imageType) // "imageMessage"
When to use:
- To determine what type of message you’re dealing with
- Before processing message content
- For routing messages based on their type
Extracts the first URL found in a text string using regex.
export const extractUrlFromText = (text: string): string | undefined
The text string to search for URLs
The first URL found in the text, or undefined if no URL is present
Example:
import { extractUrlFromText } from '@whiskeysockets/baileys'
const text1 = 'Check out https://google.com for more info'
const url1 = extractUrlFromText(text1)
console.log(url1) // "https://google.com"
const text2 = 'No links here'
const url2 = extractUrlFromText(text2)
console.log(url2) // undefined
When to use:
- To detect if a message contains a URL
- Before generating link previews
- For URL validation or filtering
generateLinkPreviewIfRequired
Generates link preview metadata for a URL in the text if a getUrlInfo function is provided.
export const generateLinkPreviewIfRequired = async (
text: string,
getUrlInfo: MessageGenerationOptions['getUrlInfo'],
logger: MessageGenerationOptions['logger']
): Promise<WAUrlInfo | undefined>
The message text potentially containing a URL
getUrlInfo
(url: string) => Promise<WAUrlInfo>
Function to fetch URL metadata (title, description, thumbnail)
Logger instance for error tracking
Link preview information including title, description, and thumbnail
Example:
import { generateLinkPreviewIfRequired } from '@whiskeysockets/baileys'
const text = 'Check out https://github.com'
const getUrlInfo = async (url) => {
// Fetch URL metadata
return {
'matched-text': url,
title: 'GitHub',
description: 'Where the world builds software',
jpegThumbnail: Buffer.from('...')
}
}
const preview = await generateLinkPreviewIfRequired(text, getUrlInfo, logger)
When to use:
- To automatically generate link previews in messages
- When sending text messages with URLs
- For rich message formatting
normalizeMessageContent
Normalizes ephemeral, view once, and other wrapped messages to their core content.
export const normalizeMessageContent = (
content: WAMessageContent | null | undefined
): WAMessageContent | undefined
content
WAMessageContent | null | undefined
The message content to normalize
return
WAMessageContent | undefined
The normalized message content without wrappers
Example:
import { normalizeMessageContent } from '@whiskeysockets/baileys'
// Ephemeral message wrapper
const ephemeralMsg = {
ephemeralMessage: {
message: {
conversation: 'This disappears'
}
}
}
const normalized = normalizeMessageContent(ephemeralMsg)
console.log(normalized)
// { conversation: 'This disappears' }
// View once message
const viewOnceMsg = {
viewOnceMessage: {
message: {
imageMessage: { url: '...', mimetype: 'image/jpeg' }
}
}
}
const normalizedImage = normalizeMessageContent(viewOnceMsg)
console.log(normalizedImage)
// { imageMessage: { url: '...', mimetype: 'image/jpeg' } }
When to use:
- To extract the actual message content from wrapped messages
- Before processing or displaying message content
- When you need the core message regardless of ephemeral/view-once status
generateForwardMessageContent
Generates message content for forwarding a message, maintaining the forward chain.
export const generateForwardMessageContent = (
message: WAMessage,
forceForward?: boolean
): WAMessageContent
Whether to show the message as forwarded even if it’s from you
Message content with forwarding metadata and score updated
Example:
import { generateForwardMessageContent } from '@whiskeysockets/baileys'
// Get the original message
const originalMessage = {
key: { remoteJid: '[email protected]', fromMe: false, id: 'ABC' },
message: { conversation: 'Hello!' }
}
// Generate forward content
const forwardContent = generateForwardMessageContent(originalMessage)
// Send the forwarded message
await sock.sendMessage(jid, forwardContent)
// Force forward even if message is from you
const forceForwardContent = generateForwardMessageContent(
originalMessage,
true
)
When to use:
- When implementing message forwarding
- To maintain the forward chain (shows “Forwarded” label)
- Automatically handles forwarding score incrementation
generateWAMessageFromContent
Generates a complete WAMessage object from message content.
export const generateWAMessageFromContent = (
jid: string,
message: WAMessageContent,
options: MessageGenerationOptionsFromContent
): WAMessage
The message content to wrap
options
MessageGenerationOptionsFromContent
required
Options for message generation
Options
Message timestamp (defaults to current time)
Custom message ID (auto-generated if not provided)
Message to quote/reply to
options.ephemeralExpiration
Ephemeral message expiration in seconds
Complete message object ready to be sent
Example:
import { generateWAMessageFromContent, proto } from '@whiskeysockets/baileys'
const jid = '[email protected]'
const messageContent = proto.Message.fromObject({
conversation: 'Hello World'
})
const message = generateWAMessageFromContent(
jid,
messageContent,
{
userJid: '[email protected]',
timestamp: new Date(),
messageId: 'custom-id'
}
)
console.log(message.key.id) // "custom-id"
console.log(message.messageTimestamp) // current timestamp
// With quoted message
const quotedMsg = generateWAMessageFromContent(
jid,
messageContent,
{
userJid: '[email protected]',
quoted: previousMessage // Quote another message
}
)
When to use:
- When you have raw message content and need a complete message object
- For advanced message construction
- When implementing custom message types
Extracts the actual message content from template messages and button messages.
export const extractMessageContent = (
content: WAMessageContent | undefined | null
): WAMessageContent | undefined
content
WAMessageContent | undefined | null
Message content to extract from
return
WAMessageContent | undefined
The extracted core message content
Example:
import { extractMessageContent } from '@whiskeysockets/baileys'
// Button message
const buttonMsg = {
buttonsMessage: {
imageMessage: { url: '...', mimetype: 'image/jpeg' },
contentText: 'Choose an option'
}
}
const extracted = extractMessageContent(buttonMsg)
console.log(extracted)
// { imageMessage: { url: '...', mimetype: 'image/jpeg' } }
// Template message
const templateMsg = {
templateMessage: {
hydratedFourRowTemplate: {
videoMessage: { url: '...', mimetype: 'video/mp4' }
}
}
}
const extractedVideo = extractMessageContent(templateMsg)
console.log(extractedVideo)
// { videoMessage: { url: '...', mimetype: 'video/mp4' } }
When to use:
- To get the media from button/template messages
- Before downloading media from interactive messages
- When processing complex message types
getDevice
Returns the device type based on the message ID format.
export const getDevice = (id: string): 'ios' | 'web' | 'android' | 'desktop' | 'unknown'
The message ID to analyze
return
'ios' | 'web' | 'android' | 'desktop' | 'unknown'
The predicted device type based on ID pattern
Example:
import { getDevice } from '@whiskeysockets/baileys'
const iosId = '3A123456789012345678'
const device1 = getDevice(iosId)
console.log(device1) // "ios"
const webId = '3E12345678901234567890'
const device2 = getDevice(webId)
console.log(device2) // "web"
const androidId = '123456789012345678901'
const device3 = getDevice(androidId)
console.log(device3) // "android"
When to use:
- For analytics on which devices users are messaging from
- To detect message origin
- For debugging purposes