Very New developer here... like really new... i'm building a skill to try to trigger a routine but I cant figure out why my device cannot be selected to trigger a routine. It adds to my account, but cannot be selected to trigger a routine. Any advice would be appreciated and talking to me like i'm dumb would help both of us lol. below is my index.js
'use strict';
console.log("test");
import AWS from 'aws-sdk';
import crypto from 'crypto';
import { v4 as uuid } from 'uuid';
import AlexaResponse from './alexa/skills/smarthome/AlexaResponse.js';
AWS.config.update({region:'us-east-1'});
//exports.handler = async function (event, context) {
export const handler = async function (event, context) {
// Dump the request for logging - check the CloudWatch logs
console.log("index.handler request -----");
console.log(JSON.stringify(event));
if (context !== undefined) {
console.log("index.handler context -----");
console.log(JSON.stringify(context));
}
// Validate we have an Alexa directive
if (!('directive' in event)) {
let aer = new AlexaResponse(
{
"name": "ErrorResponse",
"payload": {
"type": "INVALID_DIRECTIVE",
"message": "Missing key: directive, Is request a valid Alexa directive?"
}
});
return sendResponse(aer.get());
}
// Check the payload version
if (event.directive.header.payloadVersion !== "3") {
let aer = new AlexaResponse(
{
"name": "ErrorResponse",
"payload": {
"type": "INTERNAL_ERROR",
"message": "This skill only supports Smart Home API version 3"
}
});
return sendResponse(aer.get())
}
let namespace = ((event.directive || {}).header || {}).namespace;
if (namespace.toLowerCase() === 'alexa.authorization') {
let aar = new AlexaResponse({ "namespace": "Alexa.Authorization", "name": "AcceptGrant.Response", });
return sendResponse(aar.get());
}
// ATTEMPTING TO IMPLEMENT STATE REPORTING
if (namespace.toLowerCase() === 'alexa.statereport') {
let uuid = crypto.randomUUID();
let correlationToken = ((event.directive.header.correlationToken))
// let asr = new AlexaResponse({ "namespace": "Alexa", "name": "StateReport", "messageId": uuid, "correlationToken": correlationToken, "payloadVersion": "3" });
let asr = new AlexaResponse(event)
asr.addContextProperty({
"namespace": "AlexaContactSensor", "name": "detectionState", "value": "NOT_DETECTED", "timeOfSample": "timevariable", "uncertaintyInMilliseconds": 0
});
asr.addContextProperty({
"namespace": "AlexaEndpointHealth", "name": "connectivity", "value": {"value": "OK"}, "timeOfSample": "timevariable", "uncertaintyInMilliseconds": 0
});
/* let statereport_alexa = asr.createPayloadEndpoint({
"endpoint": {
"scope": {
"type": "BearerToken",
"token": "OAuth2.0 bearer token"
},
"endpointId": "Endpoint ID",
"payload": {}
}
});
*/
return sendResponse(asr.get());
}
//// Implement change reporting
if (namespace.toLowerCase() === 'alexa.changereport') {
let uuid = crypto.randomUUID();
let acr = new AlexaResponse(event)
asr.addContextProperty({
"namespace": "Alexa.ContactSensor", "name": "detectionState", "value": "DETECTED", "timeOfSample": "timevariable", "uncertaintyInMilliseconds": 0
});
return sendResponse(acr.get());
}
// Alexa Discovery
if (namespace.toLowerCase() === 'alexa.discovery') {
let adr = new AlexaResponse({"namespace": "Alexa.Discovery", "name": "Discover.Response", "payloadVersion": "3","messageId": "UniqueID"});
let capability_alexa = adr.createPayloadEndpointCapability();
/* let capability_alexa_powercontroller = adr.createPayloadEndpointCapability({
"interface": "Alexa.PowerController",
"properties": {
"supported": [
{
"name": "powerState"
}
],
"proactivelyReported": true,
"retrievable": true
}
}); */
let capability_alexa_contactsensor = adr.createPayloadEndpointCapability({
"interface": "Alexa.ContactSensor",
"properties": {
"supported": [
{
"name": "detectionState"
}
],
"proactivelyReported": true,
"retrievable": true
}
});
let capability_alexa_endpointhealth = adr.createPayloadEndpointCapability({
"interface": "Alexa.EndpointHealth",
"properties": {
"supported": [
{
"name": "connectivity"
}
],
"proactivelyReported": true,
"retrievable": true
}
});
adr.addPayloadEndpoint({
"friendlyName": "Zoom Light - Available",
"displayCategories": ["CONTACT_SENSOR"],
"endpointId": "sample-switch-01",
"capabilities": [capability_alexa, capability_alexa_endpointhealth, capability_alexa_contactsensor]
});
return sendResponse(adr.get());
}
if (namespace.toLowerCase() === 'alexa.powercontroller') {
let power_state_value = "OFF";
if (event.directive.header.name === "TurnOn")
power_state_value = "ON";
let endpoint_id = event.directive.endpoint.endpointId;
let token = event.directive.endpoint.scope.token;
let correlationToken = event.directive.header.correlationToken;
let ar = new AlexaResponse(
{
"correlationToken": correlationToken,
"token": token,
"endpointId": endpoint_id
}
);
ar.addContextProperty({"namespace":"Alexa.PowerController", "name": "powerState", "value": power_state_value});
// Check for an error when setting the state
let state_set = sendDeviceState(endpoint_id, "powerState", power_state_value);
if (!state_set) {
return new AlexaResponse(
{
"name": "ErrorResponse",
"payload": {
"type": "ENDPOINT_UNREACHABLE",
"message": "Unable to reach endpoint database."
}
}).get();
}
return sendResponse(ar.get());
}
};
function sendResponse(response)
{
// TODO Validate the response
console.log("index.handler response -----");
console.log(JSON.stringify(response));
return response
}
function sendDeviceState(endpoint_id, state, value) {
let dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
let key = state + "Value";
let attribute_obj = {};
attribute_obj[key] = {"Action": "PUT", "Value": {"S": value}};
let request = dynamodb.updateItem(
{
TableName: "SampleSmartHome",
Key: {"ItemId": {"S": endpoint_id}},
AttributeUpdates: attribute_obj,
ReturnValues: "UPDATED_NEW"
});
console.log("index.sendDeviceState request -----");
console.log(request);
let response = request.send();
console.log("index.sendDeviceState response -----");
console.log(response);
return true;
}