DDDDD
This commit is contained in:
parent
33e6203d36
commit
6dc36a2039
98
index.js
98
index.js
@ -20,7 +20,9 @@ const initBaileys = async () => {
|
||||
sock = makeWASocket({
|
||||
version,
|
||||
auth: state,
|
||||
printQRInTerminal: false
|
||||
printQRInTerminal: false,
|
||||
logger: { level: 'trace' } // Active les logs détaillés
|
||||
|
||||
});
|
||||
|
||||
sock.ev.on('connection.update', async (update) => {
|
||||
@ -129,73 +131,55 @@ app.post('/sendInteractiveImage', async (req, res) => {
|
||||
}
|
||||
|
||||
try {
|
||||
const BASE_URL = process.env.BASE_URL || 'https://wa.canguidev.fr';
|
||||
const imageUrl = `${BASE_URL}/static/logo-merlo-cs-FR.jpg`;
|
||||
const imagePath = path.join(__dirname, 'public', 'logo-merlo-cs-FR.jpg');
|
||||
const imageBuffer = fs.readFileSync(imagePath);
|
||||
|
||||
// Construire le Header correct
|
||||
const header = proto.Message.InteractiveMessage.create({
|
||||
// ici on injecte l'image dans le header sans besoin de type explicite
|
||||
header: proto.Message.InteractiveMessage.Header.create({
|
||||
type: "image",
|
||||
imageMessage: { url: imageUrl },
|
||||
title: 'Igna',
|
||||
subtitle: 'test'
|
||||
})
|
||||
});
|
||||
// Body et Footer
|
||||
const body = proto.Message.InteractiveMessage.Body.create({
|
||||
text: caption || 'Description par défaut'
|
||||
});
|
||||
const foot = proto.Message.InteractiveMessage.Footer.create({
|
||||
text: footer || 'Pied de page'
|
||||
});
|
||||
// Upload the image first
|
||||
const uploadedImage = await prepareWAMessageMedia(
|
||||
{ image: imageBuffer },
|
||||
{ upload: sock.waUploadToServer }
|
||||
);
|
||||
|
||||
// Vos boutons URL
|
||||
const nativeFlow = proto.Message.InteractiveMessage.NativeFlowMessage.create({
|
||||
const interactiveMessage = {
|
||||
imageMessage: uploadedImage.imageMessage,
|
||||
caption: caption || 'Description par défaut',
|
||||
footer: footer || 'Pied de page',
|
||||
buttons: [
|
||||
{
|
||||
name: 'cta_url',
|
||||
buttonParamsJson: JSON.stringify({
|
||||
display_text: '📄 Voir proposition',
|
||||
url: 'https://merlo-ch.com/uploads/proposition/f_p_250505_0000136_00008_EB00001909.pdf'
|
||||
})
|
||||
buttonId: 'btn1',
|
||||
buttonText: { displayText: '📄 Voir proposition' },
|
||||
type: 1 // URL button
|
||||
},
|
||||
{
|
||||
name: 'cta_url',
|
||||
buttonParamsJson: JSON.stringify({
|
||||
display_text: '🔧 Spécifications',
|
||||
buttonId: 'btn2',
|
||||
buttonText: { displayText: '🔧 Spécifications' },
|
||||
type: 1 // URL button
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
await sock.sendMessage(`${phone}@s.whatsapp.net`, {
|
||||
image: imageBuffer,
|
||||
caption: caption,
|
||||
footer: footer,
|
||||
templateButtons: [
|
||||
{
|
||||
index: 1,
|
||||
urlButton: {
|
||||
displayText: '📄 Voir proposition',
|
||||
url: 'https://merlo-ch.com/uploads/proposition/f_p_250505_0000136_00008_EB00001909.pdf'
|
||||
}
|
||||
},
|
||||
{
|
||||
index: 2,
|
||||
urlButton: {
|
||||
displayText: '🔧 Spécifications',
|
||||
url: 'https://merlo-ch.com/uploads/proposition/d_p_250505_0000136_00008_EB00001909.pdf'
|
||||
})
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
// Construire l'InteractiveMessage complet
|
||||
const interactiveMsg = proto.Message.InteractiveMessage.create({
|
||||
header,
|
||||
body,
|
||||
footer: foot,
|
||||
nativeFlowMessage: nativeFlow
|
||||
});
|
||||
|
||||
// Envelopper (ici dans viewOnceMessage, comme dans votre exemple)
|
||||
const raw = {
|
||||
viewOnceMessage: {
|
||||
message: {
|
||||
messageContextInfo: {
|
||||
deviceListMetadata: {},
|
||||
deviceListMetadataVersion: 2
|
||||
},
|
||||
interactiveMessage: interactiveMsg
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Générer et relayer
|
||||
const jid = `${phone}@s.whatsapp.net`;
|
||||
const msg = generateWAMessageFromContent(jid, raw, {});
|
||||
await sock.relayMessage(jid, msg.message, { messageId: msg.key.id });
|
||||
|
||||
return res.json({ success: true });
|
||||
} catch (e) {
|
||||
console.error('❌ Erreur /sendInteractiveImage :', e);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user