Hello there, I need a little help with the JSON format of Discovery requests and responses.
The Alexa.Discovery request arriving on my webhook seems to be a little different from the docs (https://developer.amazon.com/it/docs/device-apis/alexa-discovery.html)
I have made the adjustments and ended up with this code:
exports.handler = function (request, context) { console.log("req:", request); console.log("ctx:", context); if (request.directive.header.namespace === 'Alexa.ConnectedHome.Discovery' && request.directive.header.name === 'DiscoverAppliancesRequest') { log("DEBUG:", "Discover request", JSON.stringify(request)); handleDiscovery(request, context, ""); } else if (request.directive.header.namespace === 'Alexa.PowerController') { if (request.directive.header.name === 'TurnOn' || request.directive.header.name === 'TurnOff') { log("DEBUG:", "TurnOn or TurnOff Request", JSON.stringify(request)); handlePowerControl(request, context); } } function handleDiscovery(request, context) { var payload = { "endpoints": [ { "endpointId": "demo_id", "manufacturerName": "Smart Device Company", "friendlyName": "Bedroom Outlet", "description": "Smart Device Switch", "displayCategories": ["SWITCH"], "cookie": { "key1": "arbitrary key/value pairs for skill to reference this endpoint.", "key2": "There can be multiple entries", "key3": "but they should only be used for reference purposes.", "key4": "This is not a suitable place to maintain current endpoint state." }, "capabilities": [ { "type": "AlexaInterface", "interface": "Alexa", "version": "3" }, { "interface": "Alexa.PowerController", "version": "3", "type": "AlexaInterface", "properties": { "supported": [{ "name": "powerState" }], "retrievable": true } } ] } ] }; var header = request.directive.header; header.name = "Discover.Response"; log("DEBUG", "Discovery Response: ", JSON.stringify({ header: header, payload: payload })); context.succeed({ event: { header: header, payload: payload } }); } function log(message, message1, message2) { console.log(message + message1 + message2); } function handlePowerControl(request, context) { // get device ID passed in during discovery var requestMethod = request.directive.header.name; var responseHeader = request.directive.header; responseHeader.namespace = "Alexa"; responseHeader.name = "Response"; responseHeader.messageId = responseHeader.messageId + "-R"; // get user token pass in request var requestToken = request.directive.endpoint.scope.token; var powerResult; if (requestMethod === "TurnOn") { // Make the call to your device cloud for control // powerResult = stubControlFunctionToYourCloud(endpointId, token, request); powerResult = "ON"; } else if (requestMethod === "TurnOff") { // Make the call to your device cloud for control and check for success // powerResult = stubControlFunctionToYourCloud(endpointId, token, request); powerResult = "OFF"; } var contextResult = { "properties": [{ "namespace": "Alexa.PowerController", "name": "powerState", "value": powerResult, "timeOfSample": "2017-09-03T16:20:50.52Z", //retrieve from result. "uncertaintyInMilliseconds": 50 }] }; var response = { context: contextResult, event: { header: responseHeader, endpoint: { scope: { type: "BearerToken", token: requestToken }, endpointId: "demo_id" }, payload: {} } }; log("DEBUG", "Alexa.PowerController ", JSON.stringify(response)); context.succeed(response); } };
That from the AWS lambda console generate this response:
{ "event": { "header": { "messageId": "F8752B11-69BB-4246-B923-3BFB27C06C7D", "name": "Discover.Response", "namespace": "Alexa.ConnectedHome.Discovery", "payloadVersion": "3" }, "payload": { "endpoints": [ { "endpointId": "demo_id", "manufacturerName": "Smart Device Company", "friendlyName": "Bedroom Outlet", "description": "Smart Device Switch", "displayCategories": [ "SWITCH" ], "cookie": { "key1": "arbitrary key/value pairs for skill to reference this endpoint.", "key2": "There can be multiple entries", "key3": "but they should only be used for reference purposes.", "key4": "This is not a suitable place to maintain current endpoint state." }, "capabilities": [ { "type": "AlexaInterface", "interface": "Alexa", "version": "3" }, { "interface": "Alexa.PowerController", "version": "3", "type": "AlexaInterface", "properties": { "supported": [ { "name": "powerState" } ], "retrievable": true } } ] } ] } } }
But when I test it from the Alexa developer console I got only the message that it is unable to find any device.
Can someone point me to updated documentation or shred some light on what I am doing wrong?