/* eslint-disable func-names */
/* eslint-disable no-console */
const Alexa = require('ask-sdk');
//起動時
const GetNewFactHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'LaunchRequest'
|| (request.type === 'IntentRequest'
&& request.intent.name === 'GetNewFactIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('ようこそカラーデコードへ。ディスプレイ表示したいカラーコード6桁をおっしゃってください')
.reprompt('確認したいカラーコード6桁をおっしゃってください')
.withSimpleCard('カラー de コード', '発話例) \n FF00FF など')
.getResponse();
},
};
//ヘルプ
const HelpHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('画面付きデバイス用のスキルです。6桁のカラーコードを言うと、その色を画面表示します。確認したいカラーコード6桁をおっしゃってください')
.reprompt('確認したいカラーコード6桁をおっしゃってください')
.getResponse();
},
};
//カラーコードインテント
const ColorCodeHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& request.intent.name === 'ColorCode';
},
handle(handlerInput) {
let value1 = handlerInput.requestEnvelope.request.intent.slots.first.resolutions.resolutionsPerAuthority[0].values[0].value.id;
let value2 = handlerInput.requestEnvelope.request.intent.slots.second.resolutions.resolutionsPerAuthority[0].values[0].value.id;
let value3 = handlerInput.requestEnvelope.request.intent.slots.third.resolutions.resolutionsPerAuthority[0].values[0].value.id;
let value4 = handlerInput.requestEnvelope.request.intent.slots.fourth.resolutions.resolutionsPerAuthority[0].values[0].value.id;
let value5 = handlerInput.requestEnvelope.request.intent.slots.fifth.resolutions.resolutionsPerAuthority[0].values[0].value.id;
let value6 = handlerInput.requestEnvelope.request.intent.slots.sixth.resolutions.resolutionsPerAuthority[0].values[0].value.id;
const colorCode = value1 + value2 + value3 + value4 + value5 + value6;
const url = 'phpコードのあるサーバ/?code=' + colorCode;
//ディスプレイ表示
if (supportsDisplay(handlerInput) ) {
var speechText = 'カラーコード、' + value1 + '、' + value2 + '、' + value3 + '、' + value4 + '、' + value5 + '、' + value6 + 'の色を表示しています。'
+ '確認したいカラーコード6桁をおっしゃってください';
const myImage = new Alexa.ImageHelper()
.addImageInstance(url)
.getImage();
handlerInput.responseBuilder.addRenderTemplateDirective({
type: 'BodyTemplate1',
token: 'string',
backButton: 'HIDDEN',
backgroundImage: myImage,
title: '#' + colorCode,
});
return handlerInput.responseBuilder
.speak(speechText)
.reprompt('確認したいカラーコード6桁をおっしゃってください')
.getResponse();
}else{
speechText = 'カラーコードは' + value1 + '、' + value2 + '、' + value3 + '、' + value4 + '、' + value5 + '、' + value6 + 'ですが。画面の無いデバイスのため、色はお見せできません。'
+ '申し訳ございません。彩りあるいち日を';
return handlerInput.responseBuilder
.speak(speechText)
//.withSimpleCard('カラー de コード', 'カラーコードは')
.getResponse();
}
},
};
//キャンセル・停止時
const ExitHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'IntentRequest'
&& (request.intent.name === 'AMAZON.CancelIntent'
|| request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
return handlerInput.responseBuilder
.speak('ありがとうございました。彩りあるいち日を')
.withShouldEndSession(true)
.getResponse();
},
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
const request = handlerInput.requestEnvelope.request;
return request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
console.log(`Session ended with reason: ${handlerInput.requestEnvelope.request.reason}`);
return handlerInput.responseBuilder.getResponse();
},
};
//エラー時
const ErrorHandler = {
canHandle() {
return true;
},
handle(handlerInput, error) {
console.log(`Error handled: ${error.message}`);
return handlerInput.responseBuilder
.speak('申し訳ございません。正しいカラーコードが聞き取れませんでした。もう一度お願いします')
.reprompt('確認したいカラーコード6桁をおっしゃってください')
.getResponse();
},
};
// スキルが画面付きデバイスで動作している時は true を返す。
function supportsDisplay(handlerInput) {
var hasDisplay =
handlerInput.requestEnvelope.context &&
handlerInput.requestEnvelope.context.System &&
handlerInput.requestEnvelope.context.System.device &&
handlerInput.requestEnvelope.context.System.device.supportedInterfaces &&
handlerInput.requestEnvelope.context.System.device.supportedInterfaces.Display
return hasDisplay;
}
const skillBuilder = Alexa.SkillBuilders.standard();
exports.handler = skillBuilder
.addRequestHandlers(
GetNewFactHandler,
ColorCodeHandler,
HelpHandler,
ExitHandler,
SessionEndedRequestHandler
)
.addErrorHandlers(ErrorHandler)
.lambda();