From 509abd9847b5bfbb07cbe4ac061d7ec2a9777245 Mon Sep 17 00:00:00 2001 From: Peter Morton Date: Mon, 3 Feb 2025 18:42:39 +0000 Subject: [PATCH] Initial Commit --- emailToCaseInference.js | 107 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 emailToCaseInference.js diff --git a/emailToCaseInference.js b/emailToCaseInference.js new file mode 100644 index 0000000..ada2710 --- /dev/null +++ b/emailToCaseInference.js @@ -0,0 +1,107 @@ +(async () => { + + try { + console.log("Request: [" + JSON.stringify(req.body) + "]") + + const conversationId = req.body.conversationId; + const caseId = req.body.caseId; + 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); + console.log(`Case Note: [" + ${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: caseInstance.recentNotes[0].content, + conversationId: conversationId ? conversationId : undefined, + settings: externalSettings ? externalSettings : inferenceSettings_64f77978cbc0b66fc62838e4.nlu.settings + } + }); + 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 Studio conversationId:" + conversationId, + "createdOn": "2022-06-21T13:38:12Z", // TODO: create timestamp + "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 = response.answers[0]; + + var entities = { + "InboundEmail": { + "@type": "vde:InboundEmail", + "subject": "TODO: get this from the case", + "body": caseInstance.recentNotes[0].content + }, + "EmailResponse": { + "@type": "vde:EmailResponse", + "subject": "LIMITATION: CA Case Send Email does not allow setting of subject by dynamic entities", + "body": body + } + }; + 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 + } + }); + } +})(); \ No newline at end of file