第一个技能,只是 errorHandler

First Skill, just the errorHandler

我想编写我的第一个 Alexa Skill,但我的第一个意图并没有正常工作。错误处理程序进来并响应:"Sorry, ..."

我想做一个飞镖计算器,第一个意图应该return:

"Ok ${spieleranzahl} Spieler, wie viele Punkte sollen gespielt werden?"

英语:

"Ok ${numerOfPlayers} players, how many points are we going to play?"

这是我的 js 代码,有人可以告诉我我做错了什么吗? (请不要介意 JSON 处的间距问题)

// This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
// Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
// session persistence, api calls, and more.
const Alexa = require('ask-sdk-core');

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Ok, mit wie vielen Spielern möchtest du spielen?';
        const repromtText = 'Nenne eine Spieleranzahl zwischen eins und vier.';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(repromtText)
            .getResponse();
    }
};
//ErfasseSpieleranzahlHandler
const ErfasseSpieleranzahlHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ErfasseSpieleranzahl';
    },
    handle(handlerInput) {
        const spieleranzahl = handlerInput.requestEnvelope.request.intent.slots.spieleranzahl.value;
        
        const speakOutput = `Ok ${spieleranzahl} Spieler, wie viele Punkte sollen gespielt werden?`;
        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt(repromtText)
            .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();
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
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();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
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();
    }
};

// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        ErfasseSpieleranzahlHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .lambda();
JSON-输入

{
"version": "1.0",
"session": {
    "new": false,
    "sessionId": "amzn1.echo-api.session.3a85cdaf-f0dc-457a-ad39-16b0517b0889",
    "application": {
        "applicationId": "amzn1.ask.skill.85a2b11a-5d56-4c52-9613-44999914ce0f"
    },
    "user": {
        "userId": "amzn1.ask.account.AFNNC73ZRXQAKJKCRCEDOCVGUG6PH5MQ3JOACBXC4ATPMXG4WPB2LQEFAYHNGI2Q2LULPXXIUN6KGXBHDOMJTC75XCU5Z6OQCNZ5BNSFNUQHGD3M7B5Q52RG4JNPDMCO4UKNQJIUNZ3DLLWIED7RP7LIJ7QJA3ROR4Q7KWYP5UYOFF5OHNSWYKXOZH6MVSWSZYHGW2XPEQEVPLY"
    }
},
"context": {
    "System": {
        "application": {
            "applicationId": "amzn1.ask.skill.85a2b11a-5d56-4c52-9613-44999914ce0f"
        },
        "user": {
            "userId": "amzn1.ask.account.AFNNC73ZRXQAKJKCRCEDOCVGUG6PH5MQ3JOACBXC4ATPMXG4WPB2LQEFAYHNGI2Q2LULPXXIUN6KGXBHDOMJTC75XCU5Z6OQCNZ5BNSFNUQHGD3M7B5Q52RG4JNPDMCO4UKNQJIUNZ3DLLWIED7RP7LIJ7QJA3ROR4Q7KWYP5UYOFF5OHNSWYKXOZH6MVSWSZYHGW2XPEQEVPLY"
        },
        "device": {
            "deviceId": "amzn1.ask.device.AHH2M6X2MORRNRLKQPH43LOAWIUADHUFRQGVEPUBHEYPSFXWJNIGJRUHRMUDT2P5UORD4IRELIIRK4A6YHC6QCMFEZ47FVYCR5Z5QLBNC5XA4Y5YU7G6OL4EQPDA43DAZ476EU47JKMBVWEW4YH5D6BAZHJCTYKURK5M75YDLUVWVEFSNEA5W",
            "supportedInterfaces": {}
        },
        "apiEndpoint":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HVkVQVUJIRVlQU0ZYV0pOSUdKUlVIUk1VRFQyUDVVT1JENElSRUxJSVJLNEE2WUhDNlFDTUZFWjQ3RlZZQ1I1WjVRTEJOQzVYQTRZNVlVN0c2T0w0RVFQREE0M0RBWjQ3NkVVNDdKS01CVldFVzRZSDVENkJBWkhKQ1RZS1VSSzVNNzVZRExVVldWRUZTTkVBNVciLCJ1c2VySWQiOiJhbXpuMS5hc2suYWNjb3VudC5BRk5OQzczWlJYUUFLSktDUkNFRE9DVkdVRzZQSDVNUTNKT0FDQlhDNEFUUE1YRzRXUEIyTFFFRkFZSE5HSTJRMkxVTFBYWElVTjZLR1hCSERPTUpUQzc1WENVNVo2T1FDTlo1Qk5TRk5VUUhHRDNNN0I1UTUyUkc0Sk5QRE1DTzRVS05RSklVTlozRExMV0lFRDdSUDdMSUo3UUpBM1JPUjRRN0tXWVA1VVlPRkY1T0hOU1dZS1hPWkg2TVZTV1NaWUhHVzJYUEVRRVZQTFkifX0.d3uA1qasqTQdtqid78zvnRuE_dVz3z-czcr8ajjZ4-NtSNIa67Em3YMyLQ7nhld5e0dM8IvqiwwvViiS6e6kXMa9QZQYMeanAR994s4M7SwgQeA-9n8UebUsjMhNLORqx-SCJlcZL4sN2LqJsUuLQLA8m2VucVo9EveNGrqYQFuLHtYiadsE4xKQ-9x3GBy9th3rf1sEesrEDktRljZw44yafClegR8kz-Xv7Uso6x6SC0sO9I3iM1a5CJKFV8-3EJBQZ3cDVktPZQ2Erp-IhsyXA3LRJBWMWKx8FFSFM_p3ZeYdtcIq28KPYDSFThIWvTRh7KSXzWltm8RmFoR3JQ"
    },
    "Viewport": {
        "experiences": [
            {
                "arcMinuteWidth": 246,
                "arcMinuteHeight": 144,
                "canRotate": false,
                "canResize": false
            }
        ],
        "shape": "RECTANGLE",
        "pixelWidth": 1024,
        "pixelHeight": 600,
        "dpi": 160,
        "currentPixelWidth": 1024,
        "currentPixelHeight": 600,
        "touch": [
            "SINGLE"
        ],
        "video": {
            "codecs": [
                "H_264_42",
                "H_264_41"
            ]
        }
    },
    "Viewports": [
        {
            "type": "APL",
            "id": "main",
            "shape": "RECTANGLE",
            "dpi": 160,
            "presentationType": "STANDARD",
            "canRotate": false,
            "configuration": {
                "current": {
                    "video": {
                        "codecs": [
                            "H_264_42",
                            "H_264_41"
                        ]
                    },
                    "size": {
                        "type": "DISCRETE",
                        "pixelWidth": 1024,
                        "pixelHeight": 600
                    }
                }
            }
        }
    ]
},
"request": {
    "type": "IntentRequest",
    "requestId": "amzn1.echo-api.request.945d9c9a-49ff-423c-8d84-50de36f54c01",
    "timestamp": "2019-11-24T22:34:59Z",
    "locale": "de-DE",
    "intent": {
        "name": "ErfasseSpieleranzahl",
        "confirmationStatus": "NONE",
        "slots": {
            "Spieleranzahl": {
                "name": "Spieleranzahl",
                "value": "1",
                "confirmationStatus": "NONE",
                "source": "USER"
            }
        }
    },
    "dialogState": "COMPLETED"
}
}

