Skip to content

Commit ba3a843

Browse files
committed
update response
1 parent d2b34e6 commit ba3a843

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
2-
"to": "your-number-here",
3-
"message": "Welcome to Serverless ⊂◉‿◉つ",
4-
"image": "https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"
2+
"body": {
3+
"to": "your-number-here",
4+
"message": "Welcome to Serverless ⊂◉‿◉つ",
5+
"image": "https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg"
6+
}
57
}

aws-node-twilio-send-text-message/frontend/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ <h3>Twilio Serverless Example</h3>
5252

5353
// post to API with native browser Fetch
5454
const getdata = fetch(API_ENDPOINT, {
55+
headers: {
56+
"Content-type": "application/json"
57+
},
5558
method: 'POST',
5659
body: data,
5760
mode: 'cors',
@@ -60,9 +63,10 @@ <h3>Twilio Serverless Example</h3>
6063

6164
getdata.then((response) => {
6265
response.json().then((data) => {
63-
console.log('Message:', data);
66+
console.log('Response:', data);
67+
const body = JSON.parse(data.body);
6468
messageDiv.textContent = '';
65-
messageDiv.textContent = data.message;
69+
messageDiv.textContent = (body && body.message) ? body.message : '';
6670
});
6771
});
6872
});

aws-node-twilio-send-text-message/handler.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ const twilioClient = require('twilio')(twilioAccountSid, twilioAuthToken);
66
module.exports.sendText = (event, context, callback) => {
77
// use twilio SDK to send text message
88
const sms = {
9-
to: event.to,
10-
body: event.message || '',
9+
to: event.body.to,
10+
body: event.body.message || '',
1111
from: twilioPhoneNumber,
1212
};
1313
// add image to sms if supplied
14-
if (event.image) {
15-
sms.mediaUrl = event.image;
14+
if (event.body.image) {
15+
sms.mediaUrl = event.body.image;
1616
}
1717
twilioClient.messages.create(sms, (error, data) => { // eslint-disable-line
1818
if (error) {
@@ -21,8 +21,10 @@ module.exports.sendText = (event, context, callback) => {
2121
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
2222
},
2323
statusCode: error.status,
24-
message: error.message,
25-
error: JSON.stringify(error),
24+
body: JSON.stringify({
25+
message: error.message,
26+
error: error // eslint-disable-line
27+
}),
2628
};
2729
return callback(null, errResponse);
2830
}
@@ -36,7 +38,8 @@ module.exports.sendText = (event, context, callback) => {
3638
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
3739
},
3840
body: JSON.stringify({
39-
message: data,
41+
message: 'Text message successfully sent!',
42+
data: data // eslint-disable-line
4043
}),
4144
};
4245

0 commit comments

Comments
 (0)