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

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();
主站蜘蛛池模板: 阳新县| 镇江市| 张家界市| 泸州市| 韶山市| 无为县| 安新县| 玉屏| 陇西县| 白玉县| 长沙县| 云和县| 马关县| 克拉玛依市| 青冈县| 双辽市| 台南县| 滦平县| 平潭县| 杂多县| 鸡东县| 正阳县| 岗巴县| 阳原县| 阜平县| 新宁县| 林周县| 克什克腾旗| 澄江县| 旅游| 巢湖市| 兴安盟| 黄骅市| 周至县| 金山区| 海原县| 湟源县| 雅江县| 沾化县| 新乡县| 长治县|