|
|
|
@ -1,4 +1,4 @@
|
|
|
|
|
import { cloneDeep } from "@/utils/utils";
|
|
|
|
|
import { cloneDeep, isEmpty } from "@/utils/utils";
|
|
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
|
|
|
|
|
|
export const replaceTemplateString = (str, replacements) => {
|
|
|
|
@ -26,7 +26,7 @@ export const sentMsgTypeMapped = {
|
|
|
|
|
text: {
|
|
|
|
|
type: 'text',
|
|
|
|
|
contentToSend: (msg) => ({ action: 'message', actionId: msg.id, renderId: msg.id, to: msg.to, msgtype: 'text', msgcontent: { body: msg.text } }),
|
|
|
|
|
contentToRender: (msg) => ({ ...msg }),
|
|
|
|
|
contentToRender: (msg) => ({ ...msg, actionId: msg.id, }),
|
|
|
|
|
},
|
|
|
|
|
whatsappTemplate: {
|
|
|
|
|
contentToSend: (msg) => ({ action: 'message', actionId: msg.id, renderId: msg.id, to: msg.to, msgtype: 'template', msgcontent: msg.template }),
|
|
|
|
@ -38,6 +38,7 @@ export const sentMsgTypeMapped = {
|
|
|
|
|
// const footer = msg.template_origin.components?.footer?.[0]?.text || '';
|
|
|
|
|
return {
|
|
|
|
|
...msg,
|
|
|
|
|
actionId: msg.id,
|
|
|
|
|
type: 'text',
|
|
|
|
|
title: msg.template_origin.components.header?.[0]?.text || '',
|
|
|
|
|
text: `${fillTemplate}`, // msg.template_origin.components.body?.[0]?.text || '',
|
|
|
|
@ -49,11 +50,11 @@ const whatsappMsgMapped = {
|
|
|
|
|
'whatsapp.inbound_message.received': {
|
|
|
|
|
getMsg: (result) => {
|
|
|
|
|
console.log('whatsapp.inbound_message.received', result);
|
|
|
|
|
return result?.whatsappInboundMessage || null;
|
|
|
|
|
return isEmpty(result?.whatsappInboundMessage) ? null : { ...result.whatsappInboundMessage, conversationid: result.conversationid };
|
|
|
|
|
},
|
|
|
|
|
contentToRender: (result) => {
|
|
|
|
|
console.log('whatsapp.inbound_message.received to render', result);
|
|
|
|
|
const contentObj = result?.whatsappInboundMessage || result; // debug:
|
|
|
|
|
contentToRender: (contentObj) => {
|
|
|
|
|
console.log('whatsapp.inbound_message.received to render', contentObj);
|
|
|
|
|
// const contentObj = result?.whatsappInboundMessage || result; // debug:
|
|
|
|
|
return parseRenderMessageItem(contentObj);
|
|
|
|
|
},
|
|
|
|
|
contentToUpdate: () => null,
|
|
|
|
@ -61,41 +62,31 @@ const whatsappMsgMapped = {
|
|
|
|
|
'whatsapp.message.updated': {
|
|
|
|
|
getMsg: (result) => {
|
|
|
|
|
console.log('getMsg', result);
|
|
|
|
|
return result?.whatsappMessage || null;
|
|
|
|
|
return isEmpty(result?.whatsappMessage) ? null : { ...result.whatsappMessage, conversationid: result.conversationid };
|
|
|
|
|
},
|
|
|
|
|
contentToRender: (msgcontent) => {
|
|
|
|
|
return null; // * 仅更新消息状态, 没有输出
|
|
|
|
|
|
|
|
|
|
// let contentObj = msgcontent?.whatsappMessage || msgcontent; // debug:
|
|
|
|
|
// if ((contentObj?.status === 'failed' )) {
|
|
|
|
|
// contentObj = {
|
|
|
|
|
// type: 'error',
|
|
|
|
|
// text: {body: '❌ Message failed to send' }, // contentObj.errorMessage
|
|
|
|
|
// id: contentObj.id,
|
|
|
|
|
// wamid: contentObj.id,
|
|
|
|
|
// };
|
|
|
|
|
// return contentObj;
|
|
|
|
|
// }
|
|
|
|
|
// return null;
|
|
|
|
|
// console.log('whatsapp.message.updated to render', contentObj);
|
|
|
|
|
// const _r = parseRenderMessageItem(contentObj);
|
|
|
|
|
// console.log('_r', _r);
|
|
|
|
|
// return parseRenderMessageItem(contentObj);
|
|
|
|
|
},
|
|
|
|
|
contentToUpdate: (msgcontent) => ({ ...msgcontent, id: msgcontent.wamid, status: msgcontent.status }),
|
|
|
|
|
contentToRender: () => null, // * 仅更新消息状态, 没有输出
|
|
|
|
|
contentToUpdate: (msgcontent) => ({ ...msgcontent, id: msgcontent.wamid, status: msgStatusRenderMapped[(msgcontent?.status || 'failed')] }),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
export const msgStatusRenderMapped = {
|
|
|
|
|
'accepted': 'sent',
|
|
|
|
|
'sent': 'sent',
|
|
|
|
|
'delivered': 'received',
|
|
|
|
|
'read': 'read',
|
|
|
|
|
'failed': 'failed',
|
|
|
|
|
};
|
|
|
|
|
export const receivedMsgTypeMapped = {
|
|
|
|
|
...cloneDeep(whatsappMsgMapped),
|
|
|
|
|
'message': {
|
|
|
|
|
// 发送消息的同步记录 status: 'accepted'
|
|
|
|
|
getMsg: (result) => ({ ...result, conversationId: result.actionId.split('.')[0] }),
|
|
|
|
|
getMsg: (result) => ({ ...result, conversationid: result.actionId.split('.')[0] }),
|
|
|
|
|
contentToRender: () => null,
|
|
|
|
|
contentToUpdate: (msgcontent) => ({
|
|
|
|
|
...msgcontent,
|
|
|
|
|
id: msgcontent.actionId,
|
|
|
|
|
status: ['delivered', 'accepted', 'sent'].includes(msgcontent?.status || '' ? 'sent' : 'failed'),
|
|
|
|
|
conversationId: msgcontent.conversation.id,
|
|
|
|
|
actionId: msgcontent.actionId,
|
|
|
|
|
id: msgcontent.wamid,
|
|
|
|
|
status: msgStatusRenderMapped[(msgcontent?.status || 'failed')],
|
|
|
|
|
conversationid: msgcontent.actionId.split('.')[0], // msgcontent.conversation.id,
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
'error': {
|
|
|
|
@ -174,6 +165,7 @@ export const parseRenderMessageItem = (msg) => {
|
|
|
|
|
return {
|
|
|
|
|
date: msg?.sendTime || '',
|
|
|
|
|
...(whatsappMsgTypeMapped?.[msg.type]?.data(msg) || {}),
|
|
|
|
|
conversationid: msg.conversationid,
|
|
|
|
|
...(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,
|
|
|
|
|