iva-studio-workspace/IVA-Wakeup/proxy/CAInterface_Messenger.js

132 lines
5.6 KiB
JavaScript

console.log("===== Main Messenger Proxy - v17.9 =====");
const casettings = CAInterface_settings;
console.log(req);
const external = req.body.event?.external;
const sessionId = req.body.event?.conversationId;
const metadata = req.body.event?.metadata;
const msgrSettings = eval(`settings${casettings.hubSuffixMessenger}`);
let nlu = msgrSettings?.nlu;
if (!nlu) {
console.log("Install Basic Messenger Webhook.");
console.log("Validate the value of hubSuffixMessenger in CAInterface_settings");
}
const messengerUrl = `${nlu.apiBaseURL}ProxyScript/run/${req.params.workspaceId}/${req.params.branch}/messenger${casettings.hubSuffixMessenger}`;
const CA = CAInterface();
const channel = req?.body?.event?.metadata?.channel ?? req?.body?.channel;
const attachments = req?.body?.event?.metadata?.attachments;
const validEventInput = (event) => {
console.log(20);
console.log(event);
return event?.input && event.input.length != 0;
};
const getGatewayHeaders = async () => {
const header = {};
if (channel == "whatsapp") {
const notify = casettings.notifications ?? [];
const handler = notify.find(v => v.name === "VMGateway_eventHandler");
if (handler) {
const suffix = handler.hubSuffix || "";
const gatewaySettings = eval("VMGateway_settings" + suffix);
const { refreshGatewayKey } = eval("VMGateway_support" + suffix);
if (gatewaySettings?.gateway) {
header["x-auth-apiKey"] = await refreshGatewayKey(gatewaySettings.gateway);
}
}
}
return header;
}
(async () => {
try {
const smStr = `session-map-${sessionId}`;
//await redis.set("sentdata", "false");
const sessionMap = await redis.hGetAll(smStr);
console.log(sessionMap);
console.log(`47 line`);
console.log(smStr);
console.log({
"Session Map": sessionMap,
});
if (sessionMap.chatDetails) {
sessionMap.chatDetails = JSON.parse(sessionMap.chatDetails);
}
if (attachments?.length && sessionMap.liveChatRequest !== "true") {
res.send({
conversationId: sessionId, workspaceId: req.params.workspaceId,
answers: [casettings.liveChat.attachment.noLiveChat]
});
} else if (sessionMap.liveChatRequest == "true" && sessionMap.formFlow !== "true") {
if (attachments && attachments.length && sessionMap?.chatDetails?.userId) {
const headers = await getGatewayHeaders();
for (let attachment of attachments) {
CA.sendAttachmentToAgent(sessionMap.chatDetails.userId, attachment, headers);
console.log({ attachment });
}
}
if (metadata?.liveChat === false) {
await CA.leaveLiveChat(sessionId, sessionMap.conversationId, channel,
req.body.event?.input, req.body.event?.sentBy?.userId);
} else if (validEventInput(req.body.event)) {
await CA.sendToLiveChat(sessionId, sessionMap.conversationId, channel,
req.body.event?.input, req.body.event?.sentBy?.userId);
}
res.send({
conversationId: sessionId, workspaceId: req.params.workspaceId,
input: req.body.event?.input, answers: [],
classificationResults: []
});
// }
}
else if (validEventInput(req.body.event)) {
console.log(79);
console.log(metadata);
// call the webhook
const { data: recognizedData } = await axios({
url: messengerUrl,
method: "post",
data: req.body,
});
if (recognizedData?.formFlowEnd) {
let sentdata = sessionMap?.sentdata;
if (sessionMap?.datacollect === "true" && sentdata === "false") {
let history = await gvf_ConversationHistory().fetchConversationHistory(sessionMap?.conversationId, 0);
if (metadata?.liveChat === false) {
await CA.leaveLiveChat(sessionId, sessionMap.conversationId, channel,
history, req.body.event?.sentBy?.userId);
} else if (validEventInput(req.body.event)) {
await CA.sendToLiveChat(sessionId, sessionMap.conversationId, channel,
history, req.body.event?.sentBy?.userId);
}
await redis.hSet(smStr, {
datacollect: "false",
sentdata: "true"
});
}
}
if (recognizedData?.outputs?.liveChat === true || metadata?.liveChat == true) {
if (external && metadata?.liveChat == true) {
// need to log invisible chat messages
CA.addTransaction(recognizedData.conversationId, "Start Live Chat", "user", {
liveChat: true,
});
}
CA.joinLiveChat(
channel,
sessionId,
recognizedData.conversationId,
recognizedData?.outputs?.liveChatRequest
);
}
res.send(recognizedData);
}
} catch (error) {
console.log(error.message);
res.send({
answers: ["Something went wrong. Please try again."],
outputs: {},
});
}
})();