Twilio Forward SMS to email - Cannot find module 'got'

只愿长相守 提交于 2019-12-04 14:50:14

First, the above code with got works with my Twilio and SendGrid accounts, I just tested, I don't know why you're having trouble..., maybe try to create a Twilio subaccount and run from there.


Second, if you still can't get got to work, here is some code, you could try, and I'we also tested and it works. It's using https instead:


const https = require('https');

exports.handler = function (context, event, callback) {

    let postData = JSON.stringify({
        personalizations: [{
            to: [{
                email: 'somebody@gmail.com'
            }]
        }],
        from: {
            email: 'somebody@gmail.com'
        },
        subject: `New SMS message from: ${event.From}`,
        content: [{
            type: 'text/plain',
            value: event.Body
        }]
    });

    let postOptions = {
        host: 'api.sendgrid.com',
        port: '443',
        path: '/v3/mail/send',
        method: 'POST',
        headers: {
            'Authorization': 'Bearer YOUR_API_KEY',
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData),
        }
    };

    let req = https.request(postOptions, function (res) {
        // some code to handle the async response if needed
        let twiml = new Twilio.twiml.MessagingResponse();
        callback(null, twiml);
    });

    req.write(postData);
    req.end();

};

Good luck!

After a little more research, I found some comments on GitHub that indicate 'got' is no longer included by default in the Twilio dependencies. Per the instructions there I went to the Runtime Functions Config section of the Twilio console and added got version 6.7.1 and now the original source code works!

I prefer Alex's solution however since it works "out of the box" and I'm keeping it as the accepted answer.

I was able to get this to work by making sure that "got" is installed as a dependency under these settings here: https://www.twilio.com/console/runtime/functions/configure

Functions Screenshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!