exports.handler=function(request,context){ var client_ID=''; var client_secret=''; var sensor_num=10; if(request.directive.header.namespace=='Alexa.Discovery'&&request.directive.header.name=='Discover'){ var eps=[]; for(var i=0; i<sensor_num; i++){ eps.push({ "endpointId":"dummy_sensor_" + ('000' + i).slice(-3), "manufacturerName":"dummy", "friendlyName":"ダミーセンサー" + ('000' + i).slice(-3), "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}}, ] }); } request.directive.header.name = "Discover.Response"; context.succeed({event:{header:request.directive.header,payload:{"endpoints":eps}}}); }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; var options = { host:'api.amazon.com', path:'/auth/o2/token', method:'POST', headers:{ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' } }; (new Promise((resolve, reject) => { var req=require("https").request(options ,res =>{ var data=''; res.setEncoding='UTF-8'; res.on('data',function(d){ data+=d; }); res.on('end',function(){ resolve(data); }); console.log('statusCode:' + res.statusCode ); } ).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":{}}}); }); } };