-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalexa-cancel.js
35 lines (32 loc) · 1001 Bytes
/
alexa-cancel.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
const CancelOrderIntentHandler = {
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'CancelOrderIntent';
},
async handle(handlerInput) {
// Get user inputs and declare the Alpaca object
const slots = handlerInput.requestEnvelope.request.intent.slots;
const api = new Alpaca({
keyId: keyId,
secretKey: secretKey,
paper: true
});
const orders = await api.getOrders({
status: "open",
limit: 1,
});
// Get the most recent order and cancel it
if(orders.length == 0){
return handlerInput.responseBuilder
.speak("No orders to cancel.")
.getResponse();
} else {
await api.cancelOrder(orders[0].id);
// Send verbal response to user
const speakOutput = "Order canceled.";
return handlerInput.responseBuilder
.speak(speakOutput)
.getResponse();
}
}
};