6

I am having trouble with slot/parameter filling in dialogflow. I am not able to search any good documentation for how to use webhooks/backend-code for parameter filling.

My use case is, I want to extract date but if the user is not providing YEAR then it should ask the user "which year?". And then fill it back in date.

I am using $date.partial as value so it is giving UUUU for the year part, but how can I prompt the user to give the year so that I can fill it in the parameter to get complete date.

enter image description here

Any help is appreciated.

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
sid8491
  • 6,622
  • 6
  • 38
  • 64
  • Put the link of the screenshot how you are asking a date to a user ? – Nikhil Savaliya Apr 02 '18 at 04:04
  • simply by making an entity on dialogflow console and checking it as `required` @NikhilSavaliya – sid8491 Apr 02 '18 at 05:23
  • then add the question in as parameter in dialogFLow – Nikhil Savaliya Apr 02 '18 at 06:02
  • @NikhilSavaliya oh yes, i mostly work with Amazon Lex hence the mistake. corrected and added screenshot for better understanding. thanks. – sid8491 Apr 02 '18 at 06:49
  • set date as required and in prompt ask for a year if a year is not entered. – Nikhil Savaliya Apr 02 '18 at 07:30
  • @NikhilSavaliya if the user is not entering the year then it will consider year as `UUUU`, so the value of date will be something like `UUUU-03-01`. so the prompt will not work. however if i can handle it programatically (using webhooks for slot filling) then it can be solved. i have mentioned in the question details. – sid8491 Apr 02 '18 at 08:44
  • https://stackoverflow.com/questions/49363518/php-mysql-dialogflow/49364638#49364638 refer this for code and in dialogFlow as choose options for slot filling and it will work. – Nikhil Savaliya Apr 02 '18 at 09:16
  • 1
    Are you still trying to do this? I don't know python but can give you a nodejs example, and the Dialogflow console configuration to achieve what you're asking. – Marcos Casagrande Apr 05 '18 at 00:05
  • @MarcosCasagrande i have found a way to do that and will be adding the answer soon, still you can add the answer so that it would help many others like me. thanks! and language is not a constraint. :) – sid8491 Apr 05 '18 at 04:01
  • @sid8491 Done!, It's a little long, but it does work! I use it in my multiple agents. – Marcos Casagrande Apr 06 '18 at 00:02

2 Answers2

9

Sometimes setting a param as required is not enough, because you don't only need that param to be present, but you need it in a specific format, there's an alternative using events.

There are multiple steps, but once you're familiar with it, you'll do it very fast.

1) Create 2 new intents: Year - Confirmation & Year - Confirmed

2) Add an event in the first intent: Intent > Events > ask-year (or whatever name you like)

3) Add an output context: year-confirmation

4) Set a response asking the user to enter the year: Please provide the year...

5) Set parameter:

  • Name: date
  • Entity: -
  • Value: #ask-year.date (This will come from event data, you will send it from your backend)

enter image description here

6) On your second intent Year - Confirmed, add as input context: year-confirmation (the output from the previous intent)

7) Set the same action as your main intent: insurance

8) Add some training phrases where you can match the year:

  • 2017 (@sys.number:year)
  • The year is @sys.number:year (Use template mode for this one)

enter image description here

Now you will have $year as params.

9) Add one extra parameter:

  • Name: date
  • Entity: -
  • Value: #year-confirmation.date (This will come from year-confirmation context)

enter image description here

10) On your backend, when you receive the incompleted date you should send ask-year event.

Node.js example, I don't know python

const apiai = require('apiai');
const client = apiai('my-dev-token');

function sendEvent(data) {

    const request = client.eventRequest(data, {
        sessionId: 'current-session'
    });

    request.on('response', response => {
        // Push message to your UI
        console.log(response.result.fulfillment.speech); // Please provide the year...
    });

    request.on('error', error => {
        console.error('Event error: ', error);
    });

    request.end();

}

/* ... */

// Your insurance action handler
function insuranceHandler(result) {

    const { parameters } = result;

    if(parameters.date || parameters.date.includes('UUUU')) { // Or whatever check for invalid year

        // Send previous date as data, so you will have it in the event response
        return sendEvent({
            name: 'ask-year',
            data: {
                date: parameters.date
            }
        });

    }

    // Year comes from `ask-year` intent
    if(parameters.year)
        parameters.date = parameters.date.replace('UUUU', parameters.year);

    // Do whatever you need
}

Now when the date is incomplete, the event will be triggered, executing Year - Confirmation and you will be asked to provide the year. After you provide one, Year - Confirmed intent will be executed by your response. Now your backend will receive the insurance action with an additional parameter, year

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • Glad to help! Let's hope they provide a parameter validation option to avoid this work around :) – Marcos Casagrande Apr 06 '18 at 13:46
  • @MarcosCasagrande hey i have a question, what if there are similar multiple parameters and we want to handle it using intents instead of slots, then how would we track the values of these parameters until we reach the final intent? – sid8491 Sep 20 '18 at 06:41
  • @MarcosCasagrande Your solution seems like you are triggering a new intent to capture the year. What if I alrady have the year and want to set it properly in the context of the initial intent? Is it possible to do it programmatically with fulfillment? – agwntr Feb 20 '19 at 13:01
  • @agwntr you can send the year directly either as an event or using `detectIntent` and sending a context with the parameters. If I'm not mistaken fulfillment works only as webhook, I will need to know your exact case in order to help you. – Marcos Casagrande Feb 20 '19 at 21:37
-3

Check the box next to the parameter labeled Required and click Add prompt on the right of the parameter and add the prompt Which year?

mattcarrollcode
  • 3,429
  • 16
  • 16
  • 1
    that's not gonna help since the user will be entering a date, without the year, so the parameter value will be filled. – sid8491 Apr 02 '18 at 18:37