Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing previous follow up intent parameters within dialogflow-fulfillment

I'm using the default dialogflow code provided in the inline editor, based on dialogflow-fulfillment ^0.5.0 to collate all the parameters given over several follow up intents. I have a setup where follow up intents ask questions, resulting in a final intent that has all questions asked.

Pulling data from previous intents inside the dialogflow console to include in a response would just be using i.e. #order-cream-followup.chocolate-type to get a parameter from a previous intent or $quantity to get a parameter from the current intent. But while agent.parameters['quantity'] works like $quantity, I can't find the way to do the equivalent of #order-cream-followup.chocolate-type within dialogflow-fulfillment

Apologies if this is an obvious answer, I'm getting lost in the various different documentation for dialigflow and action on google in general.

My code:(currently just logging to console before adding code to handle pushing out that data)

// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';

const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function placeOrder(agent) {
    console.log('placing order:');
    console.log(agent.context.get('order-cream-followup').parameters['choctype']);
    agent.add('Thanks ' + agent.parameters['name'] + ', please collect your order from the window.');
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('order - cream - marshmallow - check - yes - name - submit', placeOrder);
  agent.handleRequest(intentMap);
});
like image 752
solexious Avatar asked Oct 16 '22 11:10

solexious


1 Answers

To get a context that is still active (ie - its lifespanCount has not reached 0), you can use agent.context.get(). So your example would look something like

agent.context.get('order-cream-followup').params['chocolate-type']

(This was introduced in version 0.6.0 of the library.)

However... this requires that the context still be valid. If you are using Followup Intents (which can get messy), the lifespan is originally only set to 2, so they may have expired.

There are two things you should do:

  1. Don't use Followup Intents. While useful in some cases, they can narrow the response options too much and can make very stilted conversations.

  2. Use a Context that you control, with a large lifespan, to collect the results as part of a webhook. So after each Intent where you have collected new information, you store this in a Context named, for example "order" that has a lifespan that you reset to 99 after each time you update it.

like image 132
Prisoner Avatar answered Oct 21 '22 00:10

Prisoner