User Tools

Site Tools


echo_stock_price

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
echo_stock_price [2017/05/30 16:39] – created tbrodeurecho_stock_price [2017/05/30 17:13] (current) tbrodeur
Line 18: Line 18:
  
 <fs large>Overview</fs> <fs large>Overview</fs>
 +
 +Create an Alexa Skill that returns stock data for 10 listed companies.
  
  
Line 32: Line 34:
 2. <fs large>Add the code</fs> 2. <fs large>Add the code</fs>
  
 +<fc #4682b4>
 +******The code below is commented out in order to provide you with a better understanding. If you have any further questions, please feel free to contact me at the email listed above.******</fc>
  
 <code python index.py> <code python index.py>
Line 46: Line 50:
  if (event['session']['application']['applicationId'] !=  if (event['session']['application']['applicationId'] !=
  #Change to application id listed under your alexa skill in the developer portal  #Change to application id listed under your alexa skill in the developer portal
- "amzn1.ask.skill.f976aad9-6567-4a27-8bce-7a0cfe9861e7"):+ "amzn1.ask.skill.XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"):
  raise ValueError("Invalid Application ID")  raise ValueError("Invalid Application ID")
  
Line 291: Line 295:
 <fc #4682b4> <fc #4682b4>
 ******Make sure to change the application id listed in lambda_handler() to your own id once you create the Alexa Skill with Alexa Skills Kit******</fc> ******Make sure to change the application id listed in lambda_handler() to your own id once you create the Alexa Skill with Alexa Skills Kit******</fc>
- 
-<color #22b14c>----------------------------------------------------------------------------------------------------------------------------- 
-</color> 
- 
-The handler function takes two parameters: 
- 
-  * event: will contain an object with information about the request. 
- 
-  * context: will represent the state of the Lambda function (for 
-  * example, how much time is left before the function times out). The 
-  * context parameter is also used for sending a response back to the 
-  * caller. 
- 
-In this tutorial, we’ll run the web service on AWS Lambda, a service 
-that executes code in response to events, thus saving the developer 
-the trouble of maintaining a server. Lambda also links seamlessly to 
-the Alexa Skills Kit, which makes it an excellent tool for running the 
-code for Alexa Skills. 
- 
-We will implement the server functionality as a python module, so to 
-complete the tutorial, you’ll need a basic understanding of python2.7. 
- 
-Every request from the Alexa Skills Kit has a type, passed in event.request.type. 
- 
-In all, there are three request types, all of which the service needs to 
-respond to separately: 
- 
-  * Launch request: Sent when the user launches the Skill. The 
-  * example code calls the helper function onLaunch to initialize the 
-  * help function. 
- 
-  * SessionEndedRequest: Sent when the user stops using the skill 
-  * by saying “exit”, by not responding for a while, or if an error occurs. 
-  * The example code calls the helper function onSessionEnded, 
-  * which is currently just a placeholder and doesn’t do anything. 
- 
-  * IntentRequest: Sent when the user speaks a command that 
-  * maps to an intent. The example code calls the helper function 
-  * onIntent. 
- 
-Amazon’s documentation explains that: 
- 
-“[A]n intent represents a high-level action that fulfills a user’s 
-spoken request. Intents can optionally have arguments called 
-slots that collect additional information needed to fulfill the 
-user’s request.” 
- 
-When a user speaks a command, Alexa converts it to text and 
-compares it to a list of phrases the application understands. Each 
-phrase is mapped to an intent definition so that your code doesn’t 
-have to worry about parsing the free-form input from the user but can 
-rely on the intents to organize its communication with the user. 
- 
-<color #22b14c>----------------------------------------------------------------------------------------------------------------------------- 
-</color> 
- 
- 
-You’ll soon see how to specify the intents for your skill, but first, let’s 
-take a look at the onIntent function to see how Alexa handles 
-the intents. 
- 
-<code python> 
-def on_intent(intent_request, session): 
- intent = intent_request["intent"] 
- intent_name = intent_request["intent"]["name"] 
- 
- if intent_name == "Hello": 
- return say_hello() 
- elif intent_name == "AMAZON.HelpIntent": 
- return get_welcome_response() 
- elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent": 
- return handle_session_end_request() 
- else: 
- raise ValueError("Invalid intent") 
-</code> 
- 
-As you can see, most of the function is an if..else structure that 
-compares the intent name from the request to a set of intents the 
-skill accepts. Depending on the intent name, it then calls a matching function. 
- 
-For example, when Alexa receives a request with the intent name 
-Hello, it calls the corresponding say_hello() function. 
- 
-<color #22b14c>----------------------------------------------------------------------------------------------------------------------------- 
-</color> 
- 
-Looking at the say_hello function,  
- 
-<code python> 
-def say_hello(): 
- session_attributes = {} 
- card_title = "Hello World" 
- speech_output = "Hello World" 
- reprompt_text = "" 
- should_end_session = True 
- 
- return build_response(session_attributes, build_speechlet_response( 
-        card_title, speech_output, reprompt_text, should_end_session)) 
-</code> 
- 
-we can see that the function returns a build_response function that will pass arguments defined in the say_hello function. 
- 
--card_title: name of the card that will display on the Alexa app on your smartphone 
- 
--speech_output: what the Echo or Echo Dot will ouput once the function build_response function is called 
- 
--should_end_session: tells Alexa whether to end the skill session after a response has been outputed. This should be set to True in this case. If a reprompt text is defined, usually should_end_session would be set to false, however all we are doing with this function is asking for a simple output. 
  
 <color #22b14c>----------------------------------------------------------------------------------------------------------------------------- <color #22b14c>-----------------------------------------------------------------------------------------------------------------------------
echo_stock_price.1496187584.txt.gz · Last modified: by tbrodeur