exports.handler=function(request,context){ var client_ID=''; var client_secret=''; var sensors={ 'sensor_001':'スイッチ01', 'sensor_002':'スイッチ02', }; var buttons={ 'button_001':{"name":"テスト1","URL":"http://example.com/XXXXXXXXXXXXXXXXXXX"}, 'button_002':{"name":"テスト2","URL":"http://example.com/XXXXXXXXXXXXXXXXXXX"}, }; if(request.directive.header.namespace=='Alexa.Discovery'&&request.directive.header.name=='Discover'){ var eps=[]; for(var n in sensors){ eps.push({ "endpointId":n, "manufacturerName":"dummy", "friendlyName":sensors[n], "description":"dummy sensor", "displayCategories": ["MOTION_SENSOR"], "cookie":{}, "capabilities":[ {"type": "AlexaInterface","interface": "Alexa","version": "3"}, {"type": "AlexaInterface","interface": "Alexa.MotionSensor","version": "3","properties":{"supported":[{"name":"detectionState"}],"proactivelyReported": true,"retrievable": false}}, {"type": "AlexaInterface","interface": "Alexa.EndpointHealth","version": "3","properties":{"supported":[{"name":"connectivity"}],"proactivelyReported": true,"retrievable": false}}, ] }); } for(var n in buttons){ eps.push({ "endpointId":n, "manufacturerName":"dummy", "friendlyName":buttons[n].name, "description":"GET "+buttons[n].URL, "displayCategories": ["LIGHT"], "cookie":{}, "capabilities":[ {"type": "AlexaInterface","interface": "Alexa","version": "3"}, {"type": "AlexaInterface","interface": "Alexa.PowerController","version": "3","properties":{"supported":[]}}, ] }); } request.directive.header.name = "Discover.Response"; context.succeed({event:{header:request.directive.header,payload:{"endpoints":eps}}}); }else if(request.directive.header.namespace=='Alexa.PowerController'&&request.directive.header.name=='TurnOn'){ var url=buttons[request.directive.endpoint.endpointId].URL; (new Promise((resolve, reject) => { require(url.match(/^https:/)?"https":"http").get(url, res => resolve()).on('error', error=>reject(error)); })).then(()=>{ request.directive.header.name="Response"; request.directive.context={"properties":[]}; context.succeed({"event":request.directive}); }); }else if( (request.directive.header.namespace=='Alexa'&&request.directive.header.name=='ReportState') ||(request.directive.header.namespace=='Alexa.MotionSensor'&&request.directive.header.name=='detectionState') ||(request.directive.header.namespace=='Alexa.EndpointHealth') ){ request.directive.header.name="StateReport"; request.directive.context={"properties":[]}; context.succeed({"event":request.directive}); }else if(request.directive.header.namespace=='Alexa.Authorization'&&request.directive.header.name=='AcceptGrant'){ var post='grant_type=authorization_code&code='+request.directive.payload.grant.code+'&client_id='+client_ID+'&client_secret='+client_secret; (new Promise((resolve, reject) => { var req=require("https").request({ host:'api.amazon.com', path:'/auth/o2/token', method:'POST', headers:{ 'Content-Type': 'application/x-www-form-urlencoded;Accept-Charset=UTF-8', 'Content-Length': post.length } },res =>{ var data=''; res.setEncoding='UTF-8'; res.on('data',function(d){ data+=d; }); res.on('end',function(){ resolve(data); }); }).on('error', error=>reject(error)); req.write(post); req.end(); })).then((json)=>{ var obj=JSON.parse(json); console.log('AcceptGrant:token='+obj.refresh_token); request.directive.header.name="AcceptGrant.Response"; context.succeed({"event":{"header":request.directive.header,"payload":{}}}); }); } };