-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
211 lines (166 loc) · 5.04 KB
/
index.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
200
201
202
203
204
205
206
207
208
209
210
211
let AWS = require('aws-sdk');
//
// Create the DynamoDB object.
//
let ddb = new AWS.DynamoDB.DocumentClient({
apiVersion: '2012-08-10',
region: process.env.AWS_REGION
});
//
// Create the Simple Notification System object.
//
let sns = new AWS.SNS({
apiVersion: '2010-03-31'
});
//
// This function will be triggered by connect with all the details of the
// conversation, so in this case we can save the message left and send
// it to SNS.
//
exports.handler = (event) => {
return new Promise(function(resolve, reject) {
//
// 1. Create a container to pass around the promises.
//
let container = {
//
// Organize all the data that we get so we know what
// this code uses.
//
req: {
phone_nr: event.Details.ContactData.CustomerEndpoint.Address,
message: event.Details.ContactData.Attributes.Message,
name: event.Details.ContactData.Attributes.first_name || "N/A"
},
//
// The message to be sent.
//
message: "",
//
// Hold the response for Connect.
//
res: 'OK'
}
//
// -> Start the chain.
//
save_the_message(container)
.then(function(container) {
return create_the_message(container);
}).then(function(container) {
return send_the_message(container);
}).then(function(container) {
return resolve(container.res);
}).catch(function(error) {
return reject(error);
});
});
};
// _____ _____ ____ __ __ _____ _____ ______ _____
// | __ \ | __ \ / __ \ | \/ | |_ _| / ____| | ____| / ____|
// | |__) | | |__) | | | | | | \ / | | | | (___ | |__ | (___
// | ___/ | _ / | | | | | |\/| | | | \___ \ | __| \___ \
// | | | | \ \ | |__| | | | | | _| |_ ____) | | |____ ____) |
// |_| |_| \_\ \____/ |_| |_| |_____| |_____/ |______| |_____/
//
//
// This lambda saves the name that the user over the phone said.
//
function save_the_message(container) {
return new Promise(function(resolve, reject) {
console.info('save_the_message');
//
// 1. Get the actual time, to organize our data in DDB.
//
let timestamp = Math.floor(Date.now() / 1000);
//
// 2. Prepare the query.
//
let params = {
TableName: "0x4447_connect_sessions",
Item: {
id: container.req.phone_nr,
type: 'message#' + timestamp,
message: container.req.message,
timestamp_created: timestamp
},
};
//
// 3. Execute the query
//
ddb.put(params, function(error, data) {
//
// 1. Check if there were any errors.
//
if(error)
{
console.info(params);
return reject(error);
}
//
// -> Move to the next Promise.
//
return resolve(container);
});
});
};
function create_the_message(container) {
return new Promise(function(resolve, reject) {
console.info('create_the_message');
//
// 1. Use an array to make a message since it is easier to work with.
//
let message = [
"Greetings Human,", "\n",
"\n",
"You just got a new voice message, here are the details:", "\n",
"\n",
"- Name: " + container.req.name, "\n",
"- Phone Nr.: " + container.req.phone_nr, "\n",
"- Message: " + container.req.message, "\n",
"\n",
"Enjoy your existence."
]
//
// 2. Combine the array in to a single string.
//
container.message = message.join('');
//
// -> Move to the next Promise.
//
return resolve(container);
});
};
//
// Send a message to whoever is interest to know that a new phone call happened.
//
function send_the_message(container) {
return new Promise(function(resolve, reject) {
console.info('send_the_message');
//
// 1. Prepare the query.
//
let params = {
Subject: 'New message on your voice mail.',
Message: container.message,
TopicArn: process.env.SNS_TOPIC
};
//
// -> Execute the query.
//
sns.publish(params, function(error, data) {
//
// 1. Check if there were any errors.
//
if(error)
{
console.info(params);
return reject(error);
}
//
// -> Move to the next Promise.
//
return resolve(container);
});
});
};