|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
|
|
|
|
|
|
|
|
export const replaceTemplateString = (str, replacements) => {
|
|
|
|
export const replaceTemplateString = (str, replacements) => {
|
|
|
|
let result = str;
|
|
|
|
let result = str;
|
|
|
@ -23,21 +24,22 @@ export const replaceTemplateString = (str, replacements) => {
|
|
|
|
export const sentMsgTypeMapped = {
|
|
|
|
export const sentMsgTypeMapped = {
|
|
|
|
text: {
|
|
|
|
text: {
|
|
|
|
type: 'text',
|
|
|
|
type: 'text',
|
|
|
|
contentToSend: (msg) => ({ renderId: msg.id, to: msg.to, msgtype: 'text', msgcontent: { body: msg.text } }),
|
|
|
|
contentToSend: (msg) => ({ action: 'message', actionId: uuid(), renderId: msg.id, to: msg.to, msgtype: 'text', msgcontent: { body: msg.text } }),
|
|
|
|
contentToRender: (msg) => ({ ...msg }),
|
|
|
|
contentToRender: (msg) => ({ ...msg }),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
whatsappTemplate: {
|
|
|
|
whatsappTemplate: {
|
|
|
|
contentToSend: (msg) => ({ renderId: msg.id, to: msg.to, msgtype: 'template', msgcontent: msg.template }),
|
|
|
|
contentToSend: (msg) => ({ action: 'message', actionId: uuid(), renderId: msg.id, to: msg.to, msgtype: 'template', msgcontent: msg.template }),
|
|
|
|
contentToRender: (msg) => {
|
|
|
|
contentToRender: (msg) => {
|
|
|
|
console.log(msg);
|
|
|
|
console.log(msg);
|
|
|
|
const templateDataMapped = msg.template?.components ? msg.template.components.reduce((r, v) => ({...r, [v.type]: v}), {}) : null;
|
|
|
|
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 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 || '');
|
|
|
|
const fillTemplate = templateParam.length ? replaceTemplateString(msg.template_origin.components.body?.[0]?.text || '', templateParam) : (msg.template_origin.components.body?.[0]?.text || '');
|
|
|
|
|
|
|
|
const footer = msg.template_origin.components?.footer?.[0]?.text || '';
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...msg,
|
|
|
|
...msg,
|
|
|
|
type: 'text',
|
|
|
|
type: 'text',
|
|
|
|
title: msg.template_origin.components.header?.[0]?.text || '',
|
|
|
|
title: msg.template_origin.components.header?.[0]?.text || '',
|
|
|
|
text: fillTemplate, // msg.template_origin.components.body?.[0]?.text || '',
|
|
|
|
text: `${fillTemplate}`, // msg.template_origin.components.body?.[0]?.text || '',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
@ -60,6 +62,8 @@ export const whatsappMsgMapped = {
|
|
|
|
return result?.whatsappMessage || null;
|
|
|
|
return result?.whatsappMessage || null;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
contentToRender: (msgcontent) => {
|
|
|
|
contentToRender: (msgcontent) => {
|
|
|
|
|
|
|
|
return null; // todo: 仅更新消息状态, 没有输出
|
|
|
|
|
|
|
|
|
|
|
|
let contentObj = msgcontent?.whatsappMessage || msgcontent; // debug:
|
|
|
|
let contentObj = msgcontent?.whatsappMessage || msgcontent; // debug:
|
|
|
|
if ((contentObj?.status === 'failed' )) {
|
|
|
|
if ((contentObj?.status === 'failed' )) {
|
|
|
|
contentObj = {
|
|
|
|
contentObj = {
|
|
|
@ -68,7 +72,9 @@ export const whatsappMsgMapped = {
|
|
|
|
id: contentObj.id,
|
|
|
|
id: contentObj.id,
|
|
|
|
wamid: contentObj.id,
|
|
|
|
wamid: contentObj.id,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return contentObj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
console.log('whatsapp.message.updated to render', contentObj);
|
|
|
|
console.log('whatsapp.message.updated to render', contentObj);
|
|
|
|
const _r = parseRenderMessageItem(contentObj);
|
|
|
|
const _r = parseRenderMessageItem(contentObj);
|
|
|
|
console.log('_r', _r);
|
|
|
|
console.log('_r', _r);
|
|
|
@ -144,12 +150,16 @@ export const whatsappMsgTypeMapped = {
|
|
|
|
// 'contact-card-with-photo': 'contact-card-with-photo',
|
|
|
|
// 'contact-card-with-photo': 'contact-card-with-photo',
|
|
|
|
// 'contact-card-with-photo-and-label': 'contact-card-with-photo-and-label',
|
|
|
|
// 'contact-card-with-photo-and-label': 'contact-card-with-photo-and-label',
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* render received msg
|
|
|
|
|
|
|
|
*/
|
|
|
|
export const parseRenderMessageItem = (msg) => {
|
|
|
|
export const parseRenderMessageItem = (msg) => {
|
|
|
|
console.log('parseRenderMessageItem', msg);
|
|
|
|
console.log('parseRenderMessageItem', msg);
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
date: msg?.sendTime || '',
|
|
|
|
date: msg?.sendTime || '',
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.data(msg) || {}),
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.data(msg) || {}),
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.type(msg) || { type: 'text' }), // type: whatsappMsgTypeMapped?.[msg.type]?.type || 'text',
|
|
|
|
...(typeof whatsappMsgTypeMapped?.[msg.type]?.type === 'function' ? whatsappMsgTypeMapped?.[msg.type]?.type(msg) : { type: whatsappMsgTypeMapped?.[msg.type]?.type || 'text' }),
|
|
|
|
|
|
|
|
// type: whatsappMsgTypeMapped?.[msg.type]?.type || 'text',
|
|
|
|
sender: msg.from,
|
|
|
|
sender: msg.from,
|
|
|
|
status: msg?.status || 'waiting',
|
|
|
|
status: msg?.status || 'waiting',
|
|
|
|
// title: msg.customerProfile.name,
|
|
|
|
// title: msg.customerProfile.name,
|
|
|
|