You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Global-sales/wai-server/helper/wai.msg.helper.js

274 lines
7.8 KiB
JavaScript

/**
*
*/
'use strict';
const { objectMapper, pick } = require('../utils/commons.util');
const mediaMsg = {
contentToSend: msg => {
const { msgtype, msgcontent, ...body } = msg;
return {
// ...body,
to: body.to,
externalId: body.actionId,
// type WAMediaUpload = Buffer | WAMediaPayloadStream | WAMediaPayloadURL;
[msgtype]: {
url: msgcontent[msgtype].link,
},
...(msgcontent[msgtype].caption ? { caption: msgcontent[msgtype].caption } : {}), // image, video, document
// mimetype: 'audio/mp4',
...(msgtype === 'document' ? { filename: msgcontent[msgtype].filename } : {}), // document
...(msgcontent.context ? { quoted: { key: msgcontent.context.message_id } } : {}),
// seconds?: number; // audio
// isAnimated?: boolean; // sticker
// jpegThumbnail?: string; // image, video
imageUrl: msgcontent[msgtype].link, // 25.01.01 版本, 后续更改payload格式 todo:
};
},
dataToDB: msg => {
const { msgtype, msgcontent, ...body } = msg;
const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']);
return {
direction: 'outbound',
msg_status: 'ready',
createTime: Date.now(),
id: msg.actionId,
...record,
IVADS_link: msgcontent[msgtype].link,
IVADS_caption: msgcontent[msgtype].caption || '',
IVADS_filename: msgcontent[msgtype].filename || '',
...(msgcontent.context
? {
context_id: msgcontent.context.message_id,
context_from: msgcontent.message_origin.from,
}
: {}),
message_origin: JSON.stringify(msg),
};
},
DbData: row => ({
[row.msgtype]: {
link: row.IVADS_link,
caption: row.IVADS_caption,
filename: row.IVADS_filename,
},
...(row.context_id ? { context: { message_id: row.context_id, from: row.context_from } } : {}),
}),
};
const waiMsgTypeMapped = {
text: {
type: 'text',
contentToSend: msg => ({
// ...msg,
to: msg.to,
externalId: msg.actionId,
text: msg.msgcontent.body,
content: msg.msgcontent.body, // 25.01.01 版本, 后续更改payload格式 todo:
// linkPreview: true, // {}
...(msg.msgcontent.context ? { quoted: { key: msg.msgcontent.context.message_id } } : {}),
}),
dataToDB: msg => {
const { msgcontent } = msg;
const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']);
return {
direction: 'outbound',
msg_status: 'ready',
createTime: Date.now(),
id: msg.actionId,
...record,
text_body: msgcontent.body,
text_preview_url: msgcontent.preview_url,
...(msgcontent.context
? {
context_id: msgcontent.context.message_id,
context_from: msgcontent.message_origin.from,
}
: {}),
message_origin: JSON.stringify(msg),
};
},
DbData: row => ({
text: { body: row.text_body, preview_url: row.text_preview_url },
...(row.context_id ? { context: { message_id: row.context_id, from: row.context_from } } : {}),
}),
},
image: {
type: 'image',
contentToSend: msg => ({
...mediaMsg.contentToSend({ ...msg }),
}),
dataToDB: msg => mediaMsg.dataToDB(msg),
DbData: msg => mediaMsg.DbData(msg),
},
sticker: {
type: 'sticker',
contentToSend: msg => ({
...mediaMsg.contentToSend({ ...msg }),
}),
dataToDB: msg => mediaMsg.dataToDB(msg),
DbData: msg => mediaMsg.DbData(msg),
},
audio: {
type: 'audio',
contentToSend: msg => ({
...mediaMsg.contentToSend({ ...msg }),
}),
dataToDB: msg => mediaMsg.dataToDB(msg),
DbData: msg => mediaMsg.DbData(msg),
},
video: {
type: 'video', // todo: gif
contentToSend: msg => ({
...mediaMsg.contentToSend({ ...msg }),
}),
dataToDB: msg => mediaMsg.dataToDB(msg),
DbData: msg => mediaMsg.DbData(msg),
},
document: {
type: 'document',
contentToSend: msg => ({
...mediaMsg.contentToSend({ ...msg }),
}),
dataToDB: msg => mediaMsg.dataToDB(msg),
DbData: msg => mediaMsg.DbData(msg),
},
react: {
type: 'react',
contentToSend: msg => ({
// ...msg,
to: msg.to,
externalId: msg.actionId,
react: {
text: msg.msgcontent.body, // emoji | use an empty string to remove the reaction
key: msg.msgcontent.context.message_id,
},
}),
dataToDB: msg => {
const { msgtype, msgcontent, ...body } = msg;
const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']);
return {
direction: 'outbound',
msg_status: 'ready',
createTime: Date.now(),
id: msg.actionId,
...record,
reaction_message_id: '',
reaction_emoji: '',
message_origin: JSON.stringify(msg),
};
},
DbData: row => ({
reaction: { emoji: row.reaction_emoji, message_id: row.reaction_message_id },
}),
},
location: {
type: 'location',
contentToSend: msg => ({
// ...msg,
to: msg.to,
externalId: msg.actionId,
// location: { degreesLatitude: msg.latitude, degreesLongitude: msg.longitude }, // todo:
}),
dataToDB: msg => {
const { msgtype, msgcontent, ...body } = msg;
const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']);
return {
direction: 'outbound',
msg_status: 'ready',
createTime: Date.now(),
id: msg.actionId,
...record,
message_origin: JSON.stringify(msg),
};
},
DbData: row => ({
location: {
latitude: row.location_latitude,
longitude: row.location_longitude,
name: row.location_name,
address: row.location_address,
url: row.location_url,
},
}),
},
contacts: {
type: 'contacts',
contentToSend: msg => ({
// ...msg,
to: msg.to,
externalId: msg.actionId,
// contacts: { displayName: '', contacts: msg.contacts }, // todo:
}),
dataToDB: msg => {
const { msgtype, msgcontent, ...body } = msg;
const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']);
return {
direction: 'outbound',
msg_status: 'ready',
createTime: Date.now(),
id: msg.actionId,
...record,
contacts: JSON.stringify(msg.contacts),
message_origin: JSON.stringify(msg),
};
},
DbData: row => ({
contacts: JSON.parse(row.contacts),
}),
},
template: {
type: 'template',
contentToSend: msg => ({
// ...msg,
to: msg.to,
externalId: msg.actionId,
// msgcontent: {
// ...msg.template,
// components: [
// ...msg.template.components.filter(com => !['footer', 'buttons'].includes(com.type.toLowerCase())),
// ],
// },
}),
dataToDB: msg => {
const { msgtype, msgcontent, ...body } = msg;
const record = pick(msg, ['actionId', 'msgtype', 'externalId', 'from', 'to']);
return {
direction: 'outbound',
msg_status: 'ready',
createTime: Date.now(),
id: msg.actionId,
...record,
message_origin: JSON.stringify(msg),
};
},
},
};
/**
* API Payload to Send
*/
exports.ctxToSendBuilder = ctxContent => {
const { contentToSend } = waiMsgTypeMapped[ctxContent.msgtype];
const msgReady = contentToSend(ctxContent);
return msgReady;
};
/**
* API Payload to DB
*/
exports.ctxToDB = ctxContent => {
const { dataToDB } = waiMsgTypeMapped[ctxContent.msgtype];
const msgReady = dataToDB(ctxContent);
return msgReady;
};
/**
* Parse DB Data to UI/API/Webhook
*/
exports.DbData = row => {
const { DbData } = waiMsgTypeMapped[row.msgtype];
const msgReady = DbData(row);
return msgReady;
};