Amazon Echo 用のオーディオ プレーヤーの開発に取り組んでいます。Amazon は ( https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs ) に基づいて作業するための良い例を提供しました。私は Python 出身で、node.js はまだ初めてなので、これは楽しい演習でした。
ユーザーのタイムゾーンに基づいて再生したい複数のストリームがあります。Amazon Echo にはこれを行う簡単な方法がないため、ユーザーに郵便番号を入力させ、それを使用してタイムゾーンを決定することにしました。
最初の試行では、関連するすべての郵便番号とタイムゾーンのペアの JSON ファイルを使用しましたが、読み込みに時間がかかりすぎました。
複数の DB を使用するように Alexa スキルを構成するのに問題があったため、それらをデータベースにロードしようとすると、さまざまな結果が得られました。(ユーザーセッションを追跡するために使用しています。)
最終的に、サードパーティの API を使用して場所を特定することにしました ( https://www.zipcodeapi.com/API#zipToLoc )。ノード シェルでのテストは順調に進んだので、ラムダにアップロードしてテストしました。
残念ながら、テストで「未定義のプロパティ 'body'を読み取れません」というメッセージが表示されました。
問題のあるコード
var constants = require(constants);
var request = require(request);
var zipcode = 85223; // Normally, this is taken from the intent.
var request_string = 'https://www.zipcodeapi.com/rest/' + constants.zipAPIKey + '/info.json/' + zipcode + '/degrees';
var bd = request(request_string, function(error, response, body){
return body;
});
var proc_body = JSON.parse(bd.response.body); // THE PROBLEM LINE
if (proc_body.hasOwnProperty('error_code')){
var message = 'An error occurred. Try again later.';
console.log("ZipCodeAPI Error : %j", proc_body.error_msg);
this.attributes['utc'] = -5; // Default to EST
} else {
this.attributes['utc'] = proc_body.timezone.utc_offset_sec / 3600; // utc_offset_sec is in seconds. Bump it back to hours.
var message = 'Your system has been configured.';
}
シェルの礼儀: JSON.parse(bd.response.body)
{
zip_code: '85223',
lat: 32.740013,
lng: -111.679788,
city: 'Arizona City',
state: 'AZ',
timezone:
{ timezone_identifier: 'America/Phoenix',
timezone_abbr: 'MST',
utc_offset_sec: -25200,
is_dst: 'F' },
acceptable_city_names: []
}
前に言ったように、シェル内では問題なく動作します。ラムダで問題が発生する理由がわかりません。リクエストは非同期で、Echo は API からの応答を待っていませんか? 何かを正しく構成していませんか?
あなたが提供できる助けに感謝します。お時間をいただきありがとうございます。
編集:以下のコメントに従って、ドキュメントを読み直しました。範囲を超えていたからだと思いますか?
'GetZip' : function () {
this.attributes['zipcode'] = this.event.request.intent.slots.zipcode.value;
var request_string = 'https://www.zipcodeapi.com/rest/' + constants.zipKey + '/info.json/' + this.attributes['zipcode'] + '/degrees';
request(request_string, function(error, response, body){
var proc_body = JSON.parse(body);
if (proc_body.hasOwnProperty('error_code')){
var message = 'An error occurred. Try again later.';
console.log("ZipCodeAPI Error : %j", proc_body.error_msg);
this.attributes['utc'] = -5;
} else {
this.attributes['utc'] = proc_body.timezone.utc_offset_sec / 3600;
var message = 'Your system has been configured.';
}
this.handler.state = constants.states.START_MODE;
this.response.speak(message);
controller.play.call(this);
});
this
アクセスできないため、スコープの問題がまだあると思います。