Search code examples
dialogflow-es

How make Dialogflow generate a random number?


I am trying to create a bank chatbot. For creating a new bank account for the user I want it to generate a random number and make it display to the user. How to do it?


Solution

  • I think there are two possibility to do that. Using inline editor(cloud functions) or seperate webhooks. But as you don't need to store the account number in the session parameters then I think cloud functions will help you the most here.

    Here I have create a Account intent and mapped the generateRandom function with that intent.

    package.json

    {
       "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": "Google Inc.",
       "engines": {
            "node": "10"
        },
        "scripts": {
             "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
             "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
         },
        "dependencies": {
              "actions-on-google": "^2.2.0",
              "firebase-admin": "^5.13.1",
              "firebase-functions": "^2.0.2",
              "dialogflow": "^0.6.0",
              "dialogflow-fulfillment": "^0.5.0",
              "uuid-int": "3.1.0"
            }
     }
    

    index.js

      'use strict';
    
       const functions = require('firebase-functions');
       const {WebhookClient} = require('dialogflow-fulfillment');
       const {Card, Suggestion} = require('dialogflow-fulfillment');
       const UUID = require('uuid-int');
    
       process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
    
       exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
       const agent = new WebhookClient({ request, response });
    
       function generateRandom() {
          const accountNumber =String(UUID(10).uuid()).slice(-10);
          agent.add(`Your account number is ` +accountNumber);
          console.log(accountNumber);
          // So here we haven't stored the account number in the parameter so we can use it for the same intent only. 
        }
      let intentMap = new Map();
      intentMap.set('Account', generateRandom);
      agent.handleRequest(intentMap);
     });
    

    Let me know if you face any issue.

    Thanks