Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webhooks for slot filling

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.

like image 305
sid8491 Avatar asked Mar 31 '18 15:03

sid8491


People also ask

How does slot filling work?

Slot filling gives the user a second chance to get their input right. If user makes a simple typo, for example, slot filling will re-prompt them for the input value. So it gives the user a chance to recover from input error. In theory, the bot can “keep asking till the user gets it right”.

What are events in Dialogflow?

When Dialogflow receives a webhook response that includes an event, it immediately triggers the corresponding intent in which it was defined. The following flow describes the steps: The end-user types or speaks an expression. Dialogflow matches the end-user expression to Intent-1, which is configured for fulfillment.


1 Answers

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

like image 72
Marcos Casagrande Avatar answered Sep 28 '22 15:09

Marcos Casagrande