forked from cweems/function-frontline-salesforce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinbound-conversation.protected.js
199 lines (183 loc) · 6.68 KB
/
inbound-conversation.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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 customerNumber =
event["MessagingBinding.Address"] &&
event["MessagingBinding.Address"].startsWith("whatsapp:")
? event["MessagingBinding.Address"].substring(9)
: event["MessagingBinding.Address"];
switch (event.EventType) {
case "onConversationAdd": {
const isIncomingConversation = !!customerNumber;
//console.log(event);
if (isIncomingConversation) {
const sfdcConnectionIdentity = await sfdcAuthenticate(context, null); // this is null due to no user context, default to env. var SF user
const { connection } = sfdcConnectionIdentity;
const customerDetails =
(await getCustomerByNumber(customerNumber, connection)) || {};
const conversationProperties = {
friendly_name: customerDetails.display_name || customerNumber,
};
response.setBody(conversationProperties);
}
break;
}
case "onParticipantAdded": {
const conversationSid = event.ConversationSid;
const participantSid = event.ParticipantSid;
const isCustomer = customerNumber && !event.Identity;
const sfdcConnectionIdentity = await sfdcAuthenticate(context, null);
const { connection } = sfdcConnectionIdentity;
if (isCustomer) {
const customerParticipant = await twilioClient.conversations
.conversations(conversationSid)
.participants.get(participantSid)
.fetch();
const customerDetails =
(await getCustomerByNumber(customerNumber, connection)) || {};
await setCustomerParticipantProperties(
customerParticipant,
customerDetails
);
// Writeback to SF
sfdcParticipant = await connection
.sobject("Frontline_Conversation_Participant_Custo__c")
.create({
AccountSid__c: event.AccountSid,
ConversationSid__c: event.ConversationSid,
CustomerId__c: customerDetails.customer_id,
CustomerPhone__c: event["MessagingBinding.Address"],
ProxyPhone__c: event["MessagingBinding.ProxyAddress"],
DateCreated__c: event.DateCreated,
DateUpdated__c: event.DateCreated,
ClientIdentity__c: event.ClientIdentity ? event.ClientIdentity : "",
RoleSid__c: event.RoleSid,
ParticipantSid__c: event.ParticipantSid,
Source__c: event.Source,
});
console.log(`New Participant Customer : ${sfdcParticipant.id}`);
} else {
sfdcParticipant = await connection
.sobject("Frontline_Conversation_Participant_User__c")
.create({
AccountSid__c: event.AccountSid,
ConversationSid__c: event.ConversationSid,
UserName__c: event.Identity,
DateCreated__c: event.DateCreated,
DateUpdated__c: event.DateCreated,
ClientIdentity__c: event.ClientIdentity ? event.ClientIdentity : "",
RoleSid__c: event.RoleSid,
ParticipantSid__c: event.ParticipantSid,
Source__c: event.Source,
});
console.log(`New Participant User : ${sfdcParticipant.id}`);
}
break;
}
case "onConversationAdded": {
const sfdcConnectionIdentity = await sfdcAuthenticate(context, null);
const { connection } = sfdcConnectionIdentity;
sfdcConversation = await connection
.sobject("Frontline_Conversation__c")
.create({
AccountSid__c: event.AccountSid,
Attributes__c: event.Attributes,
ChatServiceSid__c: event.ChatServiceSid,
ConversationSid__c: event.ConversationSid,
DateCreated__c: event.DateCreated,
FriendlyName__c: event.FriendlyName,
MessagingServiceSid__c: event.MessagingServiceSid,
Source__c: event.Source,
State__c: event.State,
});
console.log(`New Conversation: ${sfdcConversation.id}`);
break;
}
case "onMessageAdded": {
const sfdcConnectionIdentity = await sfdcAuthenticate(context, null);
const { connection } = sfdcConnectionIdentity;
console.log(event);
sfdcMessage = await connection
.sobject("Frontline_Conversation_Message_Log__c")
.create({
AccountSid__c: event.AccountSid,
Attributes__c: event.Attributes,
Author__c: event.Author__c,
ConversationSid__c: event.ConversationSid,
DateCreated__c: event.DateCreated,
Body__c: event.Body,
Index__c: event.Index,
MessageSid__c: event.MessageSid,
Source__c: event.Source,
});
console.log(`New Message: ${sfdcMessage.id}`);
break;
}
default: {
console.log("Unknown event type: ", event.EventType);
console.log(event);
response.setStatusCode(200);
}
}
return callback(null, response);
};
const getCustomerByNumber = async (number, sfdcConn) => {
console.log("Getting Customer details by #: ", number);
let sfdcRecords = [];
try {
sfdcRecords = await sfdcConn
.sobject("Account")
.find(
{
Phone: number,
},
{
Id: 1,
Name: 1,
}
)
.sort({ LastModifiedDate: -1 })
.limit(1)
.execute();
console.log(
"Fetched # SFDC records for contact by #: " + sfdcRecords.length
);
if (sfdcRecords.length === 0) {
return;
}
const sfdcRecord = sfdcRecords[0];
return {
display_name: sfdcRecord.Name,
customer_id: sfdcRecord.Id,
};
} catch (err) {
console.error(err);
}
};
const setCustomerParticipantProperties = async (
customerParticipant,
customerDetails
) => {
const participantAttributes = JSON.parse(customerParticipant.attributes);
console.log(participantAttributes);
const customerProperties = {
attributes: JSON.stringify({
...participantAttributes,
customer_id:
participantAttributes.customer_id || customerDetails.customer_id,
display_name:
participantAttributes.display_name || customerDetails.display_name,
}),
};
// If there is difference, update participant
if (customerParticipant.attributes !== customerProperties.attributes) {
// Update attributes of customer to include customer_id
await customerParticipant
.update(customerProperties)
.catch((e) => console.log("Update customer participant failed: ", e));
}
};