-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (31 loc) · 1.28 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
const smsService = require('./services/sms.twilio.service');
exports.handler = async (event) =>{
return await sendMessagesBulk(event);
};
async function sendMessagesBulk(event) {
// Send SMS
if (!event) return sendRes(500, 'Internal server error');
// if (!event.body) return sendRes(500, 'Internal server error');
const params = event;
if (!params.toNumbers) return sendRes(500, 'Internal server error'); //aint supporting numbers directly
if (typeof params.toNumbers === 'string') return sendRes(500, 'Internal server error');
if (!params.message) return sendRes(500, 'Internal server error');
const smsOptions = {
sender: params.from,
messageText: params.message,
toNumbers: [],// {number, type}its good to store it at twilio if were gonna send more than 100 sms
};
if (typeof params.toNumbers === "object" && !Array.isArray(params.toNumbers)) {
params.toNumbers = [params.toNumbers];
}
smsOptions.toNumbers = params.toNumbers;
const result = await smsService.sendTwilioMessage(smsOptions);
return result.err ? sendRes(500, 'Internal server error'): sendRes(200, result);
}
const sendRes = (status, body) => {
const response = {
statusCode: status,
body: body
}
return response;
};