From 6446712f5097a4174cbeee86264f3c5fb4ce84df Mon Sep 17 00:00:00 2001 From: Oliver Vogt Date: Mon, 24 Aug 2026 19:53:01 +0200 Subject: [PATCH] static-handlers.js: Pflicht-Handler (Launch/Help/Cancel/Fallback/Error) ausgelagert --- home-assistant-skill/static-handlers.js | 84 +++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 home-assistant-skill/static-handlers.js diff --git a/home-assistant-skill/static-handlers.js b/home-assistant-skill/static-handlers.js new file mode 100644 index 0000000..d7ec131 --- /dev/null +++ b/home-assistant-skill/static-handlers.js @@ -0,0 +1,84 @@ +// ---- Standard-Handler (Pflicht für jeden Alexa Skill) ---- +const Alexa = require('ask-sdk-core'); + +const LaunchRequestHandler = { + canHandle(handlerInput) { + return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest'; + }, + handle(handlerInput) { + const speakOutput = 'Willkommen beim Hausassistent. Du kannst mich zum Beispiel fragen, wie warm es in einem Raum ist.'; + return handlerInput.responseBuilder + .speak(speakOutput) + .reprompt(speakOutput) + .getResponse(); + }, +}; + +const HelpIntentHandler = { + canHandle(handlerInput) { + return ( + Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && + Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent' + ); + }, + handle(handlerInput) { + const speakOutput = 'Du kannst mich zum Beispiel fragen: wie warm ist es im Wohnzimmer.'; + return handlerInput.responseBuilder.speak(speakOutput).reprompt(speakOutput).getResponse(); + }, +}; + +const CancelAndStopIntentHandler = { + canHandle(handlerInput) { + return ( + Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && + (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent' || + Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent') + ); + }, + handle(handlerInput) { + const speakOutput = 'Bis bald!'; + return handlerInput.responseBuilder.speak(speakOutput).getResponse(); + }, +}; + +const FallbackIntentHandler = { + canHandle(handlerInput) { + return ( + Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && + Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent' + ); + }, + handle(handlerInput) { + const speakOutput = 'Das habe ich nicht verstanden. Du kannst mich zum Beispiel fragen, wie warm es im Wohnzimmer ist.'; + return handlerInput.responseBuilder.speak(speakOutput).reprompt(speakOutput).getResponse(); + }, +}; + +const SessionEndedRequestHandler = { + canHandle(handlerInput) { + return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest'; + }, + handle(handlerInput) { + return handlerInput.responseBuilder.getResponse(); + }, +}; + +const ErrorHandler = { + canHandle() { + return true; + }, + handle(handlerInput, error) { + console.error(`~~~~ Error handled: ${error.stack}`); + const speakOutput = 'Entschuldigung, da ist etwas schiefgelaufen. Bitte versuch es noch einmal.'; + return handlerInput.responseBuilder.speak(speakOutput).reprompt(speakOutput).getResponse(); + }, +}; + +module.exports = { + LaunchRequestHandler, + HelpIntentHandler, + CancelAndStopIntentHandler, + FallbackIntentHandler, + SessionEndedRequestHandler, + ErrorHandler, +};