Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

Commit

Permalink
Committing in sample NodeJS skill that uses the Alexa Device Address API
Browse files Browse the repository at this point in the history
  • Loading branch information
Duran committed Apr 5, 2017
0 parents commit 92b75ff
Show file tree
Hide file tree
Showing 12 changed files with 539 additions and 0 deletions.
26 changes: 26 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,26 @@
Amazon Software License
1. Definitions
“Licensor” means any person or entity that distributes its Work.

“Software” means the original work of authorship made available under this License.

“Work” means the Software and any additions to or derivative works of the Software that are made available under this License.

The terms “reproduce,” “reproduction,” “derivative works,” and “distribution” have the meaning as provided under U.S. copyright law; provided, however, that for the purposes of this License, derivative works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work.

Works, including the Software, are “made available” under this License by including in or with the Work either (a) a copyright notice referencing the applicability of this License to the Work, or (b) a copy of this License.
2. License Grants
2.1 Copyright Grant. Subject to the terms and conditions of this License, each Licensor grants to you a perpetual, worldwide, non-exclusive, royalty-free, copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense and distribute its Work and any resulting derivative works in any form.
2.2 Patent Grant. Subject to the terms and conditions of this License, each Licensor grants to you a perpetual, worldwide, non-exclusive, royalty-free patent license to make, have made, use, sell, offer for sale, import, and otherwise transfer its Work, in whole or in part. The foregoing license applies only to the patent claims licensable by Licensor that would be infringed by Licensor’s Work (or portion thereof) individually and excluding any combinations with any other materials or technology.
3. Limitations
3.1 Redistribution. You may reproduce or distribute the Work only if (a) you do so under this License, (b) you include a complete copy of this License with your distribution, and (c) you retain without modification any copyright, patent, trademark, or attribution notices that are present in the Work.
3.2 Derivative Works. You may specify that additional or different terms apply to the use, reproduction, and distribution of your derivative works of the Work (“Your Terms”) only if (a) Your Terms provide that the use limitation in Section 3.3 applies to your derivative works, and (b) you identify the specific derivative works that are subject to Your Terms. Notwithstanding Your Terms, this License (including the redistribution requirements in Section 3.1) will continue to apply to the Work itself.
3.3 Use Limitation. The Work and any derivative works thereof only may be used or intended for use with the web services, computing platforms or applications provided by Amazon.com, Inc. or its affiliates, including Amazon Web Services, Inc.
3.4 Patent Claims. If you bring or threaten to bring a patent claim against any Licensor (including any claim, cross-claim or counterclaim in a lawsuit) to enforce any patents that you allege are infringed by any Work, then your rights under this License from such Licensor (including the grants in Sections 2.1 and 2.2) will terminate immediately.
3.5 Trademarks. This License does not grant any rights to use any Licensor’s or its affiliates’ names, logos, or trademarks, except as necessary to reproduce the notices described in this License.
3.6 Termination. If you violate any term of this License, then your rights under this License (including the grants in Sections 2.1 and 2.2) will terminate immediately.
4. Disclaimer of Warranty.
THE WORK IS PROVIDED “AS IS” WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF M ERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY ACTIVITIES UNDER THIS LICENSE. SOME STATES’ CONSUMER LAWS DO NOT ALLOW EXCLUSION OF AN IMPLIED WARRANTY, SO THIS DISCLAIMER MAY NOT APPLY TO YOU.
5. Limitation of Liability.
EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE SHALL ANY LICENSOR BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF OR RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK (INCLUDING BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION, LOST PROFITS OR DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER COMM ERCIAL DAMAGES OR LOSSES), EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Effective Date – April 18, 2008 © 2008 Amazon.com, Inc. or its affiliates. All rights reserved.
42 changes: 42 additions & 0 deletions README.md
@@ -0,0 +1,42 @@
# NodeJS ASK Alexa Device Address API Integration Sample Project

The Alexa Skills Kit now allows developers to build skills that can retrieve a user's address via the new Alexa Device Address API.
## How to Run the Sample

1. Clone the project and package the skill:
```bash
git clone git@github.com:alexa/skill-sample-node-device-address-api.git
cd skill-sample-node-device-address-api
npm install
```
2. Create or login to an [AWS account](https://aws.amazon.com/). In the AWS Console:

1. Create an AWS Role in IAM with access to Lambda, CloudWatch Logs and DynamoDB.
![create_role_1](https://cloud.githubusercontent.com/assets/7671574/17451098/09f64f40-5b19-11e6-82ee-b82c98387052.png "AWS Create Role Screenshot 1")
![create_role_2](https://cloud.githubusercontent.com/assets/7671574/17451100/0c3ef928-5b19-11e6-9aca-8cd353106396.png "AWS Create Role Screenshot 2")
![create_role_3](https://cloud.githubusercontent.com/assets/7671574/18011103/7b05f2b2-6b68-11e6-8dc3-3aa9ead6d83e.png "AWS Create Role Screenshot 3")

2. Create an AWS Lambda function named DeviceAddressLambdaFunction being sure to select the role created above, configuring "Alexa Skills Kit" as the "Trigger" and using the zip file created above as the source.
![alt text](https://s3.amazonaws.com/lantern-public-assets/audio-player-assets/aws-lambda-role.PNG "AWS Lambda Role")
![alt text](https://s3.amazonaws.com/lantern-public-assets/audio-player-assets/aws-lambda-ask-trigger.PNG "AWS Lambda Trigger")
3. After creation, take note of the ARN on the upper right, which you'll configure in the Developer Console below.

3. Create or login to an [Amazon Developer account](https://developer.amazon.com). In the Developer Console:

1. [Create an Alexa Skill](https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/developing-an-alexa-skill-as-a-lambda-function) named MySkill and using the invocation name "my skill".
2. Copy the contents of `speechAssets/intentSchema.json` and `speechAssets/Utterances.txt` into the intent schema and sample utterances fields on the Interaction Model tab.
![alt text](https://s3.amazonaws.com/lantern-public-assets/permissions-assets/intentSchema.png "Developer Portal Interaction Model")
![alt text](https://s3.amazonaws.com/lantern-public-assets/permissions-assets/utterances.png "Developer Portal Interaction Model")
3. Specify that you'll need permissions for access to the user's address. For the scope of this sample skill, you'll want the full address.
![alt text](https://s3.amazonaws.com/lantern-public-assets/permissions-assets/permissions.png "Developer Portal Configuration")

4. Edit the src/index.js file. You'll want to make APP_ID equal the Application Id for your skill.

5. Compress your skill's source code into a single archive:
```
zip -r packagedSkill.zip *
```

6. Upload the packagedSkill.zip from earlier to your Lambda function via the Code tab on the AWS Lambda Console.

7. You can start using the skill on your device or in the simulator using the invocation phrase "Alexa, ask my skill what's my address".
11 changes: 11 additions & 0 deletions index.js
@@ -0,0 +1,11 @@
'use strict';

/**
* This is a simple file that aliases your skill handler to a root
* level file so that you don't have to tinker with Lambda handler paths.
* This will make the default lambda value of "index.handler" work.
*/

const MyAlexaSkill = require('./src/index');

exports.handler = MyAlexaSkill.handler;
23 changes: 23 additions & 0 deletions package.json
@@ -0,0 +1,23 @@
{
"name": "skill-sample-node-device-address-api",
"version": "1.0.0",
"description": "A Device Address API sample skill.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"alexa",
"skill",
"settings"
],
"author": "Amazon.com",
"license": "SEE LICENSE IN LICENSE.txt",
"dependencies": {
"alexa-sdk": "^1.0.8"
},
"repository" : {
"type" : "git",
"url" : ""
}
}
16 changes: 16 additions & 0 deletions speechAssets/IntentSchema.json
@@ -0,0 +1,16 @@
{
"intents": [
{
"intent": "GetAddress"
},
{
"intent": "AMAZON.CancelIntent"
},
{
"intent": "AMAZON.HelpIntent"
},
{
"intent": "AMAZON.StopIntent"
}
]
}
5 changes: 5 additions & 0 deletions speechAssets/SampleUtterances_en_US.txt
@@ -0,0 +1,5 @@
GetAddress where am I located
GetAddress where do I live
GetAddress whats my address
GetAddress where am I
GetAddress whats my location
98 changes: 98 additions & 0 deletions src/AlexaDeviceAddressClient.js
@@ -0,0 +1,98 @@
'use strict';

const Https = require('https');

/**
* This is a small wrapper client for the Alexa Address API.
*/
class AlexaDeviceAddressClient {

/**
* Retrieve an instance of the Address API client.
* @param apiEndpoint the endpoint of the Alexa APIs.
* @param deviceId the device ID being targeted.
* @param consentToken valid consent token.
*/
constructor(apiEndpoint, deviceId, consentToken) {
console.log("Creating AlexaAddressClient instance.");
this.deviceId = deviceId;
this.consentToken = consentToken;
this.endpoint = apiEndpoint.replace(/^https?:\/\//i, "");
}

/**
* This will make a request to the Address API using the device ID and
* consent token provided when the Address Client was initialized.
* This will retrieve the full address of a device.
* @return {Promise} promise for the request in flight.
*/
getFullAddress() {
const options = this.__getRequestOptions(`/v1/devices/${this.deviceId}/settings/address`);

return new Promise((fulfill, reject) => {
this.__handleDeviceAddressApiRequest(options, fulfill, reject);
});
}

/**
* This will make a request to the Address API using the device ID and
* consent token provided when the Address Client was initialized.
* This will retrieve the country and postal code of a device.
* @return {Promise} promise for the request in flight.
*/
getCountryAndPostalCode() {
const options = this.__getRequestOptions(
`/v1/devices/${this.deviceId}/settings/address/countryAndPostalCode`);

return new Promise((fulfill, reject) => {
this.__handleDeviceAddressApiRequest(options, fulfill, reject);
});
}

/**
* This is a helper method that makes requests to the Address API and handles the response
* in a generic manner. It will also resolve promise methods.
* @param requestOptions
* @param fulfill
* @param reject
* @private
*/
__handleDeviceAddressApiRequest(requestOptions, fulfill, reject) {
Https.get(requestOptions, (response) => {
console.log(`Device Address API responded with a status code of : ${response.statusCode}`);

response.on('data', (data) => {
let responsePayloadObject = JSON.parse(data);

const deviceAddressResponse = {
statusCode: response.statusCode,
address: responsePayloadObject
};

fulfill(deviceAddressResponse);
});
}).on('error', (e) => {
console.error(e);
reject();
});
}

/**
* Private helper method for retrieving request options.
* @param path the path that you want to hit against the API provided by the skill event.
* @return {{hostname: string, path: *, method: string, headers: {Authorization: string}}}
* @private
*/
__getRequestOptions(path) {
return {
hostname: this.endpoint,
path: path,
method: 'GET',
'headers': {
'Authorization': 'Bearer ' + this.consentToken
}
};
}
}

module.exports = AlexaDeviceAddressClient;
40 changes: 40 additions & 0 deletions src/Events.js
@@ -0,0 +1,40 @@
'use strict';

/**
* This file contains all the events that we'll be
* interested in outside of a normal intent.
*/

/**
* Your skill will receive a NewSession event when a
* session has been started on your skill. An example of this would be
* when a user says "open skill blah blah blah".
*/
const NEW_SESSION = "NewSession";

/**
* Your service receives a LaunchRequest when the user invokes the skill with the
* invocation name, but does not provide any command mapping to an intent.
* Refer to the following URL for documentation:
* https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa#launchrequest
*/
const LAUNCH_REQUEST = "LaunchRequest";

/**
* Your service receives a SessionEndedRequest when a currently open session is closed.
* https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/handling-requests-sent-by-alexa#sessionendedrequest
*/
const SESSION_ENDED = "SessionEndedRequest";

/**
* Your skill will receive an Unhandled event when it receives an intent that
* it has not registered for.
*/
const UNHANDLED = "Unhandled";

module.exports = {
"NEW_SESSION": NEW_SESSION,
"LAUNCH_REQUEST": LAUNCH_REQUEST,
"SESSION_ENDED": SESSION_ENDED,
"UNHANDLED": UNHANDLED
};

0 comments on commit 92b75ff

Please sign in to comment.