AWS Lambda

时间:2018-05-15 04:11:47

标签: node.js lambda alexa-skills-kit

我正在尝试通过AWS Lambda中的Alexa技能将项目添加到我的Todoist项目中。我对所有这些技术都很陌生,所以如果修复非常明显,请原谅我。当我要求Alexa调用我的addZipcode技能时,它失败了。这就是我所拥有的(不包括所有Alexa Lambda函数中的一些东西):

Alexa stuff
...
const handlers = {
'LaunchRequest': function() {
    this.emit('AMAZON.HelpIntent');
},
'addZipcode': function() {

    const newZip = this.event.request.intent.slots.zipcode.value;
    const speechOutput = newZip;

    var http = require("https");
    function postZip(newZip) {
        var options = {
            "method": "POST",
            "hostname": [
                "beta",
                "todoist",
                "com"
            ],
            "path": [
                "API",
                "v8",
                "tasks"
            ],
            "headers": {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + token
            }
        };

        var req = http.request(options, function(res) {
            var chunks = [];
            res.on("data", function(chunk) {
                chunks.push(chunk);
            });

            res.on("end", function() {
                var body = Buffer.concat(chunks);
                console.log(body.toString());
            });
        });
        req.write(JSON.stringify({ content: newZip, project_id: XXXXXXXXXX }));
        req.end();
    }

    postZip(newZip);

    this.response.cardRenderer(SKILL_NAME, newZip);
    this.response.speak(speechOutput);
    this.emit(':responseReady');

},
.... cont

当我尝试使用Alexa运行技能时,我得到了结果错误:

Response:
{
  "errorMessage": "hostHeader.replace is not a function",
  "errorType": "TypeError",
  "stackTrace": [
    "Agent.addRequest (_http_agent.js:130:39)",
    "new ClientRequest (_http_client.js:159:16)",
    "Object.exports.request (http.js:31:10)",
    "Object.exports.request (https.js:199:15)",
    "postZip (/var/task/index.js:72:28)",
    "Object.addZipcode (/var/task/index.js:88:9)",
    "emitNone (events.js:86:13)",
    "AlexaRequestEmitter.emit (events.js:185:7)",
    "AlexaRequestEmitter.EmitEvent (/var/task/node_modules/alexa-sdk/lib/alexa.js:216:10)",
    "AlexaRequestEmitter.ValidateRequest (/var/task/node_modules/alexa-sdk/lib/alexa.js:181:23)"
  ]
}

我尝试搜索有关hostHeader.replace甚至是hostHeader的更多信息,但无济于事。当我用

包围我的postZip函数时
exports.handler = function(event, context, callback) {} 

技能实际上有效,但是Post请求没有通过(因为新的邮政编码不会作为我的Todoist上的新任务添加)。我很确定Post请求代码本身是正确的,因为我通过Postman运行它并且添加了zipcode。

请帮助我理解为什么它不起作用。

1 个答案:

答案 0 :(得分:0)

很难说出导致错误的原因。但是node docs说,hostname以及path应该只是字符串而不是数组,因为它是您代码中的情况。

所以我首先要做的就是将代码更改为:

var options = {
        "method": "POST",
        "hostname": "beta.todoist.com",
        "path": "/API/v8/tasks",
        "headers": {
            "Content-Type": "application/json",
            "Authorization": "Bearer " + token
        }