This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinbound-routing.protected.js
77 lines (73 loc) · 2.95 KB
/
inbound-routing.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const sfdcAuthenticatePath = Runtime.getFunctions()['auth/sfdc-authenticate'].path;
const { sfdcAuthenticate } = require(sfdcAuthenticatePath);
exports.handler = async function (context, event, callback) {
const twilioClient = context.getTwilioClient();
let response = new Twilio.Response();
response.appendHeader('Content-Type', 'application/json');
const conversationSid = event.ConversationSid;
const workerNumber = event['MessagingBinding.ProxyAddress'];
const sfdcConnectionIdentity = await sfdcAuthenticate(context, null); // this is null due to no user context, default to env. var SF user
const { connection } = sfdcConnectionIdentity;
await routeConversation(context, twilioClient, conversationSid, workerNumber, connection);
return callback(null, response);
};
const routeConversation = async (context, twilioClient, conversationSid,
workerNumber, sfdcConn) => {
let workerIdentity = await getContactOwnerByNumber(workerNumber, sfdcConn);
if (!workerIdentity) { // Customer doesn't have a worker
// Select a default worker
workerIdentity = context.DEFAULT_WORKER;
}
await routeConversationToWorker(twilioClient, conversationSid, workerIdentity);
}
const routeConversationToWorker = async (twilioClient, conversationSid, workerIdentity) => {
// Add worker to the conversation with a customer
console.log('Conversation SID: ', conversationSid);
const participant = await twilioClient.conversations
.conversations(conversationSid)
.participants
.create({ identity: workerIdentity });
console.log('Created agent participant: ', participant.sid);
}
const getContactOwnerByNumber = async (number, sfdcConn) => {
console.log('Getting Contact Owner by #: ', number);
let sfdcRecords = [];
try {
// Use below query if looking up Worker identity based on Twilio proxy #
sfdcRecords = await sfdcConn.sobject("User")
.find(
{
'MobilePhone': number
},
{
'Username': 1,
}
)
.sort({ LastModifiedDate: -1 })
.limit(1)
.execute();
/* Use below query if looking up Contact owner by Contact phone #
sfdcRecords = await sfdcConn.sobject("Contact")
.find(
{
'MobilePhone': number
},
{
'Owner.Username': 1,
}
)
.sort({ LastModifiedDate: -1 })
.limit(1)
.execute();
*/
console.log("Fetched # SFDC records for contact owner by #: " + sfdcRecords.length);
if (sfdcRecords.length === 0) {
return;
}
const sfdcRecord = sfdcRecords[0];
console.log('Matched to worker: ' + sfdcRecord.Username);
return sfdcRecord.Username;
} catch (err) {
console.error(err);
}
};