官术网_书友最值得收藏!

Creating handlers

When our intents are triggered by a user saying one of our utterances, we need to handle that inside our code. To do this, we create an object containing a method for each of our intents. Currently, we only have one hello intent, so we only need to create one handler:

const helloHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'hello';
},
handle(handlerInput) {
const speechText = `Hello from Sam's new intent!`;

return handlerInput.responseBuilder
.speak(speechText)
.getResponse();
}
};

This hello handler has two parts: canHandle and handle. The canHandle function decides whether this handler can deal with this request, returning true if it can and false if it can't. This is calculated using the request type and intent name. If both match, then this is the correct handler. handle is telling Alexa how to respond. For this intent, all we want Alexa to do is to say Hello from Sam's new intent! and then get the user's next message.

Now we need to add our helloHandler to our skill.

We can add multiple handlers by passing them as multiple parameters to the .addRequestHandlers method:

exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(
helloHandler)
.lambda();
主站蜘蛛池模板: 行唐县| 泸西县| 崇信县| 三台县| 江华| 陈巴尔虎旗| 克山县| 冷水江市| 凯里市| 自治县| 拜城县| 庆城县| 宝丰县| 江都市| 遂宁市| 浙江省| 临城县| 册亨县| 山东省| 亚东县| 文水县| 建平县| 临沭县| 囊谦县| 通化县| 株洲县| 图木舒克市| 炎陵县| 扶沟县| 周至县| 南宫市| 灵璧县| 南溪县| 龙陵县| 永德县| 东兴市| 肥西县| 平度市| 绍兴县| 吉林省| 旬阳县|