diff --git a/src/Types/Message.ts b/src/Types/Message.ts index 31fb3b95..74f44a55 100644 --- a/src/Types/Message.ts +++ b/src/Types/Message.ts @@ -632,23 +632,23 @@ export type ProductCarouselCard = { * ```typescript * await sock.sendMessage(jid, { * productCarousel: { - * catalogId: '123456789', + * businessOwnerJid: '5511999999999@s.whatsapp.net', * products: [ * { productId: 'iphone_15' }, * { productId: 'macbook_air' }, * { productId: 'apple_watch' } - * ] - * }, - * body: 'Check out our featured products!' + * ], + * body: 'Check out our featured products!' + * } * }) * ``` */ export type ProductCarouselMessageOptions = { - /** Catalog ID from WhatsApp Business */ - catalogId: string + /** JID of the business owner (catalog owner) - e.g., '5511999999999@s.whatsapp.net' */ + businessOwnerJid: string /** Products to display (2-10 cards required) */ products: ProductCarouselCard[] - /** Body text for the message */ + /** Body text for the carousel message */ body?: string } @@ -795,19 +795,18 @@ export type AnyRegularMessageContent = ( * ```typescript * await sock.sendMessage(jid, { * productCarousel: { - * catalogId: '123456789', + * businessOwnerJid: '5511999999999@s.whatsapp.net', * products: [ * { productId: 'produto_001' }, * { productId: 'produto_002' }, * { productId: 'produto_003' } - * ] - * }, - * body: 'Confira nossos produtos em destaque!' + * ], + * body: 'Confira nossos produtos em destaque!' + * } * }) * ``` */ productCarousel: ProductCarouselMessageOptions - body?: string } | { /** diff --git a/src/Utils/messages.ts b/src/Utils/messages.ts index 9a6f9bcd..ef9e44b8 100644 --- a/src/Utils/messages.ts +++ b/src/Utils/messages.ts @@ -888,10 +888,13 @@ export const generateProductListMessage = (options: ProductListMessageOptions): * Generates a product carousel message using products from WhatsApp Business catalog * Uses viewOnceMessage wrapper for better iOS/Android compatibility * + * Each card in the carousel references a product from the business catalog using + * collectionMessage with bizJid (business owner) and id (product ID). + * * @example * ```typescript * const msg = generateProductCarouselMessage({ - * catalogId: '123456789', + * businessOwnerJid: '5511999999999@s.whatsapp.net', * products: [ * { productId: 'produto_001' }, * { productId: 'produto_002' }, @@ -905,10 +908,10 @@ export const generateProductListMessage = (options: ProductListMessageOptions): export const generateProductCarouselMessage = ( options: ProductCarouselMessageOptions ): WAMessageContent => { - const { catalogId, products, body } = options + const { businessOwnerJid, products, body } = options - if (!catalogId || typeof catalogId !== 'string' || catalogId.trim().length === 0) { - throw new Boom('catalogId is required and must be a non-empty string', { statusCode: 400 }) + if (!businessOwnerJid || typeof businessOwnerJid !== 'string' || businessOwnerJid.trim().length === 0) { + throw new Boom('businessOwnerJid is required and must be a non-empty string', { statusCode: 400 }) } if (!products || products.length < 2) { @@ -927,13 +930,16 @@ export const generateProductCarouselMessage = ( } } - // Build cards array with product references - const cards = products.map((product, index) => ({ - card_index: index, - type: 'product' as const, - action: { - product_retailer_id: product.productId, - catalog_id: catalogId + // Normalize business owner JID + const normalizedBizJid = jidNormalizedUser(businessOwnerJid) + + // Build cards array - each card is an IInteractiveMessage with collectionMessage + // collectionMessage references a product from the business catalog + const cards: proto.Message.IInteractiveMessage[] = products.map((product) => ({ + collectionMessage: { + bizJid: normalizedBizJid, + id: product.productId, + messageVersion: 1 } })) @@ -941,7 +947,7 @@ export const generateProductCarouselMessage = ( const interactiveMessage: proto.Message.IInteractiveMessage = { body: { text: body || '' }, carouselMessage: { - cards: cards as any, + cards, messageVersion: 1 } } @@ -1100,9 +1106,9 @@ export const generateWAMessageContent = async ( else if (hasNonNullishProperty(message, 'productCarousel')) { const productCarouselMsg = message as any const productCarouselOptions: ProductCarouselMessageOptions = { - catalogId: productCarouselMsg.productCarousel.catalogId, + businessOwnerJid: productCarouselMsg.productCarousel.businessOwnerJid, products: productCarouselMsg.productCarousel.products, - body: productCarouselMsg.body + body: productCarouselMsg.productCarousel.body } const generated = generateProductCarouselMessage(productCarouselOptions) m.viewOnceMessage = generated.viewOnceMessage