JSON-输出

{
"body": {
    "version": "1.0",
    "response": {
        "outputSpeech": {
            "type": "SSML",
            "ssml": "<speak>Sorry, I had trouble doing what you asked. Please try again.</speak>"
        },
        "reprompt": {
            "outputSpeech": {
                "type": "SSML",
                "ssml": "<speak>Sorry, I had trouble doing what you asked. Please try again.</speak>"
            }
        },
        "shouldEndSession": false,
        "type": "_DEFAULT_RESPONSE"
    },
    "sessionAttributes": {},
    "userAgent": "ask-node/2.7.0 Node/v8.10.0"
}
}

您正在注销失败位置的堆栈跟踪,您是否尝试在您的 cloudwatch 日志中查找错误,因为它会缩小范围?

虽然我可以立即看到您的插槽名为 Spieleranzahl,但是在您尝试访问它时在您的代码中使用 spieleranzahl <- 注意小写字母 's'。 属性 名称将区分大小写,因此这可能会导致错误。

这是更正后的处理程序:

//ErfasseSpieleranzahlHandler
const ErfasseSpieleranzahlHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'ErfasseSpieleranzahl';
    },
    handle(handlerInput) {
        const spieleranzahl = handlerInput.requestEnvelope.request.intent.slots.Spieleranzahl.value;

        const speakOutput = `Ok ${spieleranzahl} Spieler, wie viele Punkte sollen gespielt werden?`;
        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt(repromtText)
            .getResponse();
    }
};