Coder Social home page Coder Social logo

dark3nz0 / alexa-smarthome-validation Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 11.0 53 KB

This package allows you to validate your responses to the Alexa Smart Home API to help you catch and reduce errors.

License: Other

JavaScript 52.93% Python 47.07%

alexa-smarthome-validation's Introduction

Alexa Smart Home Skill API Validation Package

This package provides modules in Node.js and Python that can be easily used in Lambda functions of Alexa Smart Home API skills to validate the responses before sending back to Alexa. The validation rules are based on the current Smart Home Skill API reference.

If a validation error is found, an exception is thrown by the Lambda function so that the skill developer can see those exceptions in CloudWatch and do something about them. Otherwise, although the Lambda function may have completed successfully, an error is sent back to Alexa, resulting in a failure for the Alexa user.

We encourage all Smart Home API skill developers to use this package as a way to ensure that their Lambda responses are always correct from the Alexa point of view, thereby improving operational excellence. As we add or modify the Smart Home Skill API, we will also update this package.

Please provide feedback as well. If we've made a mistake, or if you have any questions about specific validations, please submit an issue here.

How to Get Started

Node.js

  1. Get the validation.js file by cloning the project:
git clone https://github.com/alexa/alexa-smarthome-validation.git

Add the file to your Lambda function package. Assume it is in the same directory as your Lambda function.

At the top of your Lambda function, add:

var validator = require('validation');

At the beginning of your Lambda handler function, add the context validation. For example:

exports.handler = function(request, context) {
	try {
		validator.validateContext(request, response);
	} catch (error) {
	    log('FATAL', error);
	    throw(error);
	}
	...

Your Lambda will immediately fail if your Lambda's timeout setting is greater than 7 seconds.

Then, anytime you return a response, i.e. context.succeed(response), add a validation check in a try/catch block. Note that if you already have a live skill and you want to be safe, just wrap the call to validator.validateResponse in a try/catch block. This allows you to log an error for your reference, while still returning a response from your Lambda. For example:

var response = {header, payload};
try {
    validator.validateResponse(request, response);
} catch (error) {
    log('FATAL', error);
}
context.succeed(response);

Test your skill with some bad responses to see if this works. Then you should setup some CloudWatch alarms on error metrics for your Lambda to be alerted of these errors going forward.

Python

Get the validation.py file by cloning the project:

git clone https://github.com/alexa/alexa-smarthome-validation.git

Add the file to your Lambda function package. Assume it is in the same directory as your Lambda function.

At the top of your Lambda function, add:

from validation import validateResponse, validateContext

At the beginning of your Lambda handler function, add the context validation. For example:

def lambda_handler(event,context):
	try:
        validateContext(context)
    except ValueError as error:
        logger.error(error)
        raise
    ...        

Your Lambda will immediately fail if your Lambda's timeout setting is greater than 7 seconds.

Then, anytime you return a response, i.e. context.succeed(response), add a validation check in a try/catch block. Note that if you already have a live skill and you want to be safe, just wrap the call to validator.validateResponse in a try/catch block. This allows you to log an error for your reference, while still returning a response from your Lambda. For example:

try:
    validateResponse(request,response)
except ValueError as error:
    logger.error(error)
return response

Test your skill with some bad responses to see if this works. Then you should setup some CloudWatch alarms on error metrics for your Lambda to be alerted of these errors going forward.

Updates

Please watch this repo as we will update these validation packages every time the Smart Home API is updated.

Feedback

We welcome all feedback. Please create an issue in this repo if you have any questions, comments, or suggestions.

alexa-smarthome-validation's People

Contributors

dairong avatar ngrices avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.