|
|
|
|
|
|
|
export const replaceTemplateString = (str, replacements) => {
|
|
|
|
let result = str;
|
|
|
|
let keys = str.match(/{{(.*?)}}/g).map(key => key.replace(/{{|}}/g, ''));
|
|
|
|
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
|
|
|
let replaceValue = replacements[i];
|
|
|
|
let template = new RegExp(`{{${keys[i]}}}`, 'g');
|
|
|
|
result = result.replace(template, replaceValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
// +8618777396951 lyj
|
|
|
|
{
|
|
|
|
"to": "+8613317835586", // qqs
|
|
|
|
"msgtype": "text",
|
|
|
|
"msgcontent": "{\"body\":\"txtmsgtest\"}"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
export const sentMsgTypeMapped = {
|
|
|
|
text: {
|
|
|
|
type: 'text',
|
|
|
|
contentToSend: (msg) => ({ renderId: msg.id, to: msg.to, msgtype: 'text', msgcontent: { body: msg.text } }),
|
|
|
|
contentToRender: (msg) => ({ ...msg }),
|
|
|
|
},
|
|
|
|
whatsappTemplate: {
|
|
|
|
contentToSend: (msg) => ({ renderId: msg.id, to: msg.to, msgtype: 'template', msgcontent: msg.template }),
|
|
|
|
contentToRender: (msg) => {
|
|
|
|
console.log(msg);
|
|
|
|
const templateDataMapped = msg.template?.components ? msg.template.components.reduce((r, v) => ({...r, [v.type]: v}), {}) : null;
|
|
|
|
const templateParam = (templateDataMapped?.body?.parameters || []).map(e => e.text);
|
|
|
|
const fillTemplate = templateParam.length ? replaceTemplateString(msg.template_origin.components.body?.[0]?.text || '', templateParam) : (msg.template_origin.components.body?.[0]?.text || '');
|
|
|
|
return {
|
|
|
|
...msg,
|
|
|
|
type: 'text',
|
|
|
|
title: msg.template_origin.components.header?.[0]?.text || '',
|
|
|
|
text: fillTemplate, // msg.template_origin.components.body?.[0]?.text || '',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
export const whatsappMsgMapped = {
|
|
|
|
'whatsapp.inbound_message.received': {
|
|
|
|
getMsg: (result) => {
|
|
|
|
console.log('whatsapp.inbound_message.received', result);
|
|
|
|
return result?.whatsappInboundMessage || null;
|
|
|
|
},
|
|
|
|
contentToRender: (result) => {
|
|
|
|
console.log('whatsapp.inbound_message.received to render', result);
|
|
|
|
const contentObj = result?.whatsappInboundMessage || result; // debug:
|
|
|
|
return parseRenderMessageItem(contentObj);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'whatsapp.message.updated': {
|
|
|
|
getMsg: (result) => {
|
|
|
|
console.log('getMsg', result);
|
|
|
|
let contentObj = result?.whatsappMessage || null;
|
|
|
|
if ((contentObj?.status === 'failed' )) {
|
|
|
|
contentObj = {
|
|
|
|
type: 'error',
|
|
|
|
text: {body: 'Message failed to send' }, // contentObj.errorMessage
|
|
|
|
id: contentObj.id,
|
|
|
|
wamid: contentObj.id,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return contentObj;
|
|
|
|
},
|
|
|
|
contentToRender: (msgcontent) => {
|
|
|
|
const contentObj = msgcontent?.whatsappMessage || msgcontent; // debug:
|
|
|
|
console.log('whatsapp.message.updated to render', contentObj);
|
|
|
|
const _r = parseRenderMessageItem(contentObj);
|
|
|
|
console.log('_r', _r);
|
|
|
|
return parseRenderMessageItem(contentObj);
|
|
|
|
},
|
|
|
|
contentToUpdate: (msg) => ({ ...msg, id: msg.wamid, stauts: msg.status }),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
export const whatsappMsgTypeMapped = {
|
|
|
|
error: {
|
|
|
|
type: (_m) => ({ type: 'system' }),
|
|
|
|
data: (msg) => ({ id: msg.wamid, text: msg.errorCode ? msg.errorMessage : msg.text.body }),
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
type: (msg) => ({ type: msg.errorCode ? 'system' : 'text' }),
|
|
|
|
data: (msg) => ({ id: msg.wamid, text: msg.errorCode ? msg.errorMessage : msg.text.body }),
|
|
|
|
},
|
|
|
|
image: {
|
|
|
|
type: 'photo',
|
|
|
|
data: (msg) => ({
|
|
|
|
data: { id: msg.wamid, uri: msg.image.link, width: 200, height: 200, alt: '' },
|
|
|
|
onOpen: () => {
|
|
|
|
console.log('Open image', msg.image.link);
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
sticker: {
|
|
|
|
type: 'photo',
|
|
|
|
data: (msg) => ({
|
|
|
|
data: { id: msg.wamid, uri: msg.sticker.link, width: 150, height: 120, alt: '' },
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
video: {
|
|
|
|
type: 'video',
|
|
|
|
data: (msg) => ({
|
|
|
|
data: {
|
|
|
|
id: msg.wamid,
|
|
|
|
videoURL: msg.video.link,
|
|
|
|
status: {
|
|
|
|
click: true,
|
|
|
|
loading: 0,
|
|
|
|
download: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
audio: {
|
|
|
|
type: 'audio',
|
|
|
|
data: (msg) => ({
|
|
|
|
id: msg.wamid,
|
|
|
|
data: {
|
|
|
|
audioURL: msg.audio.link,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
unsupported: { type: 'system', data: (msg) => ({ text: 'Message type is currently not supported.' }) },
|
|
|
|
reaction: {
|
|
|
|
type: 'text',
|
|
|
|
data: (msg) => ({ id: msg.wamid, text: msg.reaction?.emoji || msg.reaction?.text?.body || 'Reaction', reply: { message: '{content}', title: 'React from' } }),
|
|
|
|
},
|
|
|
|
template: {
|
|
|
|
type: (msg) => 'text',
|
|
|
|
data: (msg) => ({
|
|
|
|
id: msg.wamid,
|
|
|
|
// todo: update status: sent
|
|
|
|
status: msg.status,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
// file: 'file',
|
|
|
|
// location: 'location',
|
|
|
|
// contact: 'contact',
|
|
|
|
// 'contact-card': 'contact-card',
|
|
|
|
// 'contact-card-with-photo': 'contact-card-with-photo',
|
|
|
|
// 'contact-card-with-photo-and-label': 'contact-card-with-photo-and-label',
|
|
|
|
};
|
|
|
|
export const parseRenderMessageItem = (msg) => {
|
|
|
|
console.log('parseRenderMessageItem', msg);
|
|
|
|
return {
|
|
|
|
date: msg?.sendTime || '',
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.data(msg) || {}),
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.type(msg) || { type: 'text' }), // type: whatsappMsgTypeMapped?.[msg.type]?.type || 'text',
|
|
|
|
sender: msg.from,
|
|
|
|
status: msg?.status || 'waiting',
|
|
|
|
// title: msg.customerProfile.name,
|
|
|
|
// replyButton: true,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
export const parseRenderMessageList = (messages) => {
|
|
|
|
return messages.map((msg) => {
|
|
|
|
return {
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.data(msg) || {}),
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.type(msg) || { type: 'text' }), // type: whatsappMsgTypeMapped?.[msg.type]?.type || 'text',
|
|
|
|
id: msg.id,
|
|
|
|
sender: msg.from,
|
|
|
|
// title: msg.customerProfile.name,
|
|
|
|
date: msg.sendTime,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|