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

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();
主站蜘蛛池模板: 邳州市| 哈密市| 鹿泉市| 青田县| 库尔勒市| 芦山县| 双鸭山市| 比如县| 三江| 芜湖县| 永川市| 南涧| 彭泽县| 堆龙德庆县| 开江县| 嘉鱼县| 怀来县| 武宣县| 景谷| 楚雄市| 石嘴山市| 商洛市| 凤翔县| 康定县| 枣庄市| 全南县| 饶阳县| 个旧市| 北流市| 马尔康县| 油尖旺区| 盐津县| 喜德县| 溧水县| 皋兰县| 峡江县| 台南县| 新邵县| 垣曲县| 陆丰市| 镇江市|