Created
August 29, 2019 22:13
-
-
Save andrewkim316/47dbad1b0da039361907fa5b589d2fc5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment