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

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();
主站蜘蛛池模板: 新巴尔虎左旗| 通山县| 林州市| 桑植县| 河津市| 棋牌| 宁都县| 镇江市| 措美县| 桦川县| 菏泽市| 山阳县| 当雄县| 林甸县| 如皋市| 登封市| 南阳市| 曲靖市| 澄城县| 定西市| 敖汉旗| 墨竹工卡县| 宝坻区| 慈利县| 新安县| 东海县| 静安区| 大厂| 大厂| 保靖县| 开鲁县| 民勤县| 博兴县| 普兰店市| 西乌| 台山市| 章丘市| 额济纳旗| 石林| 平远县| 房产|