Created
July 26, 2021 18:56
-
-
Save r0yfire/9f4d04cb3b42411f109955956e8ea54e to your computer and use it in GitHub Desktop.
Twilio serverless function to forward incoming SMS to Email
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 = { | |
to: context.TO_EMAIL_ADDRESS, | |
from: context.FROM_EMAIL_ADDRESS, | |
subject: `New SMS message from: ${event.From}`, | |
text: event.Body, | |
}; | |
const apiKey = `Basic ${Buffer.from(`api:${context.MAILGUN_API_KEY}`).toString('base64')}`; | |
got | |
.post('https://api.mailgun.net/v3/notice.autohost.ai/messages', { | |
headers: { | |
Authorization: apiKey, | |
}, | |
body: requestBody, | |
}) | |
.then(response => { | |
let twiml = new Twilio.twiml.MessagingResponse(); | |
callback(null, twiml); | |
}) | |
.catch(err => { | |
console.log(err); | |
callback(err); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment