forked from serverless/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
48 lines (45 loc) · 1.48 KB
/
handler.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
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
const twilioAuthToken = process.env.TWILIO_AUTH_TOKEN;
const twilioPhoneNumber = process.env.TWILIO_PHONE_NUMBER;
const twilioClient = require('twilio')(twilioAccountSid, twilioAuthToken);
module.exports.sendText = (event, context, callback) => {
// use twilio SDK to send text message
const sms = {
to: event.body.to,
body: event.body.message || '',
from: twilioPhoneNumber,
};
// add image to sms if supplied
if (event.body.image) {
sms.mediaUrl = event.body.image;
}
twilioClient.messages.create(sms, (error, data) => { // eslint-disable-line
if (error) {
const errResponse = {
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
statusCode: error.status,
body: JSON.stringify({
message: error.message,
error: error // eslint-disable-line
}),
};
return callback(null, errResponse);
}
// text message sent! ✅
console.log(`message: ${data.body}`); // eslint-disable-line
console.log(`date_created: ${data.date_created}`); // eslint-disable-line
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify({
message: 'Text message successfully sent!',
data: data // eslint-disable-line
}),
};
callback(null, response);
});
};