This repository demonstrates how to integrate your Dialogflow agent with 3rd-party services services using a Node.JS backend service

Overview

DialogFlow Machine Learning Chatbot

chatbot

Project Overview

This repository contains a backend service for an intelligent chatbot that onboards clients, answers questions, and sets up appointments on a retail website. The purpose of the backend service is to improve the customer experience that users have when interacting with a Dialogflow chatbot.

Prerequisites

Google Cloud Platform Setup

NodeJS Set Up

Node.js is an open-source, server-side runtime environment that allows you to create backend applications by writing JavaScript code. This repository uses Node to connect to 3rd party services such as Google Maps API, Google Sheets, Google Calendar, and Twilio in order to provide our Dialogflow agent the ability to schedule appointments on a corporate webiste for one of our clients.

Set up an Express Server using Node.JS

const express = require('express')
const app = express()

Navigate to root directory and install the following packages:

$ npm install express

Run Backend Application on Local Host:

$ node service.js

Set up a Public URL with NGROK Tunnel:

$ ngrok http 

Copy forwarding link from ngrok

Add '/webhook' to the end of your public endpoint

    app.post('/webhook', async(request, response) => {

Enter Webhook URL in Dialogflow Agent Console:

Fulfillment-Service

  • Navigate to https://dialogflow.cloud.google.com
  • On the left side of the page, click the 'Fulfillment' tab
  • In the 'Webhook' section, click 'Enable'
  • Enter your public endpoint in the 'URL' field
  • Click 'Save' at the bottom of the page
    • You can define when you want to send webhooks to this public endpoint in the DialogFlow console

Create a Dialogflow Client

    const dialogflow = require('dialogflow');
    const sessionClient = new dialogflow.SessionsClient({
      projectId,
      credentials,
    });

Project ID

Define Project ID and Session ID in Application

  • You can find your project ID in your Dialogflow Agent Settings
    const projectId = '';
    const sessionId = '';

Session Management - Redis

Redis Server Installation

https://docs.redis.com/latest/rs/references/client_references/client_nodejs/

  • To use Redis with Node.js, you need to install a Node.js Redis client. The following sections explain how to use node_redis, a community-recommended Redis client for Node.js.

  • Another community-recommended client for Node.js developers is ioredis. You can find additional Node.js clients for Redis in the Node.js section of the Redis Clients page.

Install node_redis

$ npm install node_redis

Connect to Redis Instance

  • Run the following code in your service.js backend file to create a Redis Instance:
const redis = require('redis');

Create a new Redis Client

const client = redis.createClient({
    socket: {
        host: '',
        port: <port>
    },
    password: ''
});

client.on('error', err => {
    console.log('Error ' + err);
});

https://www.sitepoint.com/using-redis-node-js/

Install Redis Server:

  • For Mac and Linux users, the Redis installation is pretty straightforward. Open your terminal and type the following commands:
$ wget https://download.redis.io/releases/redis-6.2.4.tar.gz
$ tar xzf redis-6.2.4.tar.gz
$ cd redis-6.2.4
$ make

After the installation ends, start the server with this command:

$ redis-server

If the previous command did not work, try the following command:

$ src/redis-server 

Service Account Authentication

First you have to create a service account and download a .JSON format file of credentials on your local system. Now, there are three ways to use that credentials for authentication/authorisation in dialogflow library.

Method 1

  • Create an environment variable named 'GOOGLE_APPLICATION_CREDENTIALS'
  • Set the value equal to the absolute path of that JSON credentials file.
"">
$ export GOOGLE_APPLICATION_CREDENTIALS=""
  • Then, run your code. Google library will automatically load your credentials file

Method 2

  • Assume that you know the absolute path of your JSON file and put that as value in below snippet of credentials_file_path variable.
  • You can find your project ID in your Dialogflow agent settings
    const projectId = '';
    const sessionId = ''; 
    const credentials_file_path = '';

    // Instantiate a DialogFlow client.
    const dialogflow = require('dialogflow');

    const sessionClient = new dialogflow.SessionsClient({
      projectId,
      keyFilename: credentials_file_path,
    });

Customer Onboarding

  • The body of the request contains relevant information such as the user query, action, and parameters from the client-side.
    
    const query = request.body.queryResult    
    const intent = request.body.queryResult.intent.displayName;
    const score = request.body.queryResult.intentDetectionConfidence;
    const fulfillmentText = request.body.queryResult.fulfillmentText;
    const session_path = request.body.session;
    const parameters = request.body.queryResult.parameters
    const action = request.body.queryResult.action;
       

Extracting the Sesssion ID from Request:

    const session_path = request.body.session;
    const session_array = session_path.split('/')
    const session_id = session_array[session_array.length -1]

Save the First and Last Name of Customer in Session Cache

    var FirstName = await parameters["first-name"]
    var LastName = await parameters["last-name"]
    try {
      const client = new Redis()
      const hashMap = {
          "FirstName": FirstName,
          "LastName": LastName
      }
      await client.hmset(session_id, hashMap)
      const sessData = await client.hgetall(session_id)
    } catch(err) {
  }

Use Session Cache to Get Information about Customer

  • Use Redis client to get information about the customer from the session cache, such as their first and last name, in order to personalize the interaction between the chatbot and the user.
 
    if (action == "input.welcome") {
        const client = new Redis()
        const sessData = await client.hgetall(session_id)
        if (sessData.FirstName && sessData.LastName) {           
                var agentResponse = {
                    "fulfillmentText": `Hey, what's up ${sessData.FirstName}! What could I help you with today?`
                }
                response.json(agentResponse)            
        }    
    }

Creating Appointments

Using Google Maps API:

  • This feature maps the distance between a customer pickup location and a corporate business address in order to check whether the pickup distance falls within the range of 100 miles
if (context_name == session_path + '/contexts/' + 'appointment-booking-create') {
  console.log("Calculating distance of the destination from driver location...")
  var API_KEY = process.env.DIRECTIONS_API_KEY;
  var axios = require('axios')
  var config = {
  method: 'get',
  url: `https://maps.googleapis.com/maps/api/directions/json?origin=${Origin}&destination=${Destination}&key=${API_KEY}`
  }
  axios(config).then(async function(res) {
  data = res.data
  
  distance = data["routes"][0]["legs"][0]["distance"]["text"]
  kilometers = data["routes"][0]["legs"][0]["distance"]["value"]
  console.log(`The distance between origin and destination is: ${distance}`)
  
          if (kilometers < 160394) {
              console.log("The destination is within range!")
              var jsonResponse  = {
                  "fulfillmentText" : `Perfect! I just looked up the address, it's only ${distance} away, which meets the fundraiser requirements. How many bags do you plan on selling?`
              }
              console.log("Attempting to store DestinationAddress in memory...")
              redis_client.set("DestinationAddress", Destination, function(err, response) {
                  if(err) {
                      console.log(err)
                  }
                  
              })
              console.log("Saved destination address to session cache...")
              const hashMap = {
                  "DestinationAddress": Destination 
              }
              try {
                  const client = new Redis()
                  await client.hmset(session_id, hashMap)
                  const sessData = await client.hgetall(session_id) 
                  console.log(sessData)
              } catch(err) {
                  console.log(err)
              }
          
          }
          else {
              console.log("The destination is out of range")
          
              var jsonResponse = {
                  "fulfillmentText" : "I'm so sorry, I just looked up the address on the map. It looks like that address is out of driving range, we're only allowed to book appointments that are within 100 miles of the corporate office. Would you prefer to book the appointment at a different location?"
              }
              console.log("Sent response from server to customer...")
              
          }
          response.send(jsonResponse)
  
      }).catch(function(error) {
          console.log(error)
      })
  }

Validating a customer's phone number:

  • This feature uses regex to validate the phone number that a customer provided to the Dialogflow agent
  if (action == "appointment-booking-getphone") {
        const Phone = parameters["phone-number"]
        
        if (Phone.replace(/\D/g,'').length !== 10) {
            var jsonResponse = {
                "fulfillmentText": "Sorry, it looks like that phone number is invalid, could you tell me the full phone number, please?"
                }
                response.json(jsonResponse)
            
        } else {
            var jsonResponse = {
                "fulfillmentText": "Thanks! And the address of the location for the pick-up?"
                }
        try {         
            const client = new Redis()
            const hashData = { "Phone": Phone }
            await client.hmset(session_id, hashData)
            const sessData = await client.hgetall(session_id)
            redis_client.set("Phone", Phone, function(err, response) {
                if(err) {
                    console.log(err)
                }
            })
            } catch(err) {
                console.log("Error:", err)
            }      
        }
    }
You might also like...

Your emergency buddy and agent.

Your emergency buddy and agent.

S.H.I.E.L.D: To make you Beware of your surrounding The main aim of the S.H.I.E.L.D is to safeguard the individual and also make them aware about the

Oct 2, 2022

Calculating Pi number without limitation until 10k digits or more in your browser powered by JS without any third party library!

Calculating Pi number without limitation until 10k digits or more in your browser powered by JS without any third party library!

PI Calculator Web JS (Online) Calculating Pi number without limitation until 10k digits or more in your browser powered by JS without any third party

Jul 27, 2022

Minimal versions of popular analytics libraries. Reduce the impact of third-party scripts on your application.

minimal-analytics This project aims to provide minimal implementations of popular analytics libraries. It's aimed at users who want to reduce the impa

Dec 25, 2022

Quickly check your websites for third party requests.

Third Party Checker Tool for crawling websites and checking for third party requests using Puppeteer. Installation git clone https://github.com/pxlrbt

Nov 1, 2022

Calculates maximum composite SLA for a list of sequentially provided cloud services or your custom-defined services.

SlaMax Calculates maximum composite SLA for a list of sequentially provided cloud services or your custom-defined services. Here are a few use-cases y

Sep 19, 2022

Gatsby-Formik-contact-form-with-backend-panel - Full working contact form with backend GUI panel.

Gatsby minimal starter 🚀 Quick start Create a Gatsby site. Use the Gatsby CLI to create a new site, specifying the minimal starter. # create a new Ga

Jan 2, 2022

Venni backend - The backend of the Venni client apps implementing the credit card payments, matching algorithms, bank transfers, trip rating system, and more.

Cloud Functions Description This repository contains the cloud functions used in the Firebase backend of the Venni apps. Local Development Setup For t

Jan 3, 2022

This is a little script that shows how to ddos a website. Can bypass cloudfare & ddos-guard. Ip switcher and random user agent

This is a little script that shows how to ddos a website. Can bypass cloudfare & ddos-guard. Ip switcher and random user agent

Dec 17, 2022

A cloudflare worker to use the user-agent for ~~rickrolling~~ without a discord embed

nextcord.gay A cloudflare worker to use the user-agent for rickrolling without a discord embed Build npm run build Find the output in ./dist/worker.mj

Oct 4, 2022
Owner
ddayto
Software Engineer (AI/ML) & Founding Board Member at HBN, Inc
ddayto
DialogFlow Machine Learning Chatbot

DialogFlow Machine Learning Chatbot Project Overview This repository contains a backend service for an intelligent chatbot that onboards clients, answ

ddayto 10 Jul 21, 2022
A 3rd year University physics project for simulating satellites motion in orbit.

Satellite Simulator VI - Deluxe Edition A university physics project for simulating satellites in orbit. Installation instructions Clone the git repos

Rami Sabbagh 8 Jun 26, 2022
NPM Package to integrate ONDC into Node.js backend

ondc-node This library can be used to integrate ONDC in JavaScript based applications. Package is developed in TypeScript and will work with Node.Js &

Utkarsh Mehta 12 Dec 11, 2022
This project demonstrates single transaction and batch transaction use case

Batch Transaction Fullstack ( Localhost:8545 ) This project demonstrates single transaction and batch transaction use case. It comes with a transactio

Lemonde Shazai  39 Dec 27, 2022
A simple example repo that demonstrates the dynamic ephemeral storage solution for AWS Lambda outlined in the corresponding Storyboard Dev Blog post.

AWS Lambda Dynamic Ephemeral Storage Example A simple example repo that demonstrates the dynamic ephemeral storage solution for AWS Lambda outlined in

Storyboard.fm 3 Jun 14, 2022
Unofficial API client for the Tidbyt API. Use this client to control Tidbyt devices and integrate with other services.

Tidbyt Client for Node.js Unofficial API client for the Tidbyt API. Use this client to control Tidbyt devices and integrate with other services. Insta

Nicholas Penree 19 Dec 17, 2022
MSP Sentry Open Source repository. Integrate Sentinel One with ConnectWise (Or whatever you want)

MSPSentry-FOSS Integrate Sentinel One with ConnectWise Manage. Requirements Docker Node.js if you don't want to use a container (Not recommended) Some

Ted Williams 5 Sep 4, 2022
Next-level academia! Repository for the Native Overleaf project, attempting to integrate Overleaf with native OS features for macOS, Linux and Windows.

Native Overleaf Overleaf is a fantastic webtool for writing and cooperating on LaTeX documents. However, would it not be even better if it were to beh

Floris-Jan Willemsen 40 Dec 18, 2022
UAParser.js - Detect Browser, Engine, OS, CPU, and Device type/model from User-Agent data. Supports browser & node.js environment.

UAParser.js JavaScript library to detect Browser, Engine, OS, CPU, and Device type/model from User-Agent data with relatively small footprint (~17KB m

Faisal Salman 7.4k Jan 4, 2023
Your emergency buddy and agent.

S.H.I.E.L.D: To make you Beware of your surrounding The main aim of the S.H.I.E.L.D is to safeguard the individual and also make them aware about the

Prathik Shetty 6 Oct 2, 2022