-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-csat-survey.js
65 lines (57 loc) · 2.48 KB
/
send-csat-survey.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
exports.handler = async function (context, event, callback) {
const client = context.getTwilioClient();
const CSAT_STUDIO_FLOW_SID = context.CSAT_STUDIO_FLOW_SID;
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const { interactionSid, channelSid, participantSid, conversationSid } = event;
// Create a custom Twilio Response
const response = new Twilio.Response();
// Set the CORS headers to allow Flex to make an error-free HTTP request
// to this Function
response.appendHeader('Access-Control-Allow-Origin', '*');
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
if (!interactionSid || !channelSid || !participantSid || !conversationSid) {
const errorMessage = `(${context.PATH}) Please provide all required parameters to continue`;
console.error(errorMessage);
response.appendHeader('Content-Type', 'plain/text');
response.setBody(errorMessage);
response.setStatusCode(500);
return callback(null, response);
}
try {
// Remove agent from the conversation
const removeAgentResult = await client.flexApi.v1
.interaction(interactionSid)
.channels(channelSid)
.participants(participantSid)
.update({ status: 'wrapup' });
// Set webhook to survey Studio flow
const webhookResult = await client.conversations.v1
.conversations(conversationSid)
.webhooks.create({
'configuration.method': 'POST',
'configuration.filters': ['onMessageAdded'],
'configuration.flowSid': CSAT_STUDIO_FLOW_SID,
target: 'studio',
});
// Include some delay so Copilot runs before including the CSAT message
await delay(2000);
// Send message
// When the customer replies, the Studio flow set above will receive it
const sendCSATMessageResult = await client.conversations.v1
.conversations(conversationSid)
.messages.create({
body: 'Como você avalia a sua interação com nosso agente hoje? (de 1 a 10)',
});
response.appendHeader('Content-Type', 'application/json');
response.setBody(sendCSATMessageResult);
callback(null, response);
} catch (error) {
const errorMessage = `(${context.PATH}) Unexpected error occurred: ${error}`;
console.error(errorMessage);
response.appendHeader('Content-Type', 'plain/text');
response.setBody(errorMessage);
response.setStatusCode(500);
return callback(null, response);
}
};