Created
July 16, 2019 12:19
-
-
Save science/ba5f714b817cc07e927dcd5f7c268d64 to your computer and use it in GitHub Desktop.
Forward SMS from a twilio number to email using SendGrid API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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