Skip to content

Instantly share code, notes, and snippets.

@dave-malone
Created January 3, 2018 22:13
Show Gist options
  • Save dave-malone/671a021417cd591a61d888b434cbf4d2 to your computer and use it in GitHub Desktop.
Save dave-malone/671a021417cd591a61d888b434cbf4d2 to your computer and use it in GitHub Desktop.
/* eslint-disable func-names */
/* eslint quote-props: ["error", "consistent"]*/
/**
* This sample demonstrates a simple skill built with the Amazon Alexa Skills
* nodejs skill development kit.
* This sample supports multiple lauguages. (en-US, en-GB, de-DE).
* The Intent Schema, Custom Slots and Sample Utterances for this skill, as well
* as testing instructions are located at https://github.com/alexa/skill-sample-nodejs-fact
**/
'use strict';
const Alexa = require('alexa-sdk');
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-1'
const APP_ID = undefined;
const IOT_ENDPOINT=process.env.IOT_ENDPOINT
const MQTT_TOPIC=process.env.MQTT_TOPIC
const ACCESS_KEY_ID=process.env.ACCESS_KEY_ID
const SECRET_ACCESS_KEY=process.env.SECRET_ACCESS_KEY
const iotdata = new AWS.IotData({
endpoint: IOT_ENDPOINT,
accessKeyId: ACCESS_KEY_ID,
secretAccessKey: SECRET_ACCESS_KEY,
region:'us-east-1',
httpOptions:{
xhrAsync: false,
}
});
const languageStrings = {
'en': {
translation: {
SKILL_NAME: 'LED Controller',
GET_FACT_MESSAGE: "And there was light ",
HELP_MESSAGE: 'You can say turn on the light, or, you can say exit... What can I help you with?',
HELP_REPROMPT: 'What can I help you with?',
STOP_MESSAGE: 'Goodbye!',
},
},
};
function publishMessage(){
}
const handlers = {
'LaunchRequest': function () {
this.emit('ControlLight');
},
'ControlLightIntent': function () {
this.emit('ControlLight');
},
'ControlLight': function () {
// Get a random space fact from the space facts list
// Use this.t() to get corresponding language data
// Create speech output
//const speechOutput = this.t('GET_FACT_MESSAGE');
//this.emit(':tellWithCard', speechOutput, this.t('SKILL_NAME'), speechOutput);
var params = {
topic: MQTT_TOPIC,
payload: '{"state" : "on"}',
qos: 1
};
console.log('iotdata.publish::params: ' + JSON.stringify(params))
iotdata.publish(params, (err, data) => {
if(err){
this.emit(':tellWithCard', `an error occurred when publishing iot message: ${err}`, this.t('SKILL_NAME'),
"I'm sorry, I couldn't turn on your light");
}
else{
this.emit(':tellWithCard', 'And there was light', this.t('SKILL_NAME'), 'And there was light');
//context.succeed(event);
}
});
},
'AMAZON.HelpIntent': function () {
const speechOutput = this.t('HELP_MESSAGE');
const reprompt = this.t('HELP_MESSAGE');
this.emit(':ask', speechOutput, reprompt);
},
'AMAZON.CancelIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
'AMAZON.StopIntent': function () {
this.emit(':tell', this.t('STOP_MESSAGE'));
},
};
exports.handler = function (event, context) {
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
// To enable string internationalization (i18n) features, set a resources object.
alexa.resources = languageStrings;
alexa.registerHandlers(handlers);
alexa.execute();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment