'use strict';/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills Kit.
* The Intent Schema, Custom Slots, and Sample Utterances for this skill, as well as
* testing instructions are located at http://amzn.to/1LzFrj6
*
* For additional samples, visit the Alexa Skills Kit Getting Started guide at
* http://amzn.to/1LGWsLG
*/var http =require('http');// --------------- Helpers that build all of the responses -----------------------functionbuildSpeechletResponse(title, output, repromptText, shouldEndSession){return{outputSpeech:{type:'PlainText',text: output,},card:{type:'Simple',title:"SessionSpeechlet - "+ title,content:"SessionSpeechlet - "+ output,},reprompt:{outputSpeech:{type:'PlainText',text: repromptText,},},shouldEndSession: shouldEndSession
};}functionbuildResponse(sessionAttributes, speechletResponse){return{version:'1.0',
sessionAttributes,response: speechletResponse,};}// --------------- Functions that control the skill's behavior -----------------------functiongetWelcomeResponse(callback){// If we wanted to initialize the session to have some attributes we could add those here.// セッションを初期化していくつかの属性を持たせたい場合は、それらをここに追加できます。const sessionAttributes ={};const cardTitle ='Welcome';const speechOutput ="2階の温度を教えてなどと話しかけると、それを答えます。";// If the user either does not reply to the welcome message or says something that is not// understood, they will be prompted again with this text.// ユーザーがウェルカムメッセージに返信しないか、理解できないことを言った場合、このテキストが表示されます。const repromptText ="私は温湿度計よっ!あなたの願い事をおっしゃりっ!";const shouldEndSession =false;callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));}functionhandleSessionEndRequest(callback){const cardTitle ='Session Ended';const speechOutput ='Thank you for trying the Alexa Skills Kit sample. Have a nice day!';// Setting this to true ends the session and exits the skill.const shouldEndSession =true;callback({},buildSpeechletResponse(cardTitle, speechOutput,null, shouldEndSession));}functioncreateLocationAttributes(location){return{location: location
};}/**
* Read temperature in the session and prepares the speech to reply to the user.
*/functionreadTemperatureInSession(intent, session, callback){const cardTitle = intent.name;const LocationSlot = intent.slots.Location;const MeasurementSlot = intent.slots.Measurement;let repromptText ='';let sessionAttributes ={};const shouldEndSession =false;let speechOutput ='';var body ='';var blynkAuthToken;var blynkPin;var location = LocationSlot.value;if(location ==''&& session.attributes){
location = session.attributes.location;}if(location ===''){
location ='二階';}switch(location){case'二階':
blynkAuthToken ='****_nikaino_BME280_notoken_****';// 二階のBME280のtokenbreak;case'仏間':
blynkAuthToken ='***_butsumano_BME280_notoken_***';// 仏間のBME280のtokenbreak;}var measurement = MeasurementSlot.value;if(measurement ===''){
measurement ='温度';}switch(measurement){case'何度':case'気温':case'室温':case'温度':
blynkPin ='V0';break;case'湿気':case'湿度':
blynkPin ='V1';break;case'大気圧':case'気圧':
blynkPin ='V2';break;}var httpPromise =newPromise(function(resolve,reject){
http.get({host:'blynk-cloud.com',path:'/'+ blynkAuthToken +'/get/'+ blynkPin,port:'80'},function(response){// Continuously update stream with data
response.on('data',function(d){
body += d;});
response.on('end',function(){// Data reception is done, do whatever with it!
console.log(body);resolve('Done Sending');});});});
httpPromise.then(function(data){
console.log('Function called succesfully:', data);var info =parseFloat(JSON.parse(body));if(measurement ==='何度'){
speechOutput = location +'は ';}else{
speechOutput = location +'の '+ measurement +'は ';}
repromptText = speechOutput;switch(measurement){default:case'何度':case'気温':case'室温':case'温度':
speechOutput = speechOutput + info.toFixed(1)+'度 です。';
repromptText = repromptText + info.toFixed(1)+'℃です。';break;case'湿気':case'湿度':
speechOutput = speechOutput + info.toFixed(1)+'パーセント です。';
repromptText = repromptText + info.toFixed(1)+'%です。';break;case'大気圧':case'気圧':
speechOutput = speechOutput + info.toFixed(0)+'ヘクトパスカル です。';
repromptText = repromptText + info.toFixed(0)+'hPaです。';break;}
console.log(speechOutput);
sessionAttributes =createLocationAttributes(location);callback(sessionAttributes,buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));},function(err){
console.log('An error occurred:', err);});}// --------------- Events -----------------------/**
* Called when the session starts.
*/functiononSessionStarted(sessionStartedRequest, session){
console.log("onSessionStarted requestId=${sessionStartedRequest.requestId}, sessionId=${session.sessionId}");}/**
* Called when the user launches the skill without specifying what they want.
*/functiononLaunch(launchRequest, session, callback){
console.log("onLaunch requestId=${launchRequest.requestId}, sessionId=${session.sessionId}");// Dispatch to your skill's launch.getWelcomeResponse(callback);}/**
* Called when the user specifies an intent for this skill.
*/functiononIntent(intentRequest, session, callback){
console.log("onIntent requestId=${intentRequest.requestId}, sessionId=${session.sessionId}");const intent = intentRequest.intent;const intentName = intentRequest.intent.name;// Dispatch to your skill's intent handlersif(intentName ==='GetMeasurement'){readTemperatureInSession(intent, session, callback);}elseif(intentName ==='AMAZON.HelpIntent'){getWelcomeResponse(callback);}elseif(intentName ==='AMAZON.StopIntent'|| intentName ==='AMAZON.CancelIntent'){handleSessionEndRequest(callback);}else{thrownewError('Invalid intent');}}/**
* Called when the user ends the session.
* Is not called when the skill returns shouldEndSession=true.
*/functiononSessionEnded(sessionEndedRequest, session){
console.log("onSessionEnded requestId=${sessionEndedRequest.requestId}, sessionId=${session.sessionId}");// Add cleanup logic here}// --------------- Main handler -----------------------// Route the incoming request based on type (LaunchRequest, IntentRequest,// etc.) The JSON body of the request is provided in the event parameter.
exports.handler=(event, context)=>{try{
console.log("event.session.application.applicationId=${event.session.application.applicationId}");/**
* Uncomment this if statement and populate with your skill's application ID to
* prevent someone else from configuring a skill that sends requests to this function.
*//*
if (event.session.application.applicationId !== 'amzn1.echo-sdk-ams.app.[unique-value-here]') {
context.fail("Invalid Application ID");
}
*/if(event.session.new){onSessionStarted({requestId: event.request.requestId }, event.session);}if(event.request.type ==='LaunchRequest'){onLaunch(event.request,
event.session,functioncallback(sessionAttributes, speechletResponse){
context.succeed(buildResponse(sessionAttributes, speechletResponse));});}elseif(event.request.type ==='IntentRequest'){onIntent(event.request,
event.session,functioncallback(sessionAttributes, speechletResponse){
context.succeed(buildResponse(sessionAttributes, speechletResponse));});}elseif(event.request.type ==='SessionEndedRequest'){onSessionEnded(event.request, event.session);
context.succeed();}}catch(e){
context.fail("Exception: "+ e);}};