ошибка "Вы только что вызвали IntentName" в навыке Alexa - PullRequest
0 голосов
/ 22 апреля 2020

PlaceOrderIntent работает нормально, пока состояние диалога не завершено, но когда оно переходит в состояние диалога завершено, оно выдает мне сообщение «Вы только что вызвали PlaceOrderIntent»

Я делаю любую ошибку в CompletedPlaceOrderIntentHandler

это мой код

const Alexa = require ('ask-sdk-core');

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Welcome, you can say Hello or Help. Which would you like to try?';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const HelloWorldIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
    },
    handle(handlerInput) {
        const HelloWorldIntent = handlerInput.requestEnvelope.request.intent.name;
        const speakOutput = 'Hello! Do you think today is a good day?';
        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        sessionAttributes.HelloWorldIntent = HelloWorldIntent;
        handlerInput.attributesManager.setSessionAttributes(sessionAttributes);

        const prompt = 'Do you think today is a good day?';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(prompt)
            .getResponse();
    }
};

const StartedInProgressPlaceOrderIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "IntentRequest"
      && handlerInput.requestEnvelope.request.intent.name === "PlaceOrderIntent"
      && handlerInput.requestEnvelope.request.dialogState !== 'COMPLETED'
      && !handlerInput.requestEnvelope.request.intent.slots.menu.value;
  },
  handle(handlerInput) {
  const speakOutput = `You can order Platters, Soups and, Shakes. What you want to order?`
  const prompt = `Please select any one from platter, soup or, drink.`
    return handlerInput.responseBuilder
      .speak(speakOutput)
      .reprompt(prompt)
      .addElicitSlotDirective('menu')
      .getResponse();
  }
};

const PlatterGivenPlaceOrderIntentHandler = {
canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "IntentRequest"
      && handlerInput.requestEnvelope.request.intent.name === "PlaceOrderIntent"
      && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name 
      && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name === 'Platter'
      && !handlerInput.requestEnvelope.request.intent.slots.platType.value;
  },
  handle(handlerInput) {
      return handlerInput.responseBuilder
      .speak("Which platter would you like Regular, Special, Rajasthani, Gujarati, or Punjabi?")
      .reprompt("Which platter would you like Regular, Special, Rajasthani, Gujarati, or Punjabi?")
      .addElicitSlotDirective('platType')
      .getResponse();
  }
};

const SoupGivenPlaceOrderIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "IntentRequest"
      && handlerInput.requestEnvelope.request.intent.name === "PlaceOrderIntent"
      && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name
      && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name === 'Soup'
      && !handlerInput.requestEnvelope.request.intent.slots.soupType.value;
  },
  handle(handlerInput) {
      return handlerInput.responseBuilder
      .speak("Which soup would you like tomato, manchow, onion, or corn soup?")
      .reprompt("Would you like a tomato, manchow, onion, or corn soup?")
      .addElicitSlotDirective('soupType')
      .getResponse();
  }
};

const ShakeGivenPlaceOrderIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "IntentRequest"
      && handlerInput.requestEnvelope.request.intent.name === "PlaceOrderIntent"
      && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name
      && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name === 'Shake'
      && !handlerInput.requestEnvelope.request.intent.slots.shakeType.value;
      },
  handle(handlerInput) {
      return handlerInput.responseBuilder
      .speak("Which shake would you like chocolate, vanilla, milk, strawberry, or mango shake?")
      .reprompt("Would you like a chocolate, vanilla, milk, strawberry, or mango shake?")
      .addElicitSlotDirective('shakeType')
      .getResponse();
  }
};

const CompletedPlaceOrderIntentHandler = {
  canHandle(handlerInput) {
    return handlerInput.requestEnvelope.request.type === "IntentRequest"
        && handlerInput.requestEnvelope.request.intent.name === "PlaceOrderIntent"
        && handlerInput.requestEnvelope.request.dialogState === "COMPLETED"
        && handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name
        && (handlerInput.requestEnvelope.request.intent.slots.platType.resolutions
      .resolutionsPerAuthority[0].values[0].value.name
        || handlerInput.requestEnvelope.request.intent.slots.soupType.resolutions
      .resolutionsPerAuthority[0].values[0].value.name
        || handlerInput.requestEnvelope.request.intent.slots.shakeType.resolutions
      .resolutionsPerAuthority[0].values[0].value.name);
  },
  handle(handlerInput){

    const menuitems = handlerInput.requestEnvelope.request.intent.slots.menu.resolutions
      .resolutionsPerAuthority[0].values[0].value.name;
    let platType = handlerInput.requestEnvelope.request.intent.slots.platType.resolutions
      .resolutionsPerAuthority[0].values[0].value.name;
    let soupType = handlerInput.requestEnvelope.request.intent.slots.soupType.resolutions
      .resolutionsPerAuthority[0].values[0].value.name;
    let shakeType = handlerInput.requestEnvelope.request.intent.slots.shakeType.resolutions
      .resolutionsPerAuthority[0].values[0].value.name;
    let speakOutput = ''; 

    if(platType){
        speakOutput = `Your order for ${platType} ${menuitems} has been placed. Anything else you want?`;
    } else if(soupType){
        speakOutput = `Your order for ${soupType} ${menuitems} has been placed. Anything else you want?`;
    } else if(shakeType){
        speakOutput = `Your order for ${shakeType} ${menuitems} has been placed. Anything else you want?`;
    }
    const prompt = `Anything else you want?`;
        return handlerInput.responseBuilder
        .speak(speakOutput)
        .reprompt(prompt)
        .getResponse();
  }
};

const YesIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.YesIntent';
    },
    handle(handlerInput) {
        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        let speakOutput = '';

        if(sessionAttributes.HelloWorldIntent){
        speakOutput = 'Glad! to hear that. Do you think tommorow will also be a good day?';
        }
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

const NoIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.NoIntent';
    },
    handle(handlerInput) {
        const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
        let speakOutput = '';

        /*if(sessionAttributes.PlaceOrderIntent && sessionAttributes.DialogState === 'COMPLETED'){
        speakOutput = 'Okay, If you ever want to place order just say "Alexa, place order".';
        } else*/ if(sessionAttributes.HelloWorldIntent){
        speakOutput = 'Sorry! to hear that. Do you think tommorow will be a good day?';
        }
        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 = 'You can say hello to me! How can I help?';

        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 = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .getResponse();
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder.getResponse();
    }
};

const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};

const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        HelloWorldIntentHandler,
        StartedInProgressPlaceOrderIntentHandler,
        PlatterGivenPlaceOrderIntentHandler,
        SoupGivenPlaceOrderIntentHandler,
        ShakeGivenPlaceOrderIntentHandler,
        CompletedPlaceOrderIntentHandler,
        YesIntentHandler,
        NoIntentHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();

ребята, пожалуйста, помогите мне в решении этой проблемы.

...