Skip to content

Instantly share code, notes, and snippets.

@science
Created July 16, 2019 12:19
Show Gist options
  • Save science/ba5f714b817cc07e927dcd5f7c268d64 to your computer and use it in GitHub Desktop.
Save science/ba5f714b817cc07e927dcd5f7c268d64 to your computer and use it in GitHub Desktop.
Forward SMS from a twilio number to email using SendGrid API
const got = require('got');
exports.handler = function(context, event, callback) {
const requestBody = {
personalizations: [{ to: [{ email: context.TO_EMAIL_ADDRESS }] }],
from: { email: context.FROM_EMAIL_ADDRESS },
subject: `New SMS message from: ${event.From}`,
content: [
{
type: 'text/plain',
value: event.Body
}
]
};
got.post('https://api.sendgrid.com/v3/mail/send', {
headers: {
Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(requestBody)
})
.then(response => {
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
})
.catch(err => {
callback(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment