createSocketTransformer({ handleInput: async ({ payload, endpoint }) => { let text = payload.text; const sessionStorage = await getSessionStorage(payload.userId, payload.sessionId); if (sessionStorage.hasOwnProperty("chatHistory")) { sessionStorage.chatHistory += "\nUser: " + text; } else { sessionStorage.chatHistory = "User: " + text; } if (sessionStorage.cognigyState == "OPENAGENT" || sessionStorage.cognigyState == "OPENBOT") { console.log("Redirecting to ServiceNow"); let body = { "token": sessionStorage.serviceNowAPIToken, "requestId": sessionStorage.cognigyInputId, "action": null, "enterpriseId": "Cognigy", "clientSessionId": payload.sessionId, "botToBot": true, "clientVariables": { "cognigyState": sessionStorage.cognigyState, "cognigyInputId": sessionStorage.cognigyInputId, "cognigyUserId": sessionStorage.cognigyUserId }, "message": { "text": payload.text, "typed": true, "clientMessageId": sessionStorage.cognigyInputId }, "userId": sessionStorage.userId, "emailId": sessionStorage.emailId, "timezone": sessionStorage.timezone } if (payload.text?.toLowerCase() == sessionStorage.quitPhrase?.toLowerCase()) { body.action = "END_CONVERSATION"; body.message.text = ""; body.clientVariables.cognigyState = "CLOSED"; sessionStorage.cognigyState = "CLOSED"; } try { const result = await httpRequest({ uri: sessionStorage.serviceNowInstanceURL + "/api/sn_va_as_service/bot/integration", method: "POST", headers: { 'Authorization': sessionStorage.authorization, 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body, json: true }); return null; } catch (e) { text = "Cannot connect to ServiceNow. Check connection details in Start_Conversation node ...\n" + e.message; } } return { userId: payload.userId, sessionId: payload.sessionId, text: text, data: payload.data }; }, handleOutput: async ({ processedOutput, output, endpoint, userId, sessionId }) => { const sessionStorage = await getSessionStorage(userId, sessionId); if (sessionStorage.hasOwnProperty("chatHistory")) { sessionStorage.chatHistory += "\nBot: " + output.text; } else { sessionStorage.chatHistory = "Bot: " + output.text; } return processedOutput; }, handleNotify: async ({ request, response, endpoint }) => { if (!request.body.clientVariables.hasOwnProperty('cognigyState')) return null; const state = request.body.clientVariables.cognigyState; if (state != "STARTAGENT" && state != "STARTBOT" && state != "OPENAGENT" && state != "OPENBOT" && state != "CLOSED") return null; let text = ""; let data = {}; const userId = request.body.clientVariables.cognigyUserId; const sessionId = request.body.clientSessionId; const sessionStorage = await getSessionStorage(userId, sessionId); console.log("state=" + state); console.log("request.body.agentChat=" + request.body.agentChat); console.log("request.body.completed=" + request.body.completed); if (state == "STARTAGENT" || state == "STARTBOT" || (state == "OPENAGENT" && request.body.completed) || (state == "OPENBOT" && request.body.completed)) { if (state == "STARTAGENT" || state == "STARTBOT") { sessionStorage.serviceNowInstanceURL = request.body.clientVariables.serviceNowInstanceURL; sessionStorage.serviceNowAPIToken = request.body.clientVariables.serviceNowAPIToken; sessionStorage.authorization = request.body.clientVariables.authorization; console.log("Authorization: " + sessionStorage.authorization); sessionStorage.userId = request.body.clientVariables.userId; sessionStorage.emailId = request.body.clientVariables.emailId; sessionStorage.timezone = request.body.clientVariables.timezone; sessionStorage.quitPhrase = request.body.clientVariables.quitPhrase; sessionStorage.cognigyUserId = userId; sessionStorage.botQuitPhrase = request.body.clientVariables.botQuitPhrase; } const body = { "token": sessionStorage.serviceNowAPIToken, "requestId": sessionStorage.cognigyInputId, "action": "AGENT", "enterpriseId": "Cognigy", "clientSessionId": sessionId, "botToBot": true, "clientVariables": { "cognigyState": "CLOSED", "cognigyInputId": sessionStorage.cognigyInputId, "cognigyUserId": userId, }, "message": { "text": null, "typed": true, "clientMessageId": sessionStorage.cognigyInputId }, "userId": sessionStorage.userId, "emailId": sessionStorage.emailId, "timezone": sessionStorage.timezone } if (state == "STARTAGENT") { const message = sessionStorage.chatHistory; body.requestId = request.body.clientVariables.cognigyInputId + "/H"; body.clientVariables.cognigyState = "OPENAGENT"; body.clientVariables.cognigyInputId = request.body.clientVariables.cognigyInputId + "/H"; body.message.clientMessageId = request.body.clientVariables.cognigyInputId + "/H"; body.message.text = message; sessionStorage.cognigyInputId = request.body.clientVariables.cognigyInputId + "/H"; try { const result = await httpRequest({ uri: request.body.clientVariables.serviceNowInstanceURL + "/api/sn_va_as_service/bot/integration", method: "POST", headers: { 'Authorization': sessionStorage.authorization, 'Content-Type': 'application/json', 'Accept': 'application/json' }, body: body, json: true }); } catch (e) { text = "Cannot connect to ServiceNow to initiate the conversation with Livechat. Check connection details in Start_Conversation node ...\n" + e.message; return { userId, sessionId, text, data }; } sessionStorage.cognigyState = "OPENAGENT"; return null; } if (state == "STARTBOT") { sessionStorage.cognigyState = "OPENBOT"; sessionStorage.cognigyInputId = request.body.clientVariables.cognigyInputId + "/H"; text = request.body.body[request.body.body.length - 1].promptMsg; return { userId, sessionId, text, data }; } if (state == "OPENAGENT" && request.body.completed) { sessionStorage.cognigyState = "CLOSED"; text = request.body.body[request.body.body.length - 1].value; return { userId, sessionId, text, data }; } if (state == "OPENBOT" && request.body.completed) { sessionStorage.cognigyState = "CLOSED"; text = request.body.body[request.body.body.length - 1].value; return { userId, sessionId, text, data }; } } sessionStorage.cognigyInputId = request.body.clientVariables.cognigyInputId + "*"; for (let row in request.body.body) { if (request.body.body[row].uiType == "OutputText") { text += request.body.body[row].value + "\n"; } if (request.body.body[row].uiType == "OutputCard") { let data = JSON.parse(request.body.body[row].data); text += data.title + "\n"; for (let field in data.fields) { console.log("Hello"); text += data.fields[field].fieldLabel + " - " + data.fields[field].fieldValue + "\n"; } } if (request.body.body[row].uiType == "InputText") { text += request.body.body[row].label + "\n"; } } if (sessionStorage.botQuitPhrase != null && sessionStorage.botQuitPhrase != "") { if (text.includes(sessionStorage.botQuitPhrase)) sessionStorage.cognigyState = "CLOSED"; } return { userId, sessionId, text, data }; } });