Skip to content

Instantly share code, notes, and snippets.

@yasugahira0810
Created December 7, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasugahira0810/598d8c1cfa2242c9a4e504b7d6e21b59 to your computer and use it in GitHub Desktop.
Save yasugahira0810/598d8c1cfa2242c9a4e504b7d6e21b59 to your computer and use it in GitHub Desktop.
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const request = require('request');
const get = (url) => new Promise((resolve, reject) => {
request.get({
url: url,
json: true
}, (error, res, json) => {
if (error) {
return reject(error);
}
resolve(json);
});
});
const post = (url) => new Promise((resolve, reject) => {
request.post({
url: url,
json: true
}, (error, res, json) => {
if (error) {
return reject(error);
}
resolve(json);
});
});
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
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?`);
}
function lightOn(agent) {
agent.add(`つけました`);
const url = '<RunKitの点灯用エンドポイントのURL>';
return get(url).then(responsedJson => {})
}
function lightOff(agent) {
agent.add(`消しました`);
const url = 'RunKitの消灯用エンドポイントのURL';
return get(url).then(responsedJson => {})
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Light On Intent', lightOn);
intentMap.set('Light Off Intent', lightOff);
agent.handleRequest(intentMap);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment