alexa/skill-sample-nodejs-fact-in-skill-purchases

Name: skill-sample-nodejs-fact-in-skill-purchases

Owner: Alexa

Description: Demonstrates how to use the new in-skill purchase features of Alexa skills by offering different packs of facts behind a purchase, and a subscription to unlock all of the packs at once

Created: 2018-04-27 19:07:51.0

Updated: 2018-05-24 01:03:22.0

Pushed: 2018-05-21 19:52:54.0

Homepage:

Size: 392

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Build An Alexa Skill with In-Skill Purchases

Setup the ASK CLI

There are several aspects of developing an Alexa skill with in-skill purchases that require the use of the Alexa Skills Kit Command Line Interface (ASK CLI), so this entire walkthrough will require you to have installed and configured the ASK CLI. If you haven't done this before, here are the resources you need to get the ASK CLI installed on your machine:

If you have used the ASK CLI previously, you will also need to make sure that you have the most recent version of the ASK CLI. You can make sure you have the latest version by running the command:

m update -g ask-cli
Installation

Note: if you have git installed, you can clone this repo and skip this section.

Note: if you don't have git installed and want to skip this section, download the repo as a zip from here.

  1. Create a new skill using the CLI.

    k new
    
  2. Name the skill “Premium_Facts_Sample”.

    ease type in your new skill name:
    ium_Facts_Sample
    
  3. Navigate to your project folder.

     Premium_Facts_Sample
    
  4. Explore the project structure. You should see folders for lambda and models, and skill.json file.

    
    da      models      skill.json
    
  5. Open the models folder.

     models
    
  6. Open the interaction model file, en-US.json.

    en en-US.json
    
  7. Replace the contents of en-US.json with the contents of this JSON file, and save it.

  8. Go back to the project's root folder.

     ..
    
  9. Replace the contents of package.json with the contents of this JSON file, and save it.

  10. Navigate to the lambda folder.

     ../lambda/custom
    
  11. Open the AWS Lambda function code file, index.js.

    en index.js
    
  12. Replace the contents of index.js with the contents of this JS file, and save it.

  13. Update the Alexa SDK and the other npm dependencies.

    m update
    
  14. Navigate back to the root project directory.

     ../..
    
Creating In-Skill Products

There are ASK CLI commands for creating your in-skill purchases. This guide will walk you through creating three different one-time purchases (entitlements), as well as a subscription. Our sample code is expecting these to be created as described, so make sure to follow along carefully.

  1. Create your first in-skill product. You should be in the project's root directory.

    k add isp
    
  2. Choose Entitlement.

    st of in-skill product types you can choose (Use arrow keys)
    titlement
    cription
    
  3. Choose Entitlement_Template as your template.

    st of in-skill product templates you can choose (Use arrow keys)
    titlement_Template
    
  4. Name your in-skill product science_pack.

    ease type in your new in-skill product name:
    itlement_Template) science_pack
    
  5. Repeat steps #2 - #5 to create two more entitlements: history_pack and space_pack.

    ease type in your new in-skill product name:
    itlement_Template) history_pack
    
    ease type in your new in-skill product name:
    itlement_Template) space_pack
    
  6. Create a subscription product named all_access using a similar process.

    k add isp
    
    st of in-skill product types you can choose (Use arrow keys)
    titlement
    bscription
    
    st of in-skill product templates you can choose (Use arrow keys)
    bscription_Template
    
    ease type in your new in-skill product name:
    scription_Template) all_access
    
  7. Navigate to the new ISPs directory, and note the two folders, entitlement and subscription. This is where the JSON files for each of your in-skill products reside.

     isps
    
    
  8. Navigate to the entitlement folder. You should see three files in this directory, one for each of the entitlements you created in our previous steps.

     entitlement
    
    
  9. Open history_pack.json

    This JSON file contains all of the necessary fields for your in-skill product, but you'll need to add the details to get them ready to sell. Because we used the Entitlement_Template template, we have provided a small explanation for each field, make sure you replace all of them. Take a look at the sample in our docs for an additional reference. For this sample, at a minimum, you will need to update the name (not referenceName!), smallIconUri, largeIconUri, summary, description, purchasePromptDescription, boughtCardDescription, releaseDate and privacyPolicyUrl. Alternatively you can copy and paste the contents of the files found here: ISP Entitlements.

    After updating history.pack.json, Fill out the details for the science_pack.json and space_pack.json files. You will need to update with content about your science and space products including icons for each.

    IMPORTANT: Don't change the referenceName in your files, as our codebase is relying on those to be consistent.

    Once you are happy with your pricing, descriptions, and the other metadata for your three entitlements, you should update the same fields plus the subscriptionPaymentFrequency for your subscription. Alternatively you can copy and paste the contents of All Access ISP subscription sample into your all_access.json file.

  10. Review and edit the subscription file.

     ../subscription
    en all_access.json
    

    Now that you have customized your in-skill products, you can deploy your skill using the ASK CLI, and start testing it.

    Note: be sure to review the output to confirm there were no errors.

Deployment
  1. Navigate to the project's root directory. You should see a file named 'skill.json' there.

     ../..
    
  2. Deploy the skill and the Lambda function in one step by running the following command:

    k deploy
    

    Assuming that you followed all of the setup instructions for the ASK CLI, your entire skill and Lambda function should be created on their respective portals.

Testing
  1. To test, login to Alexa Developer Console, click on the Premium Facts Sample entry in your skill list, and click on the “Test” tab. The “Test” switch on your skill should have been automatically enabled. If it was not, enable it now.

  2. Your skill can now be tested on devices associated with your developer account, as well as the Test tab in the Developer Portal. To start using your skill, just type or say:

    a, open premium facts sample
    

Note: The developer account associated with the skill is never charged for in-skill products. For more details about testing skills with in-skill products, please refer to the In-Skill Purchase Testing Guide

License Summary

This sample code is made available under a modified MIT license. See the LICENSE file.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.