【问题标题】:Twilio Forward SMS to email - Cannot find module 'got'Twilio 将短信转发到电子邮件 - 找不到模块“得到”
【发布时间】:2018-11-16 09:05:39
【问题描述】:

我是 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": "找不到模块 'got'", “名称”:“错误”, "stack": "错误:在 Function.Module._resolveFilename (module.js:547:15) 处找不到模块 'got'\n Function.Module._load (module.js:474:25)\n 在 Module.require (module.js:596:17)\n 在 Module.twilioRequire [按要求] (/var/task/node_modules/enigma-lambda/src/dependency.js:28:21)\n 在 需要 (internal/module.js:11:18)\n 在对象。 (/var/task/handlers/ZFa37cc3db9fd8db0501c2e5fc92137969.js:1:75)\n
在 Module._compile (module.js:652:30)\n 在 Object.Module._extensions..js (module.js:663:10)\n 在 Module.load (module.js:565:32)\n 在 tryModuleLoad (module.js:505:12)" }

我已经尝试过确保我编写的函数与教程中的相同。可以肯定的是,我直接从the github page 复制了它。我不确定如何继续进行故障排除,它似乎告诉我没有找到“得到”,但它应该在 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);
    });
};

【问题讨论】:

  • exports.handler = function(... 之前有const got = require('got'); 吗??
  • @AlexBaban 是的,谢谢。我已经编辑了我的问题以包含我正在使用的代码。

标签: twilio twilio-functions


【解决方案1】:

经过更多研究,我 found some comments on GitHub 表示“got”默认情况下不再包含在 Twilio 依赖项中。根据那里的说明,我去了Runtime Functions Config section of the Twilio console并添加了6.7.1版,现在原始源代码可以工作了!

不过,我更喜欢 Alex 的解决方案,因为它“开箱即用”,我将其作为公认的答案。

【讨论】:

  • 你好,我写了你在问题开始时链接的博客文章,我现在要去更新它。我已经知道这件事有一段时间了,所以我很抱歉没有得到它。
  • 更新了您找到 cmets 的博客文章和 GitHub 存储库(这也是我的,抱歉!)。
  • @philnash github.com/philnash/useful-twilio-functions/blob/master/… 的代码日期为 2017 年 5 月 7 日,但仍然无法使用。这是我第一次尝试使用 Twilio,我对文档不感兴趣。
  • 嗨@cja,该代码对您有什么不妥之处?您是否按照the README 中的说明在Functions configuration page 中添加了got 作为依赖项?
【解决方案2】:

首先,上面带有 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();

};

祝你好运!

【讨论】:

  • 我今天再次检查了所有内容以确保我没有遗漏任何内容,但仍然是关于“未找到”的相同错误。但是您发布的代码第一次完美运行!感谢您的帮助。
  • 使用这种方法,我得到“没有为此事件记录的 HTTP 请求”并且 SMS 没有转发到电子邮件。我一定错过了一个简单的设置步骤,但我不知道我错过了什么。我有我的 SendGrid API 密钥(它被配置为环境变量,我使用 ${context.SENDGRID_API_KEY} 访问它。
  • @BugBuddy 请查看stackoverflow.com/questions/42930601/…的评论
【解决方案3】:

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

Functions Screenshot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多