codependent
codependent

Reputation: 24442

How can you answer a client with his/her name in Dialogflow when it's provided by an external service?

I'm creating a Dialogflow agent in which the client identifies with a clientId. This uses Twilio for Whatsapp chatbot integration.

DIALOG

 - Hi, tell me your clientId
 - abcde1234

At this point I need to get the client name from an external service...

GET Authentication: Basic xxx:yyy http://xxx/clients/id/abcde1234

-> {"id":"abcde1234", "name": "John", ...}

... and answer with it:

DIALOG

 - Hi, John, how can I help you?

Is this possible with Dialogflow?

Upvotes: 1

Views: 104

Answers (1)

Anshuman Kumar
Anshuman Kumar

Reputation: 577

So in order to fetch the value of the user's input, we can create something called a session parameter. Basically, this will be a JSON object in the API request sent to your webhook API which will present throughout the lifespan of your conversation (due to the high lifetime set for the same). You can read more in depth about contexts here.

Snippet of Context variable called global-parameters

We can then set up a simple NodeJS codebase on a Cloud Function (used this only due to its simplicity of deployment, though you are free to use any cloud provider/platform of your choice). I made some minor modifications to the boiler plate codebase present in every Dialogflow ES agent. enter image description here

So for example, here's the changes made in the index.js file

.
.
.
  function welcome(agent) {
    const globalParameters = agent.getContext('global-parameters');
    const questionNumber = globalParameters.parameters.number;
    const sampleNameFromGetCall = 'John'
    agent.add(`Welcome to my agent! ${sampleNameFromGetCall}`);
  }

and here's the 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": "MIT",
  "author": "Google Inc.",
  "engines": {
    "node": "16"
  },
  "dependencies": {
    "actions-on-google": "^2.2.0",
    "dialogflow": "^1.2.0",
    "dialogflow-fulfillment": "^0.5.0",
    "firebase-admin": "^11.4.1",
    "firebase-functions": "^4.1.1"
  },
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  }
}

Here's the library we used which is the library built by Google for this purpose. https://github.com/googleapis/nodejs-dialogflow Once I enabled the webhook fulfillment on my agent, I quickly tested it and

enter image description here

There's a major caveat to using this as this repo has been archived by Google and doesn't receive any updates, so you may have to figure out how to parse the incoming request in your webhook API OR you can use this library with some major changes to its codebase. You would need to make sure the overall latency of your request isn't too much, as Dialogflow ES has a 10 second time out for all responses.

So, in a nutshell, yes, we can definitely fetch a value from your Dialogflow Agent, use it to a call an API, parse that response and use that as a part of our dynamic response. The value would be stored in a JSON object called context, which will be a part of any incoming request to your webhook API.

Upvotes: 1

Related Questions