- 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();
- Application Development with Qt Creator(Second Edition)
- 物聯(lián)網(wǎng)(IoT)基礎(chǔ):網(wǎng)絡(luò)技術(shù)+協(xié)議+用例
- 網(wǎng)管員典藏書架:網(wǎng)絡(luò)管理與運(yùn)維實(shí)戰(zhàn)寶典
- 萬物互聯(lián):蜂窩物聯(lián)網(wǎng)組網(wǎng)技術(shù)詳解
- Proxmox High Availability
- 數(shù)字調(diào)制解調(diào)技術(shù)的MATLAB與FPGA實(shí)現(xiàn):Altera/Verilog版(第2版)
- IPv6網(wǎng)絡(luò)切片:使能千行百業(yè)新體驗(yàn)
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- Echo Quick Start Guide
- 物聯(lián)網(wǎng)工程概論
- 局域網(wǎng)組成實(shí)踐
- 計(jì)算機(jī)網(wǎng)絡(luò)技術(shù)
- 信息技術(shù)安全評估準(zhǔn)則:源流、方法與實(shí)踐
- Microservices Development Cookbook
- 智慧的物聯(lián)網(wǎng):感知中國和世界的技術(shù)