5

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);
});
solexious
  • 151
  • 1
  • 7

1 Answers1

3

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.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Thanks. I've been trying to use context without success, it might be that it has expired by that point (`agent.context` isn't even defined when i try to call it). Sadly option 1 isn't possible due to client wishes. Will try option 2 :) – solexious Nov 05 '18 at 15:28
  • If `agent.context` isn't defined, there may be other issues. Can you update your question with the version of the library that you're using? – Prisoner Nov 05 '18 at 15:40
  • 1
    `agent.context` was introduced with version 0.6.0 of the library, and you say you're running 0.5.0. Since the older method has been deprecated, you should update. – Prisoner Nov 05 '18 at 16:49
  • ah thank you very much, I was using the default package.json provided by the inline editor. Will upgrade the version. – solexious Nov 05 '18 at 16:58
  • One more thing. As of v0.6.1, the context name is automatically converted to lowercase in Dialogflow's backend, so if you try it with the uppercase characters, your `agent.content.get()` might return undefined. Also, the `params` has been changed to `parameters`. – Amruth Pillai May 04 '19 at 04:56