Created
November 2, 2018 04:26
-
-
Save ahmadnassri/73f7f76469d864a95514beabafc04db7 to your computer and use it in GitHub Desktop.
Death to SMS
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 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()) | |
} |
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
// import sendgrid library | |
const sendgrid = require('@sendgrid/client') | |
// target email address | |
const email = '[email protected]' | |
// sms email domain | |
const domain = 'sms.domain.tld' | |
exports.handler = function (context, event, done) { | |
// authenticate with token | |
sendgrid.setApiKey(context.SENDGRID_API_KEY) | |
// configure request object | |
const request = { | |
method: 'POST', | |
url: '/v3/mail/send', | |
body: { | |
personalizations: [{ | |
to: [{ email }] | |
}], | |
from: { | |
email: `${event.From}@${domain}` | |
}, | |
subject: `RE: SMS: ${event.From}`, | |
content: [{ | |
type: 'text/plain', value: event.Body | |
}] | |
} | |
} | |
sendgrid | |
.request(request) // send the email | |
.then(() => done(null, new Twilio.twiml.MessagingResponse())) // send TwiML output | |
.catch(done) // quiet failure | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment