同步返回结果: API状态; 异步结果

dev/chat
Lei OT 2 years ago
parent 58038b6fe2
commit 404105fc40

@ -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,

@ -122,11 +122,7 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
console.log('currentConversation', currentConversation);
// const colisn = currentConversation.coli_sn;
// getCustomerProfile(colisn);
const _all = [];
// const _all = all.map((ele) => receivedMsgTypeMapped['whatsapp.inbound_message.received'].contentToRender(ele)); // debug: 0
// todo: update msg status
// const _all = all2.map((ele) => receivedMsgTypeMapped['whatsapp.message.updated'].contentToRender(ele)); // debug: 0
setMessages([..._all, ...(activeConversations[currentID] || [])]);
setMessages([...(activeConversations[currentID] || [])]);
return () => {};
}, [currentConversation]);
@ -168,14 +164,18 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
const addMessage = (message) => {
setMessages((prevMessages) => [...prevMessages, message]);
addMessageToConversations(currentConversationRef.current.sn, message);
// addMessageToConversations(currentConversationRef.current.sn, message);
addMessageToConversations(message.conversationid, message);
};
const updateMessage = (message) => {
let targetMsgs;
setMessages((prevMessages) => {
targetMsgs = prevMessages.map(ele => {
if (ele.id === message.id) {
if (ele.id === ele.actionId && ele.actionId === message.actionId) {
return {...ele, id: message.id, status: message.status, dateString: message.dateString};
}
else if (ele.id === message.id) {
return {...ele, id: message.id, status: message.status, dateString: message.dateString};
}
return ele;
@ -201,7 +201,7 @@ export const useConversations = ({loginUser, realtimeAPI}) => {
if (!result) {
return false;
}
let resultType = result.type;
let resultType = result?.action || result.type;
if (errcode !== 0) {
// addError('Error Connecting to Server');
resultType = 'error';

@ -48,7 +48,7 @@ const InputBox = (({ onSend }) => {
? { components: [
{
'type': 'body',
'parameters': whatsappTemplatesParamMapped[fromTemplate.name].map((v) => ({ type: 'text', text: getNestedValue(_conversation, v) }))
'parameters': whatsappTemplatesParamMapped[fromTemplate.name].map((v) => ({ type: 'text', text: getNestedValue(_conversation, v) || '' }))
// [
// {
// 'type': 'text',

Loading…
Cancel
Save