Twilio 将短信转发到电子邮件 - 找不到模块“得到" [英] Twilio Forward SMS to email - Cannot find module 'got'

查看:25
本文介绍了Twilio 将短信转发到电子邮件 - 找不到模块“得到"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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": "找不到模块 'got'","name": "错误","stack": "错误:在 Function.Module._resolveFilename (module.js:547:15)\n 处找不到模块 'got'\nFunction.Module._load (module.js:474:25)\n 在 Module.require(module.js:596:17)\n 在 Module.twilioRequire [as require](/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)" }

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

我已尝试确保我编写的函数与教程中的相同.我直接从github页面 可以肯定.我不确定如何继续解决这个问题,它似乎告诉我没有找到得到",但它应该在 Twilio 函数中可用.有任何想法吗?谢谢.

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.

这是代码:

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 子帐户并从那里运行.

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.

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

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!

这篇关于Twilio 将短信转发到电子邮件 - 找不到模块“得到"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