· 6 years ago · Sep 10, 2019, 01:26 PM
1const CancelOrderIntentHandler = {
2 canHandle(handlerInput) {
3 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
4 && Alexa.getIntentName(handlerInput.requestEnvelope) === 'CancelOrderIntent';
5 },
6 async handle(handlerInput) {
7 // Get user inputs and declare the Alpaca object
8 const slots = handlerInput.requestEnvelope.request.intent.slots;
9 const api = new Alpaca({
10 keyId: keyId,
11 secretKey: secretKey,
12 paper: true
13 });
14
15 const orders = await api.getOrders({
16 status: "open",
17 limit: 1,
18 });
19
20 // Get the most recent order and cancel it
21 if(orders.length == 0){
22 return handlerInput.responseBuilder
23 .speak("No orders to cancel.")
24 .getResponse();
25 } else {
26 await api.cancelOrder(orders[0].id);
27
28 // Send verbal response to user
29 const speakOutput = "Order canceled.";
30 return handlerInput.responseBuilder
31 .speak(speakOutput)
32 .getResponse();
33 }
34 }
35};