manniru
10/15/2018 - 6:30 AM

Simplest Express Dialogflow Fulfillment Webhook

Simplest Express Dialogflow Fulfillment Webhook

'use strict';

const {WebhookClient} = require('dialogflow-fulfillment');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));



function welcome (agent) {
    agent.add(`Welcome to Express.JS webhook!`);
}

function fallback (agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}

function WebhookProcessing(req, res) {
    const agent = new WebhookClient({request: req, response: res});
    console.info(`agent set`);

    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('<INTENT_NAME_HERE>', yourFunctionHandler);
    agent.handleRequest(intentMap);
}


// Webhook
app.post('/', function (req, res) {
    console.info(`\n\n>>>>>>> S E R V E R   H I T <<<<<<<`);
    WebhookProcessing(req, res);
});

app.listen(8080, function () {
    console.info(`Webhook listening on port 8080!`)
});
{
  "name": "Mannir Agent",
  "version": "0.0.1",
  "description": "Mannir Agent webhook",
  "main": "server.js",
  "author": "Muhammad Mannir Ahmad, Nigeria, Kano",
  "dependencies": {
    "dialogflow-fulfillment": "^0.4.1",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "actions-on-google": "^2.2.0"
  }