Created
November 2, 2018 04:31
-
-
Save ahmadnassri/9c1032697b2870dc6eebc5d2dfc29e34 to your computer and use it in GitHub Desktop.
Twilio Functions: Email to SMS with SendGrid Parse
This file contains hidden or 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 from = '+1xxxxxxxxxx' // your phone number | |
const sender = '[email protected]' // your email address | |
exports.handler = function (context, event, callback) { | |
// use the first part of the reply-to address as destination | |
const to = event.to.split('@').shift() | |
// take only the first line of the email body as the sms message | |
const body = event.text.split('\n').shift() | |
// display a log message for debugging | |
console.log('to: [%s] message: %s', to, body) | |
// parse message envelope | |
const envelope = JSON.parse(event.envelope) | |
// only accept emails from yourself | |
if (envelope.from !== sender) { | |
console.log('invlid sender: %s ', envelope.from) | |
// quietly fail | |
return callback() | |
} | |
// send sms using Twilio with quiet failure | |
context.getTwilioClient().messages.create({ from, to, body }, () => callback()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment