diff --git a/index.js b/index.js index 47ea124..ab6094c 100644 --- a/index.js +++ b/index.js @@ -243,5 +243,75 @@ app.post('/sendInteractiveImage', async (req, res) => { // res.status(500).json({ error: e.message }); // } // }); +app.post('/sendProductMessage', async (req, res) => { + if (!client || !isConnected) { + return res.status(400).json({ error: 'Non connecté à WhatsApp' }); + } + const { + phone, + productImageUrl, + productImageCount, + productTitle, + productDescription, + priceAmount1000, + currencyCode, + retailerId, + productUrl, + businessOwnerJid, + caption, + messageTitle, + footer, + interactiveButtons = [], + quoted + } = req.body; + + try { + const jid = `${phone}@s.whatsapp.net`; + + // Construire le payload "product" + const productPayload = { + productImage: { url: productImageUrl }, + productImageCount, + title: productTitle, + description: productDescription, + priceAmount1000: priceAmount1000 * 1000, // si vous avez déjà *1000, retirez l’opération + currencyCode, + retailerId, + url: productUrl + }; + + // Transformer vos boutons simples en format Baileys + const buttons = interactiveButtons.map(btn => { + const params = {}; + if (btn.id) params.id = btn.id; + if (btn.url) params.url = btn.url; + if (btn.display_text) params.display_text = btn.display_text; + return { + name: btn.name, + buttonParamsJson: JSON.stringify(params) + }; + }); + + // Envoi du message + await client.sendMessage( + jid, + { + product: productPayload, + businessOwnerJid, + caption, + title: messageTitle, + footer, + media: true, + interactiveButtons: buttons + }, + quoted ? { quoted } : {} + ); + + return res.json({ success: true }); + } catch (e) { + console.error('❌ Erreur /sendProductMessage :', e); + return res.status(500).json({ error: e.message }); + } +}); app.listen(3001, () => console.log('🚀 Serveur Baileys démarré sur http://localhost:3001'));