philnash / useful-twilio-functions

A set of useful Twilio Functions.
MIT License
54 stars 19 forks source link

Code to add SMS to Email with Mandrill #5

Open ZenCocoon opened 6 years ago

ZenCocoon commented 6 years ago

Based on the version of SMS to Email with Sendgrid, here's a version with Mandrill.

const got = require('got');

exports.handler = function(context, event, callback) {
    const requestBody = {
        key: `${context.MANDRILL_API_KEY}`,
        message: {
            to: [{ email: context.TO_EMAIL_ADDRESS, type: 'to' }],
            from_email: context.FROM_EMAIL_ADDRESS,
            subject: `New SMS message from: ${event.From}`,
            text: event.Body
        }
    };

    got
        .post('https://mandrillapp.com/api/1.0/messages/send.json', {
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(requestBody)
        })
        .then(response => {
            let twiml = new Twilio.twiml.MessagingResponse();
            callback(null, twiml);
        })
        .catch(err => {
            callback(err);
        });
};

Hope this helps, Thanks