Skip to content

Instantly share code, notes, and snippets.

@PonDad
Created October 8, 2017 02:09
Show Gist options
  • Save PonDad/957774fa65c648a0abaf3bfee4b83e05 to your computer and use it in GitHub Desktop.
Save PonDad/957774fa65c648a0abaf3bfee4b83e05 to your computer and use it in GitHub Desktop.
Googleホームアプリ「宿題の時間」のGoogle Cloud Functions実行ファイル
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').ApiAiApp;
const exec = require('child_process').exec;
const kinako_adress = "https://xxxxxxxx.ap.ngrok.io/kinako_today"
const anko_adress = "https://xxxxxxxx.ap.ngrok.io/anko_today"
const kinako_adress_2 = "https://xxxxxxxx.ap.ngrok.io/kinako_month"
const anko_adress_2 = "https://xxxxxxxx.ap.ngrok.io/anko_month"
const kinako_NAME = 'kinako_name'
const anko_NAME = 'anko_name'
const DATE = 'date';
const DATE_PERIOD = 'date-period';
exports.homeWork = (request, response) => {
const app = new App({request, response});
console.log('Request headers: ' + JSON.stringify(request.headers));
console.log('Request body: ' + JSON.stringify(request.body));
function responsekinakoDate (app) {
let kinako_name = app.getArgument(kinako_NAME);
let work_date = app.getArgument(DATE);
// Complete your fulfillment logic and send a response
exec(`curl ${kinako_adress}?date=` + work_date, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if(`${stdout}` == 'none'){
app.ask(`<speak> <say-as interpret-as="date" format="yyyymmdd" detail="1">` + work_date + `</say-as>。<break time="1s"/><sub alias="きなこ">` + kinako_name +`</sub> ちゃんは宿題をまだやってないよ。</speak>`);
}else{
let array =[]
let good_job = ['すごいね!', 'やったね!', 'よくできました!'];
let good_try = ['がんばったね!', 'よくできたね!'];
let start_time = `${stdout}`.substring(0, 8);
let stop_time = `${stdout}`.substring(9, 17);
let status = `${stdout}`.substring(18, 23);
if(status == 'TRUE'){
status = good_job[Math.floor(Math.random() * good_job.length)]
}else{
status = good_try[Math.floor(Math.random() * good_try.length)]
}
array.push(start_time,stop_time,status)
app.ask(`<speak><say-as interpret-as="date" format="YYYYmmdd" detail="1">` + work_date + `</say-as>。<break time="1s"/><sub alias="きなこ">`+ kinako_name +`</sub> ちゃんは宿題をやったよ。 始めたのは <say-as interpret-as="time" format="hms24">` + array[0] + `</say-as>だよ。 <break time="1s"/> 終わったのは <say-as interpret-as="time" format="hms24">` + array[1] + `</say-as>だよ。 <break time="1s" />` + array[2] + `</speak>`);
}
});
}
function responseankoDate (app) {
let anko_name = app.getArgument(anko_NAME);
let work_date = app.getArgument(DATE);
exec(`curl ${anko_adress}?date=` + work_date, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
if(`${stdout}` == 'none'){
app.ask(`<speak> <say-as interpret-as="date" format="yyyymmdd" detail="1">` + work_date + `</say-as>。<break time="1s"/>`+ anko_name +` ちゃんは宿題をまだやってないよ。 </speak>`);
}else{
let array =[]
let good_job = ['すごいね!', 'やったね!', 'よくできました!'];
let good_try = ['がんばったね!', 'よくできたね!'];
let start_time = `${stdout}`.substring(0, 8);
let stop_time = `${stdout}`.substring(9, 17);
let status = `${stdout}`.substring(18, 23);
if(status == 'TRUE'){
status = good_job[Math.floor(Math.random() * good_job.length)]
}else{
status = good_try[Math.floor(Math.random() * good_try.length)]
}
array.push(start_time,stop_time,status)
app.ask(`<speak><say-as interpret-as="date" format="YYYYmmdd" detail="1">` + work_date + `</say-as>。<break time="1s"/> `+ anko_name +` ちゃんは宿題をやったよ。 始めたのは <say-as interpret-as="time" format="hms24">` + array[0] + `</say-as>だよ。 <break time="1s"/> 終わったのは <say-as interpret-as="time" format="hms24">` + array[1] + `</say-as>だよ。 <break time="1s" />` + array[2] + `</speak>`);
}
});
}
function responsekinakoPeriod (app) {
let kinako_name = app.getArgument(kinako_NAME);
let work_period = app.getArgument(DATE_PERIOD);
// Complete your fulfillment logic and send a response
exec(`curl ${kinako_adress_2}?date-period=` + work_period, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}else{
let array =[]
let great_job = ['すごいね!', 'やったね!', 'よくできました!'];
let good_try = ['がんばったね!', 'よくできたね!'];
let true_cnt = Number(`${stdout}`.substring(0, 2));
let false_cnt = Number(`${stdout}`.substring(3, 5));
let start_day = work_period.substring(0, 10);
let end_day = work_period.substring(11, 21);
let status = ''
if(false_cnt == 0){
status = great_job[Math.floor(Math.random() * great_job.length)]
}else{
status = good_try[Math.floor(Math.random() * good_try.length)]
}
array.push(true_cnt,false_cnt,status)
app.ask(`<speak><sub alias="きなこ">`+ kinako_name +`</sub> ちゃんは宿題をやったよ。 10分以内は<say-as interpret-as="cardinal">` + array[0] + `</say-as>回だよ。 10分以上は<say-as interpret-as="cardinal">` + array[1] + `</say-as>回だよ。<say-as interpret-as="date" format="YYYYmmdd" detail="1">`+ start_day +`</say-as> から <say-as interpret-as="date" format="YYYYmmdd" detail="1">`+ end_day +`</say-as>の間にやったよ。 <break time="1s" />` + array[2] + `</speak>`);
}
});
}
function responseankoPeriod (app) {
let anko_name = app.getArgument(anko_NAME);
let work_period = app.getArgument(DATE_PERIOD);
// Complete your fulfillment logic and send a response
exec(`curl ${anko_adress_2}?date-period=` + work_period, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}else{
let array =[]
let great_job = ['すごいね!', 'やったね!', 'よくできました!'];
let good_try = ['がんばったね!', 'よくできたね!'];
let true_cnt = Number(`${stdout}`.substring(0, 2));
let false_cnt = Number(`${stdout}`.substring(3, 5));
let start_day = work_period.substring(0, 10);
let end_day = work_period.substring(11, 21);
let status = ''
if(false_cnt == 0){
status = great_job[Math.floor(Math.random() * great_job.length)]
}else{
status = good_try[Math.floor(Math.random() * good_try.length)]
}
array.push(true_cnt,false_cnt,status)
app.ask(`<speak>`+ anko_name +` ちゃんは宿題をやったよ。 10分以内は<say-as interpret-as="cardinal">` + array[0] + `</say-as>回だよ。10分以上は<say-as interpret-as="cardinal">` + array[1] + `</say-as>回だよ。 <say-as interpret-as="date" format="YYYYmmdd" detail="1">`+ start_day +`</say-as> から <say-as interpret-as="date" format="YYYYmmdd" detail="1">`+ end_day +`</say-as>の間にやったよ。 <break time="1s" />` + array[2] + `</speak>`);
}
});
}
const actionMap = new Map();
actionMap.set('input.kinako_date', responsekinakoDate);
actionMap.set('input.anko_date', responseankoDate);
actionMap.set('input.kinako_period', responsekinakoPeriod);
actionMap.set('input.anko_period', responseankoPeriod);
app.handleRequest(actionMap);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment