3

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);
    });
};

3 Answers3

3

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.

  • Hello, I wrote the blog post that you linked at the start of your question and I am going to go update it right now. I have known about this for a little while, so I apologise for not getting to it. – philnash Jun 14 '18 at 06:05
  • 1
    Updated the blog post and the GitHub repo where you found the comments (that's also mine, sorry!). – philnash Jun 14 '18 at 06:18
  • @philnash The code at https://github.com/philnash/useful-twilio-functions/blob/master/forward-message-as-email/sendgrid.js is dated 05/07/2017 and still doesn't work. This is my first attempt to use Twilio and I'm unimpressed with the documentation. – cja Nov 26 '18 at 09:46
  • Hi @cja, What doesn't work abou that code for you? Did you add `got` as a dependency in your [Functions configuration page](https://www.twilio.com/console/runtime/functions/configure) as described in [the README](https://github.com/philnash/useful-twilio-functions/tree/master/forward-message-as-email#dependencies)? – philnash Nov 26 '18 at 12:08
2

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!

Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • I went over everything again today to make sure I didn't miss anything, but it's still the same error about 'got' not being found. But the code you posted worked flawlessly the first time! Thank you for your help. – Scott W. Vincent Jun 07 '18 at 12:37
  • Using this approach, I am getting "There were no HTTP Requests logged for this event" and the SMS is not forwarded to email. I must be missing a simple setup step, but I don't know what I'm missing. I have my SendGrid API key (and it is configured as an environment variable and I access it using ${context.SENDGRID_API_KEY}. – BugBuddy Nov 10 '19 at 02:38
  • @BugBuddy please see the comment at https://stackoverflow.com/questions/42930601/there-were-no-http-requests-logged-for-this-event – Alex Baban Nov 10 '19 at 05:17
0

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

Timothy
  • 21
  • 2