- Hands-On Chatbot Development with Alexa Skills and Amazon Lex
- Sam Williams
- 203字
- 2021-07-16 17:45:15
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();
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- SOA用戶指南
- 5G承載網(wǎng)網(wǎng)絡(luò)規(guī)劃與組網(wǎng)設(shè)計(jì)
- 物聯(lián)網(wǎng)之魂:物聯(lián)網(wǎng)協(xié)議與物聯(lián)網(wǎng)操作系統(tǒng)
- 物聯(lián)網(wǎng)檢驗(yàn)檢測技術(shù)
- Microservice Patterns and Best Practices
- SAE原理與網(wǎng)絡(luò)規(guī)劃
- Getting Started with nopCommerce
- 人人都該都懂的互聯(lián)網(wǎng)思維
- 網(wǎng)絡(luò)綜合布線(第2版)
- 物聯(lián)網(wǎng)基礎(chǔ)及應(yīng)用
- 想象的互動:網(wǎng)絡(luò)人際傳播中的印象形成
- 巧學(xué)活用CISCO網(wǎng)絡(luò)典型配置
- 網(wǎng)絡(luò)分析技術(shù)揭秘:原理、實(shí)踐與WinPcap深入解析
- OpenShift Cookbook