-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.protected.js
58 lines (46 loc) · 1.53 KB
/
callback.protected.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
const swapRoles = true;
const sgMail = require("@sendgrid/mail");
exports.handler = async (context, event, callback) => {
console.log(event);
sgMail.setApiKey(context.SENDGRID_API_SECRET);
const transcriptSid = event.transcript_sid;
const client = context.getTwilioClient();
try {
let transcript = await client.intelligence.v2
.transcripts(transcriptSid)
.fetch();
let participants = transcript.channel.participants;
sentences = await client.intelligence.v2
.transcripts(transcriptSid)
.sentences.list();
let participantMap = participants.reduce((acc, participant) => {
if (swapRoles) {
acc[participant.channel_participant === 1 ? 2 : 1] = participant.role;
} else {
acc[participant.channel_participant] = participant.role;
}
return acc;
}, {});
let concatenatedTranscripts = "";
for (let sentence of sentences) {
const role = participantMap[sentence.mediaChannel];
concatenatedTranscripts += `<p><strong>${role}:</strong> ${sentence.transcript}</p>\n`;
}
const msg = {
to: context.TO_EMAIL_ADDRESS,
from: {
name: "Voice Intelligence Transcripts",
email: context.FROM_EMAIL_ADDRESS,
},
html: `${concatenatedTranscripts}`,
subject: "New Voice Intelligence Transcript",
};
message = await sgMail.send(msg);
console.log("Email sent!");
} catch (error) {
console.error(error);
return callback(error);
}
let resp = "Email sent!";
return callback(null, resp);
};