forked from microsoft/CopilotStudioSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomDirectLineInboundTransformer.js
More file actions
35 lines (30 loc) · 1.38 KB
/
Copy pathCustomDirectLineInboundTransformer.js
File metadata and controls
35 lines (30 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// ServiceNow Script Include: CustomDirectLineInboundTransformer
// Extends the default DirectLine transformer to trigger live agent handoff
// when a specific event ("handoff.initiate") is detected in the incoming payload.
var CustomDirectLineInboundTransformer = Class.create();
CustomDirectLineInboundTransformer.prototype = Object.extendsObject(
sn_va_bot_ic.DirectLinePrimaryBotIntegrationInboundTransformer,
{
/**
* Determines whether the conversation should be escalated to a live agent.
* This override looks for a specific Bot Framework event called "handoff.initiate".
*
* @returns {boolean} true if handoff event is detected; otherwise false.
*/
shouldConnectToAgent: function() {
// Safely retrieve the response object and activities array
var response = this._response || {};
var activities = response.activities || [];
// Use Array.prototype.some for event detection
var handoffDetected = activities.some(function(activity) {
return activity.type === "event" && activity.name === "handoff.initiate";
});
if (handoffDetected) {
gs.info("[CustomTransformer] Detected handoff.initiate event. Escalating to agent.");
return true;
}
return false;
},
// Type identifier for logging/debugging
type: 'CustomDirectLineInboundTransformer'
});