Amazon Echo のスキルを作る方法を学ぼうとしています。文字通りただこんにちはと応答するだけの超シンプルなものを作ることに成功しました。
2 回目の試みとして、私が学んだことを突き止めるために、もう少し冒険的になり、Alexa に配列からランダムな GoT の引用を提供させたいと思いました。私は一般的にコーディングにかなり慣れていませんが、主にWeb関連の作業を行っています。さまざまな手段でかなり長い間検索を試みましたが、助けになったものは何も見つかりません。
Lambda でテストすると、ログ出力に「リクエストを完了する前にプロセスが終了しました」というエラーが表示されます。また、「Alexa が exports.handler で定義されていません」と表示されることもあります。ヘルプ。これの長い曲がりくねったことを申し訳ありません..
以下は私のコードです:
"use strict";
var alexa = require('alexa-sdk');
// QUOTES ARRAY
var quotes = [
'A mind needs books as a sword needs a whetstone, if it is to keep its edge',
'Never forget what you are, for surely the world will not',
'I wont be knitting by the fire while I have men fight for me'
];
// HANDLERS
var handlers = {
getThatQuote: function() {
var quoteIndex = Math.floor(Math.random() * quotes.length);
var randomQuote = quotes[quoteIndex];
return randomQuote;
},
LaunchRequest: function() {
this.emit(":tell", "Welcome to Game of Quotes");
},
QuoteGet: function() {
this.emit(":tell", "Here is your quote" + this.getThatQuote());
},
};
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};