Created
November 17, 2017 16:40
-
-
Save r3-yamauchi/fa75ecf92d2f406fc54da5c51da4c2bb to your computer and use it in GitHub Desktop.
kintone のデータを使って Amazon Echo を朝礼当番にする https://blog.r3it.com/amazon-echo-with-kintone-38ebc81ce42f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var Alexa = require('alexa-sdk'); | |
var https = require('https'); | |
var querystring = require('querystring'); | |
//========================================================================================================================================= | |
//TODO: このコメント行より下の項目に注目してください。 | |
//========================================================================================================================================= | |
//Replace with your app ID (OPTIONAL). You can find this value at the top of your skill's page on http://developer.amazon.com. | |
//Make sure to enclose your value in quotes, like this: var APP_ID = "amzn1.ask.skill.bb4045e6-b3e8-4133-b650-72923c5980f1"; | |
var APP_ID = undefined; | |
var SKILL_NAME = "kintone skill"; | |
var GET_FACT_MESSAGE = ""; | |
var HELP_MESSAGE = "Please say kintone skill."; | |
var HELP_REPROMPT = "Please."; | |
var STOP_MESSAGE = "Good bye."; | |
//========================================================================================================================================= | |
//「TODO: ここから下のデータを自分用にカスタマイズしてください。」 | |
//========================================================================================================================================= | |
var data = []; | |
//========================================================================================================================================= | |
//この行から下のコードに変更を加えると、スキルが動作しなくなるかもしれません。わかる人のみ変更を加えてください。 | |
//========================================================================================================================================= | |
exports.handler = function (event, context, callback) { | |
const getOptions = (apiPath, method) => { | |
return { | |
hostname: process.env.DOMAIN, | |
port: 443, | |
path: apiPath, | |
method: method, | |
headers: { | |
'X-Cybozu-API-Token': process.env.API_TOKEN | |
} | |
}; | |
}; | |
const params = { | |
app: process.env.APP_ID, | |
query: `TargetDate = TODAY() limit 1` | |
}; | |
const query = querystring.stringify(params); | |
const options = getOptions('/k/v1/records.json?' + query, 'GET'); | |
const req = https.request(options, res => { | |
let body = ''; | |
res.setEncoding('utf8'); | |
res.on('data', chunk => { | |
body += chunk; | |
if (res.statusCode === 200) { | |
// callback(null, JSON.parse(chunk).records); | |
} else { | |
// callback(res.statusMessage); | |
} | |
}); | |
res.on('end', res => { | |
res = JSON.parse(body); | |
res.records.forEach(item => { | |
data.push(item.content.value); | |
}); | |
// console.log(JSON.stringify(data)); | |
var alexa = Alexa.handler(event, context); | |
alexa.APP_ID = APP_ID; | |
alexa.registerHandlers(handlers); | |
alexa.execute(); | |
}); | |
}); | |
req.on('error', err => { | |
callback(err.message); | |
}); | |
req.end(); | |
}; | |
var handlers = { | |
'LaunchRequest': function () { | |
this.emit('GetNewFactIntent'); | |
}, | |
'GetNewFactIntent': function () { | |
var factArr = data; | |
var factIndex = Math.floor(Math.random() * factArr.length); | |
var randomFact = factArr[factIndex]; | |
var speechOutput = GET_FACT_MESSAGE + randomFact; | |
this.emit(':tellWithCard', speechOutput, SKILL_NAME, randomFact) | |
}, | |
'AMAZON.HelpIntent': function () { | |
var speechOutput = HELP_MESSAGE; | |
var reprompt = HELP_REPROMPT; | |
this.emit(':ask', speechOutput, reprompt); | |
}, | |
'AMAZON.CancelIntent': function () { | |
this.emit(':tell', STOP_MESSAGE); | |
}, | |
'AMAZON.StopIntent': function () { | |
this.emit(':tell', STOP_MESSAGE); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment