UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

174 lines 9.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CXONE_HANDOVER = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../../createNodeDescriptor"); const cxOneTmsPayload_1 = require("../libs/cxOneTmsPayload"); const cxOneChannelUtils_1 = require("../libs/cxOneChannelUtils"); const cxOneConfig_1 = require("../libs/cxOneConfig"); const prepareParams_1 = require("../libs/prepareParams"); /** * Node name: 'cxOneHandover' * * Purpose: * - Finish or handover the session to CXone * - Send the conversation transcript to CXone */ exports.CXONE_HANDOVER = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "cxOneHandover", defaultLabel: "Exit Interaction", summary: "UI__NODE_EDITOR__CXONE_HANDOVER__SUMMARY", preview: { key: "action", type: "text", }, fields: [ { key: "action", label: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__ACTION__LABEL", type: "select", description: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__ACTION__DESCRIPTION", params: { options: [ { label: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__ACTION__OPTIONS__ESCALATE__LABEL", value: "Escalate" }, { label: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__ACTION__OPTIONS__END__LABEL", value: "End" }, ], required: true, }, }, { key: "businessNumber", label: "UI__NODE_EDITOR__CXONE__SHARED__BUSINESS_NUMBER__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__CXONE__SHARED__BUSINESS_NUMBER__DESCRIPTION", params: { required: true, }, defaultValue: "", }, { key: "contactId", label: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__CONTACT_ID__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__CONTACT_ID__DESCRIPTION", params: { required: true, }, }, { key: "spawnedContactId", label: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__SPAWNED_CONTACT_ID__LABEL", type: "cognigyText", description: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__SPAWNED_CONTACT_ID__DESCRIPTION", params: { required: true, }, }, { key: "optionalParamsObject", label: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__OPTIONAL_PARAMS__LABEL", type: "json", description: "UI__NODE_EDITOR__CXONE_HANDOVER__FIELDS__OPTIONAL_PARAMS__DESCRIPTION", defaultValue: "[]", params: { required: false, }, }, ], form: [ { type: "field", key: "action" }, { type: "field", key: "businessNumber" }, { type: "field", key: "contactId" }, { type: "field", key: "spawnedContactId" }, { type: "field", key: "optionalParamsObject" }, ], appearance: { color: "#3694FD", }, tags: ["nice", "cxone"], function: async ({ cognigy, config }) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s; const { api, input, context } = cognigy; const { action, businessNumber, contactId, spawnedContactId, optionalParamsObject } = config; // Validate action if (!action || (action !== "End" && action !== "Escalate")) { (_a = api.output) === null || _a === void 0 ? void 0 : _a.call(api, "cxOneHandover Error: Missing or invalid Action parameter", { error: "Missing or invalid Action parameter" }); throw new Error("cxOneHandover: Validation: Missing or invalid Action parameter"); } (_b = api.log) === null || _b === void 0 ? void 0 : _b.call(api, "info", `cxOneHandover: Contact ID: ${contactId}; Spawned Contact ID: ${spawnedContactId}; Action: ${action}`); try { const channel = (input === null || input === void 0 ? void 0 : input.channel) || ""; (_c = api.log) === null || _c === void 0 ? void 0 : _c.call(api, "info", `cxOneHandover: Interaction channel: ${channel}`); const isVoice = (0, cxOneChannelUtils_1.isVoiceChannel)(input); (_d = api.log) === null || _d === void 0 ? void 0 : _d.call(api, "info", `cxOneHandover: isVoice: ${isVoice}`); // Prepare optional parameters const finalParams = (0, prepareParams_1.prepareParams)(optionalParamsObject, api.log, "cxOneHandover"); (_e = api.log) === null || _e === void 0 ? void 0 : _e.call(api, "info", `cxOneHandover: prepared optional parameters: ${JSON.stringify(finalParams)}`); // Handle voice channel handover if (contactId && spawnedContactId && isVoice && contactId !== cxOneConfig_1.SENTINEL_CONTACT_ID && spawnedContactId !== cxOneConfig_1.SENTINEL_CONTACT_ID) { const apiClient = await api.getCxOneApiClient(); // Send transcript to TMS if available const transcript = (input === null || input === void 0 ? void 0 : input.transcript) || (context === null || context === void 0 ? void 0 : context.transcript) || ""; if (transcript && Array.isArray(transcript) && transcript.length > 0) { (_f = api.log) === null || _f === void 0 ? void 0 : _f.call(api, "info", `cxOneHandover: got transcript`); try { const tmsPayload = (0, cxOneTmsPayload_1.default)(transcript, action, contactId, businessNumber); (_g = api.log) === null || _g === void 0 ? void 0 : _g.call(api, "info", `cxOneHandover: transformed transcript to TMS payload: ${JSON.stringify(tmsPayload)}`); const tmsStatus = await apiClient.postTranscript(tmsPayload); (_h = api.log) === null || _h === void 0 ? void 0 : _h.call(api, "info", `cxOneHandover: posted transcript to TMS for contactId: ${contactId}; status: ${tmsStatus}`); } catch (tmsError) { // Don't fail the entire handover if transcript posting fails - log the error and continue with the handover signal (_j = api.log) === null || _j === void 0 ? void 0 : _j.call(api, "error", `cxOneHandover: Error posting transcript to TMS for contactId: ${contactId}; error: ${tmsError.message}`); } } // Send handover signal const signalStatus = await apiClient.sendSignalHandover(spawnedContactId || contactId, action, finalParams); (_k = api.log) === null || _k === void 0 ? void 0 : _k.call(api, "info", `cxOneHandover: sent signal to CXone for contactId: ${spawnedContactId || contactId}; action: ${action}; status: ${signalStatus}`); (_l = api.addToContext) === null || _l === void 0 ? void 0 : _l.call(api, "CXoneHandover", { success: true, action, contactId: spawnedContactId || contactId, status: signalStatus }, "simple"); } // Output the handover action to NiCE channel for CXone Guide Chat if (!isVoice && contactId && contactId !== cxOneConfig_1.SENTINEL_CONTACT_ID) { const ndata = { _cognigy: { _niceCXOne: { json: { text: "", uiComponent: {}, data: { Intent: action, }, action: action === "End" ? "END_CONVERSATION" : "AGENT_TRANSFER", }, }, }, }; if (Array.isArray(finalParams) && finalParams.length) { ndata._cognigy._niceCXOne.json.data.Params = finalParams.join("|"); } (_m = api.output) === null || _m === void 0 ? void 0 : _m.call(api, "", ndata); (_o = api.log) === null || _o === void 0 ? void 0 : _o.call(api, "info", `cxOneHandover: Done. Output data was sent to CXone Guide Chat channel: ${JSON.stringify(ndata)}`); } else { (_p = api.log) === null || _p === void 0 ? void 0 : _p.call(api, "info", `cxOneHandover: Done. No output data sent to Voice / Cognigy Webchat / Cognigy Testchat channel.`); } // Wait before returning control to avoid unwanted messages during handover await new Promise(resolve => setTimeout(resolve, cxOneConfig_1.HANDOVER_DELAY_MS)); return; } catch (error) { const errorMessage = error.message || "Unknown error"; (_q = api.log) === null || _q === void 0 ? void 0 : _q.call(api, "error", `cxOneHandover: Error signaling CXone with: '${action}' for contactId: ${spawnedContactId || contactId}; error: ${errorMessage}`); (_r = api.addToContext) === null || _r === void 0 ? void 0 : _r.call(api, "CXoneHandover", { success: false, action, contactId: spawnedContactId || contactId, error: errorMessage }, "simple"); (_s = api.output) === null || _s === void 0 ? void 0 : _s.call(api, "Something is not working. Please retry.", { error: errorMessage }); throw error; } }, }); //# sourceMappingURL=cxOneHandover.js.map