125 lines
4.1 KiB
JavaScript
125 lines
4.1 KiB
JavaScript
// Test update
|
|
(async () => {
|
|
|
|
try {
|
|
console.log("Request: [" + JSON.stringify(req.body) + "]");
|
|
|
|
const caseId = req.body.caseId;
|
|
const conversationId = req.body.conversationId;
|
|
const externalSettings = req.body.settings;
|
|
const externalModelName = req.body.modelName;
|
|
const modelName = externalModelName ? externalModelName : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.modelName;
|
|
|
|
// with Email to Case - the 1st/recent note is the body of the email
|
|
var caseInstance = await CACaseInterface().getCase(caseId);
|
|
var inboundEmailBody = ""
|
|
if (caseInstance?.recentNotes?.length === 0)
|
|
console.log(`No body found in case notes`);
|
|
else
|
|
inboundEmailBody = caseInstance.recentNotes[0].content;
|
|
|
|
const modelResponse = await axios({
|
|
url: `${inferenceSettings_64f77978cbc0b66fc62838e4.nlu.apiBaseURL}Model/run/${req.params.workspaceId}/${req.params.branch}/${modelName}`,
|
|
method: "post",
|
|
data: {
|
|
input: inboundEmailBody,
|
|
conversationId: conversationId ? conversationId : undefined,
|
|
settings: externalSettings ? externalSettings : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.settings
|
|
}
|
|
});
|
|
console.log("modelResponse.data: [" + JSON.stringify(modelResponse.data) + "]")
|
|
|
|
await redis.set(`${req.body?.conversationId}`, `${modelResponse?.data?.conversationId}`, {
|
|
EX: 3600
|
|
});
|
|
if (inferenceSettings_64f77978cbc0b66fc62838e4.analytics) {
|
|
if (!conversationId) {
|
|
db.analytics.addConversation({
|
|
id: modelResponse?.data?.conversationId
|
|
});
|
|
}
|
|
db.analytics.addTransaction(modelResponse?.data);
|
|
}
|
|
var response = {
|
|
answers: modelResponse.data.answers ? modelResponse.data.answers : [],
|
|
predictions: {
|
|
topIntent: modelResponse.data.classificationResults.length ? modelResponse.data.classificationResults[0].label : null,
|
|
intents: modelResponse.data.classificationResults.length ? modelResponse.data.classificationResults.map(q => {
|
|
return {
|
|
label: q.label,
|
|
value: q.value
|
|
};
|
|
}) : [],
|
|
entities: modelResponse.data.nerResults.entities.length ? modelResponse.data.nerResults.entities.map(q => {
|
|
return {
|
|
option: q.option ? q.option : null,
|
|
type: q.type ? q.type : "sys",
|
|
entity: q,
|
|
text: q.sourceText,
|
|
accuracy: q.accuracy,
|
|
start: q.start,
|
|
end: q.end
|
|
};
|
|
}) : []
|
|
}
|
|
}
|
|
|
|
// Create the ACK against the Notifying Case
|
|
var notes = [
|
|
{
|
|
"createdBy": "IVA conversationId:" + modelResponse?.data?.conversationId,
|
|
"createdOn": `${(new Date().toISOString())}`,
|
|
"content": "IVA Acknowledging receipt of email"
|
|
|
|
}
|
|
];
|
|
await CACaseInterface().caseAction(caseId, "IVAAccepted", true, notes, []);
|
|
|
|
// Now create the case to send the response
|
|
notes = [
|
|
{
|
|
"@type" : "vcs:Note",
|
|
"content": "IVA Requesting sending of email to Customer"
|
|
|
|
}
|
|
];
|
|
|
|
var body;
|
|
if (response.answers?.length === 0) {
|
|
body = "Answer not available";
|
|
} else {
|
|
body = "";
|
|
response.answers.forEach(answer => {
|
|
body += "\n" + answer;
|
|
})
|
|
}
|
|
|
|
var entities = {
|
|
"InboundEmail": {
|
|
"@type": "vde:InboundEmail",
|
|
"subject": req.body.subject,
|
|
"body": caseInstance.recentNotes[0].content
|
|
},
|
|
"EmailResponse": {
|
|
"@type": "vde:EmailResponse",
|
|
"subject": "RE:" + req.body.subject,
|
|
"bodyRichText": body,
|
|
"conversationId" : `${modelResponse?.data?.conversationId}`
|
|
}
|
|
};
|
|
// TODO: Replace Hardcoded Customer Reference when search API is working again
|
|
var createCase = await CACaseInterface().createCase("vcs:IVAEmailAutoReply", "CC1000050", "IVA requesting send of email", notes, entities);
|
|
|
|
response.caseData = createCase;
|
|
|
|
console.log("Response: [" + JSON.stringify(response) + "]");
|
|
res.json(response);
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.json({
|
|
error: {
|
|
message: error.message
|
|
}
|
|
});
|
|
}
|
|
})(); |