User Tools

Site Tools


music_player_skill

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
music_player_skill [2017/01/01 19:12] santiagoricoymusic_player_skill [2017/01/02 00:38] (current) santiagoricoy
Line 1: Line 1:
 ====== Creating an Alexa Skill to Play a Music File ====== ====== Creating an Alexa Skill to Play a Music File ======
 +
 +
 +{{ youtube>Yco-tKQ4k1k?large }} 
 +
 +
 +
 +
 <!-- Replace the above line with the name of your "How To" Tutorial e.g. How to Laser cut Your Name in Wood --> <!-- Replace the above line with the name of your "How To" Tutorial e.g. How to Laser cut Your Name in Wood -->
  
Line 10: Line 17:
 **Keywords:** alexa skill tutorial voice audio playback alexa sdk **Keywords:** alexa skill tutorial voice audio playback alexa sdk
 \\ \\
 +
 +----
  
 <!-- Add a representative photo of your tutorial below.  Make it centered in the page --> <!-- Add a representative photo of your tutorial below.  Make it centered in the page -->
Line 18: Line 27:
  
 This picture shows part of the Amazon Developer console, which allows you to use many items offered by Amazon to developers; in this case, testing the voice-controlled side of an Alexa skill. We need to understand how to stream files through our Alexa-enabled device. Solving this some degree is important because it demonstrates the ability to fetch files from the web with the Alexa Voice Service (AVS), which implies it can be used to manipulate and control other items in the cloud, and connected devices. This tutorial shows you how to set up a lambda function that talks to the voice-operated end of an Alexa skill.  It takes approximately 40 minutes to complete.  This picture shows part of the Amazon Developer console, which allows you to use many items offered by Amazon to developers; in this case, testing the voice-controlled side of an Alexa skill. We need to understand how to stream files through our Alexa-enabled device. Solving this some degree is important because it demonstrates the ability to fetch files from the web with the Alexa Voice Service (AVS), which implies it can be used to manipulate and control other items in the cloud, and connected devices. This tutorial shows you how to set up a lambda function that talks to the voice-operated end of an Alexa skill.  It takes approximately 40 minutes to complete. 
 +
 +
 +
 +----
  
 \\ \\
