Skip to content

Instantly share code, notes, and snippets.

@fnishio
Created October 13, 2018 01:58
Show Gist options
  • Select an option

  • Save fnishio/d036fb067d86fbb703d20425d884c030 to your computer and use it in GitHub Desktop.

Select an option

Save fnishio/d036fb067d86fbb703d20425d884c030 to your computer and use it in GitHub Desktop.
Firebase Cloud Functions
'use strict';
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const {WebhookClient} = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
admin.initializeApp()
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function welcome(agent) {
agent.add(`Welcome to my agent!`);
}
function fallback(agent) {
agent.add(`I didn't understand`);
agent.add(`I'm sorry, can you try again?`);
}
const lightPath = "/room-light"
const triggerPath = lightPath + "/trigger"
const operationPath = lightPath + "/op"
function turnOnLight(agent) {
var date = new Date();
admin.database().ref(lightPath).set({
trigger: date.getTime(),
op: 'on'
});
agent.add('明かりをつけました')
}
function turnOffLight(agent) {
var date = new Date();
admin.database().ref(lightPath).set({
trigger: date.getTime(),
op: 'off'
});
agent.add('明かりを消しました')
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Welcome Intent', welcome);
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('TurnOnRoomLight', turnOnLight);
intentMap.set('TurnOffRoomLight', turnOffLight);
agent.handleRequest(intentMap);
});
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.0.3",
"actions-on-google": "^2.1.3",
"dialogflow-fulfillment": "^0.5.0"
},
"private": true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment