Skip to content

Instantly share code, notes, and snippets.

@savelee
Last active August 18, 2022 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save savelee/bbd4ff272557ca080bda82d6a6cbd0c1 to your computer and use it in GitHub Desktop.
Save savelee/bbd4ff272557ca080bda82d6a6cbd0c1 to your computer and use it in GitHub Desktop.
Dialogflow Fulfillment Library
'use strict';
const functions = require('firebase-functions');
const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment');
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 yourFunctionHandler(agent) {
agent.add(`Ok. Buying product:`);
console.log(agent.parameters);
agent.add(new Card({
title: agent.parameters.producttype,
imageUrl: 'https://dummyimage.com/300x200/000/fff',
text: `This is the body text of a card. You can even use line\n breaks and emoji! 💁`,
buttonText: 'This is a button',
buttonUrl: 'https://console.dialogflow.com/'
})
);
agent.add(new Suggestion(`Quick Reply`));
agent.add(new Suggestion(`Suggestion`));
agent.context.set({ name: 'gamestore-picked', lifespan: 2, parameters: { gameStore: 'DialogflowGameStore' }});
}
// 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('Buy product regex', yourFunctionHandler);
agent.handleRequest(intentMap);
});
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Lee Boonstra",
"engines": {
"node": "8"
},
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"dependencies": {
"actions-on-google": "^2.12.0",
"firebase-admin": "^5.13.1",
"firebase-functions": "^2.0.2",
"dialogflow-fulfillment": "^0.6.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment