This commit is contained in:
cangui 2025-05-06 21:45:38 +02:00
parent ad3b2c33c8
commit 2c7bbb8c50

147
index.js
View File

@ -71,58 +71,12 @@ const initBaileys = async () => {
});
// (Facultatif) Gestion des messages reçus
sock.ev.on('messages.upsert', async ({ messages, type }) => {
sock.ev.on('messages.upsert', async ({ messages }) => {
const msg = messages[0];
// Ignore les messages venant de toi
if (!msg.message || msg.key.fromMe) return;
const from = msg.key.remoteJid;
console.log('📩 Message reçu de', from);
// 💬 Gérer les réponses à un bouton `cta_reply`
if (msg.message?.buttonReplyMessage) {
const id = msg.message.buttonReplyMessage.selectedDisplayText;
console.log('🟢 Réponse bouton :', id);
// Exemple de réponse simple
await sock.sendMessage(from, { text: `Vous avez sélectionné : ${id}` });
return;
}
// 🧵 Gérer les sélections dans un menu `single_select`
if (msg.message?.interactiveResponseMessage?.nativeFlowResponseMessage) {
const native = msg.message.interactiveResponseMessage.nativeFlowResponseMessage;
const params = JSON.parse(native.paramsJson);
const selectedRowId = params?.selected_row_id;
console.log('📂 ID sélectionné dans le menu :', selectedRowId);
if (selectedRowId === 'brochure_1') {
await sock.sendMessage(from, {
document: { url: "https://merlo-ch.com/uploads/proposition/f_p_250505_0000136_00008_EB00001909.pdf" },
mimetype: "application/pdf",
fileName: "Catalogue_Merlo.pdf"
});
} else if (selectedRowId === 'brochure_2') {
await sock.sendMessage(from, {
document: { url: "https://merlo-ch.com/uploads/proposition/d_p_250505_0000136_00008_EB00001909.pdf" },
mimetype: "application/pdf",
fileName: "Fiche_Technique_Merlo.pdf"
});
} else {
await sock.sendMessage(from, { text: "Option non reconnue." });
}
return;
}
// Optionnel : fallback pour tout autre message
if (msg.message?.conversation) {
await sock.sendMessage(from, { text: "Merci pour votre message ! 😊" });
if (!msg.key.fromMe && msg.message?.conversation) {
console.log('💬 Message reçu de', msg.key.remoteJid, ':', msg.message.conversation);
}
});
};
initBaileys();
@ -158,8 +112,11 @@ app.post('/testButtons', async (req, res) => {
try {
await sock.sendMessage(jid, {
image: { url : "https://wa.canguidev.fr/static/logo-merlo-cs-FR.jpg" }, // Can buffer
text: "Hello Wolrd !;",
caption: "Description Of Messages", //Additional information
footer: "© Fizzxy Dev",
media:true,
buttons: [
{
buttonId: '.tes',
@ -368,98 +325,6 @@ app.post('/sendInteractiveImage', async (req, res) => {
return res.status(500).json({ error: e.message });
}
});
app.post('/sendFullInteractive', async (req, res) => {
const { phone } = req.body;
if (!sock || !isConnected) {
return res.status(400).json({ error: 'Non connecté' });
}
const BASE_URL = process.env.BASE_URL || 'https://wa.canguidev.fr';
const imageUrl = `${BASE_URL}/static/logo-merlo-cs-FR.jpg`;
const jid = `${phone}@s.whatsapp.net`;
try {
const content = {
viewOnceMessage: {
message: {
messageContextInfo: {
deviceListMetadata: {},
deviceListMetadataVersion: 2
},
interactiveMessage: proto.Message.InteractiveMessage.create({
header: {
hasMediaAttachment: true,
imageMessage: { url: imageUrl }
},
body: { text: "Bienvenue chez Merlo France 🇫🇷\nChoisissez une action ci-dessous." },
footer: { text: "MERLO - Support & Documentation" },
nativeFlowMessage: {
buttons: [
{
name: "cta_reply",
buttonParamsJson: JSON.stringify({
display_text: "📩 Contacter support",
id: "support_action"
})
},
{
name: "cta_url",
buttonParamsJson: JSON.stringify({
display_text: "📄 Voir proposition",
url: "https://merlo-ch.com/uploads/proposition/f_p_250505_0000136_00008_EB00001909.pdf"
})
},
{
name: "cta_url",
buttonParamsJson: JSON.stringify({
display_text: "🔧 Spécifications",
url: "https://merlo-ch.com/uploads/proposition/d_p_250505_0000136_00008_EB00001909.pdf"
})
},
{
name: "single_select",
buttonParamsJson: JSON.stringify({
title: "📚 Choisir une brochure",
sections: [
{
title: "Documentation PDF",
highlight_label: "Merlo Docs",
rows: [
{
id: "brochure_1",
title: "Catalogue Général",
description: "Voir le PDF du catalogue",
header: "Catalogue"
},
{
id: "brochure_2",
title: "Fiche technique",
description: "Voir le PDF de la fiche technique",
header: "Fiche"
}
]
}
]
})
}
]
}
})
}
}
};
const msg = generateWAMessageFromContent(jid, content, {});
await sock.relayMessage(jid, msg.message, { messageId: msg.key.id });
res.json({ success: true });
} catch (e) {
console.error('❌ Erreur sendFullInteractive :', e);
res.status(500).json({ error: e.message });
}
});
// app.post('/sendInteractiveImage', async (req, res) => {
// const { phone, caption, title, subtitle, footer } = req.body;