簡體   English   中英

Amazon Alexa技能的NodeJS問題

[英]NodeJS Issue for Amazon Alexa Skill

我正在做一個學校項目,正在嘗試從URL中獲取數據以與我的Alexa Skill集成。 我絕對是NodeJS的初學者,並且對HTML或JSON的了解也不多。

我的學校在這里為我們的交通系統提供了一個“ API”: https : //prtstatus.wvu.edu/api/一個帶有時間戳的示例案例在這里: https : //prtstatus.wvu.edu/api/1501906657/?格式= JSON

在我的代碼中,我試圖從URL的get中獲取字符串作為JSON解析,但是在格式化它時遇到了問題,以便字符串的“ message:”部分將繼續傳遞。 這是我在AWS Lambda中意圖的代碼:

'getPRTStatus': function() {
    var date = Math.round(new Date().getTime()/1000);
    var http = require('http');
    var https = require('https');

    var options = {
        host: 'https://prtstatus.wvu.edu',
        path: '/api/'+date+'/?format=json'
    };
    var object;
    var callback = function(response) {
        var str = '';

        //another chunk of data has been recieved, so append it to `str`
        response.on('data', function (chunk) {
            str += chunk;
        });

        //the whole response has been recieved, so we just print it out here
        response.on('end', function () {
            console.log(str);
            object = JSON.parse(str);
        });
    }

    https.request(options, callback).end();
    this.attributes.speechOutput = this.t(object.message);
    this.attributes.repromptSpeech = this.t(object.message);
    this.emit(':ask', this.attributes.speechOutput, this.attributes.repromptSpeech);
},

代碼中需要解決幾件事:

  1. 在選項中,您無需指定協議。 根據http文檔,選項主機應該只是域名。 因此,在這種情況下,它將是“ prtstatus.wvu.edu”

  2. 在回調完成之前正在使用object.message。 因此,您可能會看到類似“無法讀取未定義的屬性'消息'”的錯誤。 需要發生的是3行(this.attributes ...)是回調本身的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM