From 440b84edfc4d1645c545ac549bc6d363031d7762 Mon Sep 17 00:00:00 2001 From: "Peter.Morton" Date: Fri, 6 Jun 2025 12:55:10 -0500 Subject: [PATCH] Updating Context Service example --- CA_ContextService/README.md | 15 +++++ .../GlobalVariable/CA_ContextService.js | 60 +++++++++++++++++++ .../snippets/postContactHistory.js | 32 ++++++++++ 3 files changed, 107 insertions(+) create mode 100644 CA_ContextService/README.md create mode 100644 CA_ContextService/_studio_dependencies/GlobalVariable/CA_ContextService.js create mode 100644 CA_ContextService/snippets/postContactHistory.js diff --git a/CA_ContextService/README.md b/CA_ContextService/README.md new file mode 100644 index 0000000..c56ef56 --- /dev/null +++ b/CA_ContextService/README.md @@ -0,0 +1,15 @@ +# Interaction Notification API + +Interaction Notification API is used to push Call Pops into Channel Automation. + +## Prerequisits + +1. Install and configure the _Channel Automation Interface_ Package from Resource Center -> Hub. + +## Studio Dependencies + +[CA_ContextService](./_studio_dependencies/Global%20Variable/CA_ContextService.js) Global Variable Function + +## Example Usage + +Conversation Flow Code Block [postContextHistory](./snippets/postContactHistory.js) \ No newline at end of file diff --git a/CA_ContextService/_studio_dependencies/GlobalVariable/CA_ContextService.js b/CA_ContextService/_studio_dependencies/GlobalVariable/CA_ContextService.js new file mode 100644 index 0000000..e7fc49e --- /dev/null +++ b/CA_ContextService/_studio_dependencies/GlobalVariable/CA_ContextService.js @@ -0,0 +1,60 @@ +function toCustomISOString(date) { + // Ensure input is a Date object + if (!(date instanceof Date)) throw new Error("Input must be a Date object"); + // Get ISO string, then trim to 3 ms digits and add 'Z' + return date.toISOString().replace(/\d{3}Z$/, match => match.slice(0, 3) + 'Z'); +} + + +return { +/** + * + * @param {String} customerId Customer identifier (i.e. 10000050) + * @param {[String]} notes Array on notes + * @param {Date} startTime Start time of the interaction + * @param {Date} endTime End time of the interaction + * @param {String} external URL + */ + post(customerId, notes, startTime, endTime, url) { + return new Promise(async (resolve, reject) => { + try { + const OIDC = await CAInterface().getOIDC(); + let config = CAInterface().settings().url.CA; + const contextServiceURL = `https://${config.base}/context-service/${config.tenant}/contexts`; + + const { data } = await CAInterface().axios("contextService", { + method: "post", + url: contextServiceURL, + headers: { + "Content-Type": "application/ld+json", + Authorization: `OIDC_id_token ${OIDC.access_token}`, + }, + data: { + "@type": "vctx:Context", + "vctx:contacts": [ + { + "@type": "vctc:ExternalContact", + "vctc:subtype": "iva", + "vctc:direction": "vctc:inbound", + "vctc:startTime": toCustomISOString(startTime), + "vctc:endTime": toCustomISOString(endTime), + "vctc:externalUrl": url, + "vctc:notes": notes, + }, + ], + "vctx:customer": { + "@type": "vcust:Customer", + "vcust:identifier": `${customerId}`, + }, + }, + }); + console.log({ + contextServiceData: data, + }); + resolve(); + } catch (error) { + reject(error); + } + }); + }, +}; \ No newline at end of file diff --git a/CA_ContextService/snippets/postContactHistory.js b/CA_ContextService/snippets/postContactHistory.js new file mode 100644 index 0000000..955414f --- /dev/null +++ b/CA_ContextService/snippets/postContactHistory.js @@ -0,0 +1,32 @@ +(async () => { + const customerId = conversationData.customer?.customerId ?? "CC1000778"; + const history = await CAInterface().getConversationHistory( + conversationData.id, + 50 + ); + let notes = ["No History Found"]; + let startTime = new Date(); + if (history.messages.length > 0) { + notes = history.messages + .map((message) => `[${message.dateSent}] ${message.direction} : ${message.text}`) + .filter((text) => text.trim() !== ""); + startTime = new Date(history.messages[0]?.dateSent || startTime); + } + const endTime = new Date(); + const url = `https://ivastudio.verint.live/organizations/${recognizedObject.classificationResults.organizationId}/workspaces/${conversationData.workspaceId}/conversation/${conversationData.id}`; + CA_ContextService().post(customerId, notes, startTime, endTime, url); +})() + .catch((error) => { + console.log(error.message); + recognizedObject.answers.push(""); + recognizedObject.errorInfo = { + ...recognizedObject.errorInfo, + label: { + data: error.toJSON ? error.toJSON() : {}, + message: error.message, + }, + }; + }) + .finally(() => { + next(); + });