Skip to content

Instantly share code, notes, and snippets.

@savelee
Last active January 31, 2022 20:54
Show Gist options
  • Save savelee/46872e77e0ab0e71a7a8d7ffd6c178d2 to your computer and use it in GitHub Desktop.
Save savelee/46872e77e0ab0e71a7a8d7ffd6c178d2 to your computer and use it in GitHub Desktop.
Dialogflow Fulfillment in Cloud Run
Dockerfile
README.md
node_modules
npm-debug.log
# Use the official lightweight Node.js 12 image.
# https://hub.docker.com/_/node
FROM node:12-slim
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
# Copying this separately prevents re-running npm install on every code change.
COPY package*.json ./
# Install production dependencies.
RUN npm install --only=production
# Copy local code to the container image.
COPY . ./
# Run the web service on container startup.
CMD [ "npm", "start" ]
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const basicAuth = require('express-basic-auth');
const { WebhookClient, Card, Suggestion } = require('dialogflow-fulfillment');
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
// Dialogflow Fulfillment Code
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' }});
}
// Express Code
const app = express().use(bodyParser.json());
// Basic Authentication
app.use(basicAuth({
users: { 'admin': 'supersecret' }
}));
app.use(function(req, res, next) {
if (!req.headers['x-auth']) {
return res.status(403).json({ error: 'No auth headers sent!' });
}
next();
});
app.post('/fulfillment', (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));
// 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);
});
app.get('/', (req, res) => {
res.send(`OK`);
});
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log('Dialogflow Fulfillment listening on port', port);
});
{
"name": "dialogflow-fulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Run",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Lee Boonstra",
"engines": {
"node": "8"
},
"scripts": {
"start": "node index.js",
"build": "gcloud builds submit --tag gcr.io/dialogflowcookbook/dialogflow",
"deploy": "gcloud run deploy --image gcr.io/dialogflowcookbook/dialogflow --platform managed"
},
"dependencies": {
"@google-cloud/dialogflow": "^2.0.0",
"actions-on-google": "^2.5.0",
"body-parser": "^1.19.0",
"dialogflow-fulfillment": "^0.6.1",
"express": "^4.17.1",
"express-basic-auth": "^1.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment