Skill link: https://www.amazon.com/dp/B01MTO5EZH (Published Nov 25, 2016) (Updated Dec 3, 2016)
Alexa is great for quick and dirty information.
StockQuoter gets you the stock price information you need, right when you need it.
Ask Alexa, "Ask Stock Quoter what's the stock price of Alphabet?" or launch Stock Quoter and when prompted say any company name like "Amazon" and it'll tell you right away. If the price of the stock changes by more than 1% in after market or pre-market, it'll let you know as well.
The data is pulled right from Google Finance, supplying (almost) real-time price data.
How it's built:
Let's start off with Intent Schema.
We really only need one thing from the user, which is the company name. We can do this by defining an intent "handleOneshotStockIntent" with a custom slot of CompanyName.
Because a lot of company names sound weird, we should probably get a list of companies that are trading. This way, we can create a custom slot type with all the possible companies to lookup. Good thing finra (Financial Industry Regulatory Authority) provides one! You can get it here: http://oatsreportable.finra.org/OATSReportableSecurities-SOD.txt
We can now add it to our Custom Slot Types after a bit of data processing (separating the name from symbol and market and removing duplicates. It provides way more than you really need... Work can be done to shorten this list and clean up securities that you wouldn't really ask alexa for)
Moving on to the Sample Utterances. We think of some ways of asking for stock prices, and we substitute the company name for our slot {CompanyName}.
The nice thing about this type of skill is that we don't actually need a separate dialog to guide users through a query. There's two scenarios. User doesn't know what to do and user already knows what to do. If the user already knows what to do, s/he will most likely ask "What is the stock price of {Company}." If they don't, they'll most likely launch the skill, or ask help. Which ends with Alexa asking "What company would you like to look up?". So we can simply add a utterance that is only a single {CompanyName} slot type to get enough information to answer the query. Because they'd either reply with just the name of a company, or ask another question that includes the name of the company. (We'll handle what happens if they don't say anything by giving them an example and asking them again, see reprompt strings in the code)
Now that's done, we deploy our code to AWS Lambda. You can do this by zipping up the source code, and uploading it to AWS Lambda. Make sure you're on AWS East (N. Virginia), at the time of this writing, only AWS East has Alexa Skills Kit trigger.
First, Create a new function. Look for this button:
Next, ignore the page and click Configure triggers on the left nav bar.
Select Alexa Skills Kit from the trigger dropdown.
Upload the zipped source code, including node_modules folder. This is important as our code uses external sources. name your function whatever you want. it is not important.
Copy the ARN. (red box in screenshot). You'll need this to link your Alexa Skill to the AWS Lambda function.
Back in the Alexa Skill developer console, go to the configuration page of your skill and copy in the ARN in the text box:
Click Next, and test your Skill!
Now there's one more step. There is no public API to lookup stock symbols. Which we need to look up the stock quotes. We'll need to host this ourselves. You'll need to update the endpoint for the stock symbol lookup in index.js using the in-line edit feature of AWS lambda console.
-----
Code Info:We used two APIs, one to look up the stock symbol given a company name, and the second to get the real-time stock data.
for stock symbol lookup, we can quickly write our own and use the data from finra. The code is also on github here: https://github.com/tuggyboat/StockSymbolSearch Note that symbolManger.py should run every 24 hours to update the database for possible new or removal of companies. symbolHost.py will run a webserver script to host our api. You may use AWS free tier or openshift from redhat for free to host this. Once hosted, remember to update the endpoint URL in index.js in the AWS lambda function.
for the stock quote data, we'll use google finance. We can see an example response here: http://finance.google.com/finance/info?client=ig&q=NASDAQ:GOOGL
See the code for more information regarding what alias references what data it represents.
And that's it!
**If you publish a skill similar to this, it'd be a good idea to have a disclaimer about the data and it's use. Cause legal reasons.
Comments