WhatsApp AI Assistant
Module 20 · ai · intermediate
report.js
// One result per message. A reply counts as sent only if WhatsApp returned a message id.
// Items arrive either from "Send WhatsApp Reply" (has_reply) or straight from "Has Reply?" (nothing to send).
return $input.all().map((item, i) => {
const isApiResponse = !item.json.wa_id; // a WhatsApp API response has no wa_id at the top level
const x = isApiResponse ? $('Save Turn').itemMatching(i).json : item.json;
const res = isApiResponse ? item.json : null;
const id = res?.messages?.[0]?.id ?? null;
const sendError = x.has_reply && !id
? String(res?.error?.message ?? res?.error ?? 'no message id returned (check the WhatsApp credential and the node error in the execution)').slice(0, 300)
: null;
return {
json: {
wa_id: x.wa_id, message_id: x.message_id, action: x.action, intent: x.intent ?? null,
reply: x.reply || null, reply_source: x.reply_source, ai_error: x.ai_error,
sent: Boolean(id), send_error: sendError, whatsapp_message_id: id,
duplicate: x.duplicate, open_now: x.open_now, handoff: x.handoff, needs_staff: x.needs_staff,
lead: x.lead, new_lead: x.new_lead, lead_complete: x.lead_complete, awaiting: x.awaiting, via_api: x.via_api,
},
};
});