· 6 years ago · Sep 10, 2019, 01:22 PM
1const PositionsIntentHandler = {
2 // Triggers when user wants to look up positions
3 canHandle(handlerInput) {
4 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
5 && Alexa.getIntentName(handlerInput.requestEnvelope) === 'PositionsIntent';
6 },
7 async handle(handlerInput) {
8 // Get user inputs and declare the Alpaca object
9 const slots = handlerInput.requestEnvelope.request.intent.slots;
10 const api = new Alpaca({
11 keyId: keyId,
12 secretKey: secretKey,
13 paper: true
14 });
15
16 // Lookup positions and craft response
17 let resp = await api.getPositions().then((positions) => {
18 if(positions.length > 0) {
19 var speakOutput = "Listing positions. ";
20 positions.forEach((position,i) => {
21 let sym = position.symbol.split("").join(", ");
22 speakOutput += `Position ${i + 1}: ${sym}, ${position.qty}, ${position.side} position, average entry price of ${parseFloat(position.avg_entry_price).toFixed(2)}.`;
23 });
24 return speakOutput;
25 }
26 else {
27 return "No open positions.";
28 }
29 }).catch((err) => {
30 return `Error: ${err.error.message}`;
31 }).then((resp) => {
32 // Send verbal response to user
33 return handlerInput.responseBuilder
34 .speak(resp)
35 .getResponse();
36 });
37
38 return resp;
39 }
40};