Search code examples
twiliotwilio-functions

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


I'm new to Twilio. I'm attempting to forward an SMS to an email address using this tutorial:

https://www.twilio.com/blog/2017/07/forward-incoming-sms-messages-to-email-with-node-js-sendgrid-and-twilio-functions.html

I feel certain I've done everything it says to do, but I get an error 11200 HTTP retrieval failure every time, with these details:

{ "message": "Cannot find module 'got'", "name": "Error", "stack": "Error: Cannot find module 'got'\n at Function.Module._resolveFilename (module.js:547:15)\n at Function.Module._load (module.js:474:25)\n at Module.require (module.js:596:17)\n at Module.twilioRequire [as require] (/var/task/node_modules/enigma-lambda/src/dependency.js:28:21)\n at require (internal/module.js:11:18)\n at Object. (/var/task/handlers/ZFa37cc3db9fd8db0501c2e5fc92137969.js:1:75)\n
at Module._compile (module.js:652:30)\n at Object.Module._extensions..js (module.js:663:10)\n at Module.load (module.js:565:32)\n at tryModuleLoad (module.js:505:12)" }

I've tried making absolutely sure I have the function written the same as the tutorial. I copied it directly from the github page to be sure. I'm not sure how to proceed in troubleshooting this, it seems it's telling me that 'got' isn't found but it's supposed to be available in Twilio functions. Any ideas? Thanks.

Edit: Here is the code:

const got = require('got');

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

  got
    .post('https://api.sendgrid.com/v3/mail/send', {
      headers: {
        Authorization: `Bearer ${context.SENDGRID_API_KEY}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(requestBody)
    })
    .then(response => {
      let twiml = new Twilio.twiml.MessagingResponse();
      callback(null, twiml);
    })
    .catch(err => {
      callback(err);
    });
};

Solution

  • 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!