Last active
December 22, 2017 09:43
-
-
Save masartz/e60edac99e937970801fe89aa328dd33 to your computer and use it in GitHub Desktop.
TrashDialogFlow
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'; | |
const functions = require('firebase-functions'); | |
const dayNames = [ | |
'日曜日', | |
'月曜日', | |
'火曜日', | |
'水曜日', | |
'木曜日', | |
'金曜日', | |
'土曜日' | |
]; | |
const dayMap = { | |
'0' : 'なにもない', | |
'1' : '燃えるゴミ', | |
'2' : 'なにもない', | |
'3' : '缶・ビン・ダンボール', | |
'4' : '燃えるゴミ', | |
'5' : 'なにもない', | |
'6' : '', // 土曜日は隔週 | |
}; | |
const SaturdayMap = { | |
'1' : '燃えないゴミ', | |
'2' : 'ペットボトル', | |
'3' : '燃えないゴミ', | |
'4' : 'ペットボトル', | |
'5' : 'なにもない', | |
}; | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
const JST = 60000 * 60 * 9; | |
var dateObj = new Date((new Date()).getTime() + JST ); | |
var dayNumber = dateObj.getDay(); | |
var todayName = ''; | |
var tomorrowName = ''; | |
var todayTrash = ''; | |
var tomorrowTrash = ''; | |
var calcWeekCountInTargetYearAndMonthByDate = Math.floor((dateObj.getDate() - dateObj.getDay() + 12) / 7); | |
// Friday | |
if(dayNumber == 5){ | |
todayName = dayNames[dayNumber]; | |
tomorrowName = '第' + calcWeekCountInTargetYearAndMonthByDate + dayNames[dayNumber + 1]; | |
todayTrash = dayMap[dayNumber]; | |
tomorrowTrash = SaturdayMap[calcWeekCountInTargetYearAndMonthByDate]; | |
} | |
else if(dayNumber == 6){ | |
todayName = '第' + calcWeekCountInTargetYearAndMonthByDate + dayNames[dayNumber]; | |
tomorrowName = dayNames[0]; | |
todayTrash = SaturdayMap[calcWeekCountInTargetYearAndMonthByDate]; | |
tomorrowTrash = dayMap[0]; | |
} | |
else{ | |
todayName = dayNames[dayNumber]; | |
tomorrowName = dayNames[dayNumber + 1]; | |
todayTrash = dayMap[dayNumber]; | |
tomorrowTrash = dayMap[dayNumber + 1]; | |
} | |
var result = `今日${todayName}は${todayTrash}です。明日${tomorrowName}は${tomorrowTrash}です。`; | |
response.setHeader('Content-Type', 'application/json'); | |
response.status(200).send(JSON.stringify({ "speech": result, "displayText": result })); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment