neroxbailx
Version:
baileys whatsapp-api
276 lines (257 loc) • 9.94 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true })
/**
* Creates a button message compatible with both WhatsApp and WhatsApp Business
* @param {Object} options Button message options
* @param {string} options.text Main message text
* @param {string} options.footer Footer text (optional)
* @param {Object[]} options.buttons Array of button objects
* @returns {Object} A button message structure compatible with both platforms
*/
const createBusinessCompatibleButtons = (options) => {
const { text, footer = '', buttons = [] } = options
// Ensure each button has the required format
const formattedButtons = buttons.map((btn, index) => {
// Handle different button formats
if (btn.type === "RESPONSE" || btn.type === 1) {
return {
buttonId: btn.buttonId || `btn_${index}_${Math.random().toString(36).substring(2, 8)}`,
buttonText: {
displayText: btn.text || btn.buttonText?.displayText || `Button ${index + 1}`
},
type: 1
}
} else {
// Default format if type is not specified
return {
buttonId: btn.buttonId || `btn_${index}_${Math.random().toString(36).substring(2, 8)}`,
buttonText: {
displayText: btn.text || btn.buttonText?.displayText || `Button ${index + 1}`
},
type: 1
}
}
})
return {
buttonsMessage: {
contentText: text,
footerText: footer,
buttons: formattedButtons,
headerType: 1,
contextInfo: {
isForwarded: false,
forwardingScore: 0,
businessMessageForwardType: 0,
enableWAMessageInteractivity: true
}
}
}
}
/**
* Creates a list message compatible with both WhatsApp and WhatsApp Business
* @param {Object} options List message options
* @param {string} options.text Description text
* @param {string} options.footer Footer text (optional)
* @param {string} options.buttonText Text for the button (optional)
* @param {string} options.title Section title (optional)
* @param {Object[]} options.items Array of list items
* @returns {Object} A list message structure compatible with both platforms
*/
const createBusinessCompatibleList = (options) => {
const {
text,
footer = '',
buttonText = 'Select an option',
title = 'Options',
items = []
} = options
// Ensure each list item has required format
const rows = items.map((item, index) => ({
rowId: item.id || `row_${index}_${Math.random().toString(36).substring(2, 8)}`,
title: item.title || `Item ${index + 1}`,
description: item.description || ''
}))
return {
listMessage: {
description: text,
buttonText: buttonText,
footerText: footer,
sections: [
{
title: title,
rows: rows
}
],
contextInfo: {
isForwarded: false,
forwardingScore: 0,
businessMessageForwardType: 0,
enableWAMessageInteractivity: true
}
}
}
}
/**
* Creates a template message compatible with WhatsApp Business
* @param {Object} options Template message options
* @param {string} options.text Main template text
* @param {string} options.footer Footer text (optional)
* @param {Object[]} options.buttons Array of button objects
* @returns {Object} A template message structure for WhatsApp Business
*/
const createBusinessTemplate = (options) => {
const { text, footer = '', buttons = [] } = options
// Process different button types
const hydratedButtons = buttons.map((btn, index) => {
if (btn.url) {
return {
index,
urlButton: {
displayText: btn.text || "Visit URL",
url: btn.url
}
}
} else if (btn.phoneNumber) {
return {
index,
callButton: {
displayText: btn.text || "Call",
phoneNumber: btn.phoneNumber
}
}
} else {
return {
index,
quickReplyButton: {
displayText: btn.text || `Button ${index + 1}`,
id: btn.id || `btn_${index}_${Math.random().toString(36).substring(2, 8)}`
}
}
}
})
return {
templateMessage: {
hydratedTemplate: {
hydratedContentText: text,
hydratedFooterText: footer,
hydratedButtons: hydratedButtons
},
contextInfo: {
isForwarded: false,
forwardingScore: 0,
businessMessageForwardType: 0,
enableWAMessageInteractivity: true
}
}
}
}
/**
* Creates an image button message compatible with WhatsApp Business
* @param {Object} options Image button message options
* @param {Object} options.image Image object with url, mimetype, etc.
* @param {string} options.text Caption or content text
* @param {string} options.footer Footer text (optional)
* @param {Object[]} options.buttons Array of button objects
* @returns {Object} An image button message structure for WhatsApp Business
*/
const createBusinessImageButtons = (options) => {
const { image, text, footer = '', buttons = [] } = options;
// Format the image message structure if needed
const imageMessage = (typeof image === 'string')
? { url: image, mimetype: 'image/jpeg' } // Simple case with just URL
: image; // Use provided image object
// Ensure each button has the required format
const formattedButtons = buttons.map((btn, index) => ({
buttonId: btn.buttonId || `btn_${index}_${Math.random().toString(36).substring(2, 8)}`,
buttonText: {
displayText: btn.text || btn.buttonText?.displayText || `Button ${index + 1}`
},
type: 1
}));
// For WhatsApp Business, we use a direct structure without viewOnceMessageV2Extension
return {
buttonsMessage: {
imageMessage: imageMessage,
contentText: text,
footerText: footer,
buttons: formattedButtons,
headerType: 4,
contextInfo: {
isForwarded: false,
forwardingScore: 0,
businessMessageForwardType: 0,
enableWAMessageInteractivity: true
}
}
};
};
/**
* Fixes button structures for WhatsApp Business compatibility
* This handles raw RESPONSE type buttons that don't have proper structure
* @param {Object[]} buttons Array of button objects that might be incomplete
* @returns {Object[]} Array of properly formatted button objects
*/
const fixButtonStructure = (buttons = []) => {
return buttons.map((btn, index) => {
// Handle "RESPONSE" type buttons that might be missing required fields
if (btn.type === "RESPONSE" || !btn.buttonText || !btn.buttonId) {
return {
buttonId: btn.buttonId || `btn_${index}_${Math.random().toString(36).substring(2, 8)}`,
buttonText: {
displayText: btn.buttonText?.displayText || btn.text || `Button ${index + 1}`
},
type: 1
};
}
// If button already has correct structure, keep it
return {
...btn,
buttonId: btn.buttonId || `btn_${index}_${Math.random().toString(36).substring(2, 8)}`,
type: btn.type || 1,
buttonText: btn.buttonText || {
displayText: btn.text || `Button ${index + 1}`
}
};
});
};
/**
* Unwraps and fixes nested message structures
* WhatsApp Business sometimes creates nested viewOnceMessageV2Extension objects
* @param {Object} msg The message object that might have nested structures
* @returns {Object} Properly structured message with fixed nesting
*/
const fixNestedMessageStructure = (msg) => {
// Check for nested viewOnceMessageV2Extension
if (msg?.viewOnceMessageV2Extension?.message?.viewOnceMessageV2Extension) {
// Unwrap nested structure
return {
viewOnceMessageV2Extension: {
message: {
messageContextInfo: {
deviceListMetadataVersion: 2,
deviceListMetadata: {},
messageAddOnDurationInSecs: 0
},
// Extract the actual message content from the nested structure
...msg.viewOnceMessageV2Extension.message.viewOnceMessageV2Extension.message,
contextInfo: {
isForwarded: false,
forwardingScore: 0,
businessMessageForwardType: 0,
enableWAMessageInteractivity: true
}
}
}
};
}
// If not nested, return as is
return msg;
};
module.exports = {
createBusinessCompatibleButtons,
createBusinessCompatibleList,
createBusinessTemplate,
createBusinessImageButtons,
fixButtonStructure,
fixNestedMessageStructure
}