Twilio 将短信转发到电子邮件 - 找不到模块 'got'

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

我是 Twilio 的新手。我正在尝试使用本教程将 SMS 转发到电子邮件地址:

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

我确信我已经完成了它所说的一切,但我每次都收到错误 11200 HTTP 检索失败,详细信息如下:

{ "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)" }

我已尝试绝对确保我编写的函数与教程中的相同。我是直接从the github page复制过来的,以防万一。我不确定如何继续对此进行故障排除,它似乎告诉我 'got' 未找到,但它应该在 Twilio 函数中可用。有任何想法吗?谢谢。

编辑:这是代码:

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

首先,上面的 got 代码适用于我的 Twilio 和 SendGrid 帐户,我刚刚测试过,我不知道你为什么遇到问题......,也许尝试创建一个 Twilio 子帐户运行 从那里开始。


其次,如果你仍然无法让 got 工作,这里有一些代码,你可以试试, 我也测试过并且有效。它使用 https 代替:


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

};

祝你好运!

经过更多研究,我 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 并添加了 6.7.1 版本,现在原始源代码可以使用了!

不过我更喜欢 Alex 的解决方案,因为它有效 "out of the box",我将其保留为公认的答案。

我能够通过确保 "got" 作为依赖项安装在此处的这些设置下来使其工作: https://www.twilio.com/console/runtime/functions/configure

Functions Screenshot