Skip to content

Instantly share code, notes, and snippets.

@hi1280
Created January 30, 2018 10:28
Show Gist options
  • Save hi1280/ed0638e6c3f2520c6898d597881c6db5 to your computer and use it in GitHub Desktop.
Save hi1280/ed0638e6c3f2520c6898d597881c6db5 to your computer and use it in GitHub Desktop.
AWS IoTを使うAlexaスキル
'use strict';
const Alexa = require('alexa-sdk');
const APP_ID = process.env.ALEXA_APP_ID;
const awsIot = require('aws-iot-device-sdk');
const device = awsIot.device({
keyPath: <YourPrivateKeyPath>,
certPath: <YourCertificatePath>,
caPath: <YourRootCACertificatePath>,
clientId: <YourUniqueClientIdentifier>,
host: <YourCustomEndpoint>
});
const handlers = {
'LaunchRequest': function() {
this.response.speak('家電を操作できるスキルです');
this.emit(':responseReady');
},
'TvOn': function() {
call('tv-on', 'テレビの電源を操作しました').then((result) => {
this.response.speak(result);
this.emit(':responseReady');
}).catch((err) => {
this.response.speak('失敗しました');
this.emit(':responseReady');
});
},
'LightOn': function() {
call('light-on', '照明をつけました').then((result) => {
this.response.speak(result);
this.emit(':responseReady');
}).catch((err) => {
this.response.speak('失敗しました');
this.emit(':responseReady');
});
},
'LightOff': function() {
call('light-off', '照明を消しました').then((result) => {
this.response.speak(result);
this.emit(':responseReady');
}).catch((err) => {
this.response.speak('失敗しました');
this.emit(':responseReady');
});
},
'Off': function() {
call('tv-on', 'テレビの電源を操作しました', 500)
.then((result) => {
return call('light-off', result + '、照明を消しました');
})
.then((result) => {
this.response.speak(result);
this.emit(':responseReady');
})
.catch((err) => {
this.response.speak('失敗しました');
this.emit(':responseReady');
});
},
'AMAZON.HelpIntent': function() {
const speechOutput = '家電を操作できるスキルです';
const reprompt = 'どうしますか?';
this.response.speak(speechOutput).listen(reprompt);
this.emit(':responseReady');
},
'AMAZON.CancelIntent': function() {
this.response.speak('終了します');
this.emit(':responseReady');
},
'AMAZON.StopIntent': function() {
this.response.speak('終了します');
this.emit(':responseReady');
},
};
module.exports.remocon = (event, context, callback) => {
device.on('connect', () => {
console.log('connect');
});
const alexa = Alexa.handler(event, context);
alexa.appId = APP_ID;
alexa.registerHandlers(handlers);
alexa.execute();
};
function call(cmd, str, interval=100) {
return new Promise((resolve, reject) => {
setTimeout(() => {
device.publish(cmd, '', {}, (err) => {
if (err) {
reject(err);
}
resolve(str);
});
}, interval);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment