-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa-orders.js
40 lines (38 loc) · 1.29 KB
/
alexa-orders.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
const OrdersIntentHandler = {
// Triggers when user wants to look up orders
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'OrdersIntent';
},
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
});
// Lookup orders using Alpaca trade API and craft a response
let resp = await api.getOrders().then((orders) => {
if(orders.length > 0) {
var speakOutput = "Listing open orders. ";
orders.forEach((order,i) => {
let sym = order.symbol.split("").join(", ");
speakOutput += `Order ${i + 1}: ${sym}, ${order.qty}, ${order.type} order, ${order.side}, ${order.filled_qty} shares filled. `;
});
return speakOutput;
}
else {
return "No open orders.";
}
}).catch((err) => {
return `Error: ${err.error.message}`;
}).then((resp) => {
// Send verbal response back to user
return handlerInput.responseBuilder
.speak(resp)
.getResponse();
});
return resp;
}
};