-1

I've something that I don't succeed to understand.

Here the situation I would like to do :

Bot: Hello, what do you want to do ?

User: Search a product

Bot: Which product are you looking for ?

User: Apple

Bot -> list of products matched with apple

here is a fragment code :

  function searchProduct() {
    agent.add('Which product are you looking for ?');
    // receive the product answer 
    //-> then research the matched product in DB
  }

  const intentMap = new Map();
  intentMap.set('I want a product', searchProduct);
  agent.handleRequest(intentMap);

In this code, I ask to user the product that he's looking for. But when he answered "Apple", how can I receive the user response in the same function to continue my process ?

I know there is the "context" concept, but to continue the "search product" process, I need to come back in the function.

For now, I use dialog-fulfillment. And I try to understand this documentation to find the solution : https://github.com/dialogflow/dialogflow-fulfillment-nodejs/blob/master/docs/WebhookClient.md

Jean-louis Gouwy
  • 182
  • 3
  • 10

1 Answers1

0

The short answer is that you can't (or, at the very least, shouldn't) do it in the "same" function. Each function represents an Intent, or what the user has communicated to us. In the function we need to do the following:

  1. Determine what the user has said that is important to us.
  2. Compute anything based on what they've said.
  3. Send a reply to the user based on (1) and (2).

Once we have sent the reply to the user - that round of the conversation is over. We need to wait for the next Intent to be triggered by the user so we can repeat the above.

Contexts are used so we know which stage of the overall conversation we're in. As part of our reply (step 3 above), we can set a Context which will help Dialogflow determine which Intent should be triggered (and thus which function should be called to process what we know so far). Contexts can also store information about previous turns of the conversation.

Keep in mind that Intents aren't about what we say, but are about what the user says. The reply we send is based on what we need, and then we would use a single Intent to capture each part. The function that handles that Intent would store the answer in the Context and determine the next part of the question.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Do you know/have a concrete example by coding ? I've to ask some questions to find the best product for the user. If I take your logic, in my case, it means I need to create an intent for each question. – Jean-louis Gouwy Apr 21 '18 at 16:03
  • Because when the user will answer, for example, "t-shirt" (i have 18500 products in database), I've to ask anothers questions to refine, to be able to find the most relevent product. – Jean-louis Gouwy Apr 21 '18 at 16:05
  • I don't have code handy (and it would take a bit to write it), but hopefully the additional paragraph clarifies things. Intents aren't for questions - they are for answers. Your answers should fit similar patterns. – Prisoner Apr 22 '18 at 01:10
  • I'm totally agree with you. I understand it. But if you receive a product, your search in db, you receive 100 products, you need to ask another question to have a smaller result. At this moment, I need to keep the "history" in my API to launch a search again. – Jean-louis Gouwy Apr 22 '18 at 18:34
  • Then ask another question. (Step 3). The user will reply (Step 1). You'll determine if you need to ask more or can give results (Step 2). Repeat as necessary. – Prisoner Apr 22 '18 at 18:36