如何让 Dialogflow 生成随机数?

How make Dialogflow generate a random number?

我正在尝试创建一个银行聊天机器人。为了为用户创建一个新的银行账户,我希望它生成一个随机数并将其显示给用户。怎么做?

我认为有两种可能性。使用内联编辑器(云函数)或单独的 webhooks。但是由于您不需要在会话参数中存储帐号,因此我认为云函数在这里对您的帮助最大。

我在这里创建了一个帐户意图,并用该意图映射了 generateRandom 函数。

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);
 });

如果您遇到任何问题,请告诉我。

谢谢