· 6 years ago · Sep 10, 2019, 01:22 PM
1const AccountIntentHandler = {
2 // Triggers when user wants to lookup account info
3 canHandle(handlerInput) {
4 return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
5 && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AccountIntent';
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 // Get account from Alpaca trade API and craft response
17 const account = await api.getAccount();
18 const speakOutput = `Account info: current equity is ${account.equity}, current cash is ${account.cash}, buying power is ${account.buying_power}, portfolio value is ${account.portfolio_value}, currency is ${account.currency}.`;
19
20 // Send verbal response to user
21 return handlerInput.responseBuilder
22 .speak(speakOutput)
23 .getResponse();
24 }
25};