DDDDD
This commit is contained in:
parent
33e6203d36
commit
6dc36a2039
102
index.js
102
index.js
@ -20,7 +20,9 @@ const initBaileys = async () => {
|
|||||||
sock = makeWASocket({
|
sock = makeWASocket({
|
||||||
version,
|
version,
|
||||||
auth: state,
|
auth: state,
|
||||||
printQRInTerminal: false
|
printQRInTerminal: false,
|
||||||
|
logger: { level: 'trace' } // Active les logs détaillés
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
sock.ev.on('connection.update', async (update) => {
|
sock.ev.on('connection.update', async (update) => {
|
||||||
@ -129,73 +131,55 @@ app.post('/sendInteractiveImage', async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const BASE_URL = process.env.BASE_URL || 'https://wa.canguidev.fr';
|
const imagePath = path.join(__dirname, 'public', 'logo-merlo-cs-FR.jpg');
|
||||||
const imageUrl = `${BASE_URL}/static/logo-merlo-cs-FR.jpg`;
|
const imageBuffer = fs.readFileSync(imagePath);
|
||||||
|
|
||||||
|
// Upload the image first
|
||||||
|
const uploadedImage = await prepareWAMessageMedia(
|
||||||
|
{ image: imageBuffer },
|
||||||
|
{ upload: sock.waUploadToServer }
|
||||||
|
);
|
||||||
|
|
||||||
// Construire le Header correct
|
const interactiveMessage = {
|
||||||
const header = proto.Message.InteractiveMessage.create({
|
imageMessage: uploadedImage.imageMessage,
|
||||||
// ici on injecte l'image dans le header sans besoin de type explicite
|
caption: caption || 'Description par défaut',
|
||||||
header: proto.Message.InteractiveMessage.Header.create({
|
footer: footer || 'Pied de page',
|
||||||
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'
|
|
||||||
});
|
|
||||||
|
|
||||||
// Vos boutons URL
|
|
||||||
const nativeFlow = proto.Message.InteractiveMessage.NativeFlowMessage.create({
|
|
||||||
buttons: [
|
buttons: [
|
||||||
{
|
{
|
||||||
name: 'cta_url',
|
buttonId: 'btn1',
|
||||||
buttonParamsJson: JSON.stringify({
|
buttonText: { displayText: '📄 Voir proposition' },
|
||||||
display_text: '📄 Voir proposition',
|
type: 1 // URL button
|
||||||
url: 'https://merlo-ch.com/uploads/proposition/f_p_250505_0000136_00008_EB00001909.pdf'
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'cta_url',
|
buttonId: 'btn2',
|
||||||
buttonParamsJson: JSON.stringify({
|
buttonText: { displayText: '🔧 Spécifications' },
|
||||||
display_text: '🔧 Spécifications',
|
type: 1 // URL button
|
||||||
url: 'https://merlo-ch.com/uploads/proposition/d_p_250505_0000136_00008_EB00001909.pdf'
|
}
|
||||||
})
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
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 });
|
return res.json({ success: true });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('❌ Erreur /sendInteractiveImage :', e);
|
console.error('❌ Erreur /sendInteractiveImage :', e);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user