-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa-moih.js
38 lines (36 loc) · 1.49 KB
/
alexa-moih.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
const MarketOrderIntentHandler = {
// Triggers when user invokes a market order
canHandle(handlerInput) {
return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
&& Alexa.getIntentName(handlerInput.requestEnvelope) === 'MarketOrderIntent';
},
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
});
let sym = `${slots['sym_one'].value ? slots['sym_one'].value : ""}${slots['sym_two'].value ? slots['sym_two'].value : ""}${slots['sym_three'].value ? slots['sym_three'].value : ""}${slots['sym_four'].value ? slots['sym_four'].value : ""}${slots['sym_five'].value ? slots['sym_five'].value : ""}`
sym = sym.toUpperCase();
// Submit the market order using the Alpaca trading api
let resp = await api.createOrder({
symbol: sym,
qty: parseInt(slots['quantity'].value),
side: slots['side'].value,
type: 'market',
time_in_force: slots['time_in_force'].value,
}).then((resp) => {
return `Market order of ${slots['side'].value}, ${slots['quantity'].value}, ${sym.split("").join(", ")} sent.`;
}).catch((err) => {
return `Error: ${err.error.message}`;
}).then((resp) => {
return handlerInput.responseBuilder
.speak(resp)
.getResponse();
});
// Send verbal response back to user
return resp;
}
};