Skip to content

Commit 055c212

Browse files
committed
updated2
1 parent e190f5e commit 055c212

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

controllers/whatsappController.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,35 +78,59 @@ const normalizeWebhookMessage = (msg = {}) => {
7878
return { from, body: String(body || '').trim(), fromMe };
7979
};
8080

81+
// const extractWebhookMessages = (body = {}) => {
82+
// const event = body?.event || body?.type || '';
83+
// const payload = body?.payload ?? body?.data ?? body;
84+
85+
// if (String(event).startsWith('message.ack')) return [];
86+
87+
// const candidates = [];
88+
// if (Array.isArray(payload?.messages)) candidates.push(...payload.messages);
89+
// if (Array.isArray(payload)) candidates.push(...payload);
90+
// if (payload?.message) candidates.push(payload.message);
91+
// if (payload?.body || payload?.text || payload?.from || payload?.fromNumber) {
92+
// candidates.push(payload);
93+
// }
94+
// if (body?.message) candidates.push(body.message);
95+
// if (body?.body || body?.text || body?.from || body?.fromNumber) candidates.push(body);
96+
97+
// const unique = new Set();
98+
// const normalized = [];
99+
// for (const c of candidates) {
100+
// const msg = normalizeWebhookMessage(c);
101+
// const key = `${msg.from}|${msg.body}|${msg.fromMe}`;
102+
// if (!msg.from || !msg.body || msg.fromMe) continue;
103+
// if (unique.has(key)) continue;
104+
// unique.add(key);
105+
// normalized.push(msg);
106+
// }
107+
// return normalized;
108+
// };
109+
110+
81111
const extractWebhookMessages = (body = {}) => {
82-
const event = body?.event || body?.type || '';
83-
const payload = body?.payload ?? body?.data ?? body;
112+
if (body?.event !== 'message') return [];
84113

85-
if (String(event).startsWith('message.ack')) return [];
114+
const payload = body.payload;
115+
if (!payload) return [];
86116

87-
const candidates = [];
88-
if (Array.isArray(payload?.messages)) candidates.push(...payload.messages);
89-
if (Array.isArray(payload)) candidates.push(...payload);
90-
if (payload?.message) candidates.push(payload.message);
91-
if (payload?.body || payload?.text || payload?.from || payload?.fromNumber) {
92-
candidates.push(payload);
93-
}
94-
if (body?.message) candidates.push(body.message);
95-
if (body?.body || body?.text || body?.from || body?.fromNumber) candidates.push(body);
96-
97-
const unique = new Set();
98-
const normalized = [];
99-
for (const c of candidates) {
100-
const msg = normalizeWebhookMessage(c);
101-
const key = `${msg.from}|${msg.body}|${msg.fromMe}`;
102-
if (!msg.from || !msg.body || msg.fromMe) continue;
103-
if (unique.has(key)) continue;
104-
unique.add(key);
105-
normalized.push(msg);
106-
}
107-
return normalized;
108-
};
117+
// Ignore messages sent by the bot
118+
if (payload.fromMe) return [];
119+
120+
// Ignore group messages
121+
if (!payload.from?.endsWith('@c.us')) return [];
109122

123+
// Ignore empty/system messages
124+
if (!payload.body || payload.body.trim() === '') return [];
125+
126+
return [
127+
{
128+
from: payload.from,
129+
body: payload.body.trim(),
130+
fromMe: false,
131+
},
132+
];
133+
};
110134
const startSession = async (req, res) => {
111135
try {
112136
const webhookUrl = req.body?.webhookUrl || process.env.WEBHOOK_BASE_URL || null;
@@ -221,8 +245,12 @@ const handleWebhook = async (req, res) => {
221245
const messages = extractWebhookMessages(req.body || {});
222246
const results = [];
223247

248+
console.log(`[Webhook] Extracted ${messages.length} message(s) from payload.`);
249+
224250
for (const msg of messages) {
225251
const result = await followUpService.handleReply(msg.from, msg.body);
252+
253+
console.log(result, 'result from handleReply');
226254
results.push({
227255
from: msg.from,
228256
action: result?.action || 'ignored',

0 commit comments

Comments
 (0)