Line 42: Line 55:
   * [[music_player_skill#Programming | Programming]]   * [[music_player_skill#Programming | Programming]]
   * [[music_player_skill#Final Words | Final Words]]   * [[music_player_skill#Final Words | Final Words]]
 +
 +
 +----
 +
  
 ==== Required Items ==== ==== Required Items ====
Line 60: Line 77:
 ^PART NAME/DESCRIPTION             ^VENDOR              ^VENDOR Number or URL       ^PRICE          ^QTY       ^ ^PART NAME/DESCRIPTION             ^VENDOR              ^VENDOR Number or URL       ^PRICE          ^QTY       ^
 | Amazon Echo Dot   | Amazon.com         |https://www.amazon.com/All-New-Amazon-Echo-Dot-Add-Alexa-To-Any-Room/dp/B01DFKC2SO/ref=sr_1_2?ie=UTF8&qid=1483065428&sr=8-2&keywords=echo    |$49.99     | 1 | | Amazon Echo Dot   | Amazon.com         |https://www.amazon.com/All-New-Amazon-Echo-Dot-Add-Alexa-To-Any-Room/dp/B01DFKC2SO/ref=sr_1_2?ie=UTF8&qid=1483065428&sr=8-2&keywords=echo    |$49.99     | 1 |
 +
 +
 +
 +----
  
  
Line 205: Line 226:
  
 It's time to begin testing...maybe troubleshooting? It's time to begin testing...maybe troubleshooting?
 +
 +
 +----
 +
  
 ==== Programming ==== ==== Programming ====
Line 212: Line 237:
 Well, we'll go through a quick overview here, and I'll link relevant content as I go. Well, we'll go through a quick overview here, and I'll link relevant content as I go.
  
-    1. **The Alexa Skill**+1. **The Alexa Skill** 
 +    
 The skill built through the developer console has no actual programming involved.  The skill built through the developer console has no actual programming involved. 
  
 What we do define is what the end user will say to invoke our skill, or navigate options within the skill. What we do define is what the end user will say to invoke our skill, or navigate options within the skill.
 +
 +{{:sricoy:echo_music:alexainteractionmodel.png?nolink&600|}}
  
 The intent schema and sample utterances combined represent things the user might say, and what to do in response. The intent schema and sample utterances combined represent things the user might say, and what to do in response.
  
 +The intent schema is where we set up which intents we will use. Intents are a way of specifying the request sent by the skill to our function in Lambda.
  
-  +In our example, we use a custom intent called "PlayAudio". In the sample utterances section, we wrote possible things the end user will say that we want to invoke the "PlayAudio" intent. You will notice that there are other intents in our intent schema. These are built-in intents and we do not have to define the possible utterances for that Alexa will understand
-    2**The Lambda Function**+
  
 +For more on defining an Alexa skill's voice interface, please visit this [[https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/defining-the-voice-interface#The Intent Schema|link]].
 +
 +{{:alexaglobalfields.png?nolink&500|}}
 +
 +
 +We must send our request somewhere, and the global fields section of our skill allows us to specify whether we are sending requests to our own URL or to an AWS Lambda function. It is easier to use the Amazon Resource Number (ARN) of a Lambda function, because we do not have to deal with any specifics concerning how our requests are sent to the Lambda function, we only need to write the code that our function will use to handle requests.
  
    
 +2. **The Lambda Function**
 +
 +The Lambda function is where our code is hosted and responds when triggered by the Alexa skill. Otherwise, the function will sit idle and do nothing, making it very efficient for our purposes. With that said, let's take a brief look at our code below.
 +
 +<code javascript>
 +
 +'use strict';
 +
 +var Alexa = require('alexa-sdk'); //import Alexa SDK 
 +var constants = require('./constants'); //will need values from constants.js
 +var stateHandlers = require('./stateHandlers'); //need to register statehandlers
 +var audioEventHandlers = require('./audioEventHandlers');//must register audio event handlers
 +
 +exports.handler = function(event, context, callback){
 +    var alexa = Alexa.handler(event, context);
 +    alexa.appId = constants.appId;
 +    alexa.dynamoDBTableName = constants.dynamoDBTableName; //table name
 +    alexa.registerHandlers( //this function allows us to register our handlers
 +        stateHandlers.startModeIntentHandlers,//we can register multiple at a time
 +        stateHandlers.playModeIntentHandlers,
 +        stateHandlers.remoteControllerHandlers,
 +        stateHandlers.resumeDecisionModeIntentHandlers,
 +        audioEventHandlers //there are multiple in this file
 +    );
 +
 +    alexa.execute(); //just makes it all happen
 +    
 +};
 +
 +</code>
 +
 +
 +If you recall in the Lambda function, we use "index.handler" when we defined our handler in the configuration page. That is how our lambda function accesses the handlers that are used to process the requests. A handler looks for a specific intent and when it is invoked, runs the actions we program. Inside of the files imported into index.js, we can find the actual handlers. 
 +
 +For more on handling requests, please visit this link: [[https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa|Handling Requests]]
 +
 +3. **IAM and the DynamoDB Table**
 +   
 +{{:iammanagement.png?nolink&500|}}
 +   
 +In our example, we don't explicitly setup an Amazon DynamoDB table, but rather it is set up by our Lambda function. However, when we created a role with the Identity and Access Management service (IAM) and set it as our Lambda function's role, we were giving our function permission to create that table for us, as well as access to our Amazon Cloudwatch service.
 +
 +Amazon CloudWatch is a service that allows us to track metrics of our Amazon Web Services (AWS) account. This ability isn't explicitly used by the skill, but there may be overlap in permissions from CloudWatch that let the function create a table in DynamoDB. 
 +
 +**NOTE:** If you intend to repurpose this sample and change what is actually played by the skill, you will need to go into DynamoDB and delete the table created by the function, as it holds details about what the user has played, but may not replace them correctly with new audio sources.
 +
 +----
 +
  
 ==== Final Words ==== ==== Final Words ====
Line 230: Line 312:
 This walkthrough has gotten us off the ground with a music-playing Alexa skill, using built-in intents, Amazon DynamoDB, and Amazon IAM. For more on how skills work, please review other links [[echo_tutorial|here]] for an introduction to developing for Alexa. This walkthrough has gotten us off the ground with a music-playing Alexa skill, using built-in intents, Amazon DynamoDB, and Amazon IAM. For more on how skills work, please review other links [[echo_tutorial|here]] for an introduction to developing for Alexa.
  
 +For more information on this particular sample, please go through the README.md file.
  
  
music_player_skill.1483326755.txt.gz · Last modified: by santiagoricoy