-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·152 lines (131 loc) · 5.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
//import ask-sdk-core
const Alexa = require('ask-sdk-core');
//skill name
const appName = 'My Calculator';
//code for the handlers
const LaunchRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
},
handle(handlerInput) {
//welcome message
let speechText = 'Welcome to My Calculator by GB, you can try by saying add two number or add 5 and 10';
//welcome screen message
let displayText = "Welcome to My Calculator"
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard(appName, displayText)
.getResponse();
}
};
//implement custom handlers
const AddIntentHandler = {
canHandle(handlerInput){
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'AddIntent'
},
handle(handlerInput){
let speechText = '';
let displayText = '';
let intent = handlerInput.requestEnvelope.request.intent;
let firstNumber = intent.slots.firstNumber.value;
let secondNumber = intent.slots.secondNumber.value;
if( firstNumber && secondNumber) {
// perform operation
console.log('inputed data',firstNumber,secondNumber);
let result = parseInt(firstNumber) + parseInt(secondNumber);
speechText = `The result of ${firstNumber} plus ${secondNumber} is ${result}`;
displayText = `${result}`;
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard(appName,displayText)
.withShouldEndSession(false)
.getResponse();
} else {
// call fall back
return handlerInput.responseBuilder
.addDeligateDirective(intent)
.getResponse();
}
}
};
const SubstractionIntentHandler = {
canHandle(handlerInput){
return handlerInput.requestEnvelope.request.type === 'IntentRequest' &&
handlerInput.requestEnvelope.request.intent.name === 'SubstractionIntent'
},
handle(handlerInput){
let speechText = '';
let displayText = '';
let intent = handlerInput.requestEnvelope.request.intent;
let firstNumber = intent.slots.firstNumber.value;
let secondNumber = intent.slots.secondNumber.value;
if( firstNumber && secondNumber) {
// perform operation
console.log('inputed data',firstNumber,secondNumber);
let result = parseInt(secondNumber) - parseInt(firstNumber);
speechText = `The result of ${firstNumber} minus ${secondNumber} is ${result}`;
displayText = `${result}`;
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard(appName,displayText)
.withShouldEndSession(false)
.getResponse();
} else {
// call fall back
return handlerInput.responseBuilder
.addDeligateDirective(intent)
.getResponse();
}
}
};
//end Custom handlers
const HelpIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.HelpIntent';
},
handle(handlerInput) {
//help text for your skill
let speechText = 'Try Saying add 10 and 20';
return handlerInput.responseBuilder
.speak(speechText)
.reprompt(speechText)
.withSimpleCard(appName, speechText)
.getResponse();
}
};
const CancelAndStopIntentHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& (handlerInput.requestEnvelope.request.intent.name === 'AMAZON.CancelIntent'
|| handlerInput.requestEnvelope.request.intent.name === 'AMAZON.StopIntent');
},
handle(handlerInput) {
let speechText = 'Goodbye from My Calculator';
return handlerInput.responseBuilder
.speak(speechText)
.withSimpleCard(appName, speechText)
.getResponse();
}
};
const SessionEndedRequestHandler = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'SessionEndedRequest';
},
handle(handlerInput) {
//any cleanup logic goes here
return handlerInput.responseBuilder.getResponse();
}
};
//Lambda handler function
//Remember to add custom request handlers here
exports.handler = Alexa.SkillBuilders.custom()
.addRequestHandlers(LaunchRequestHandler,
AddIntentHandler,
SubstractionIntentHandler,
HelpIntentHandler,
CancelAndStopIntentHandler,
SessionEndedRequestHandler).lambda();