How to get dynamic response based on the result of the action performed in Api.ai? - dialogflow-es

I'm trying out few examples in Api.ai console. I see there are examples where the agent responds by using the values that are extracted from the user query or from the context. But is there a way to display response that is formed using the output of the action performed?
For example -
User: Is my pizza order confirmed?
Action: check order status
Response: Yes, it's confirmed (or "No, it's not")

You will have to use some custom webhook to do this, either integrating with a 3rd party API or will a server side call of your own to a database- in this case the webhook would take the user's ID and query if their pizza is ready or not, and this would then be passed back to API.ai.
Here is an example using a 3rd party webhook to do what you are asking:
https://github.com/api-ai/apiai-weather-webhook-sample

Yes you can do this with webhooks.
Typically, you would save the status of the order in your db, then you could have an intent that would:
Understand the query of your user (Is my pizza order confirmed)
The intent would call your webhook
the webhook would return the status order.
Api.ai would then give the answer.
When you use webhooks you need to return the speech response, the displayText, optionally the data that your apps need, the contextOut and the source.
I guess you would also need to have implement some context in api.ai so that you could now who is asking about the satus of the order. You would then be able to use the sessionId

Matt is correct here. You must use a webhook. Unfortunately the documentation from API.AI is currently very poor. Here's a screenshot example of an incoming post request. My message function will return an object with a desired response. In this case, I am using a Telegram chat bot. If you are not using any messenger, you can drop the data key: value. FollowupEvent is also optional.
Node.JS code with Restify
On your API.AI intent, make sure to check off Webhook. If you end getting the text response 'Broken' here, then something went wrong and you should check your error codes.
API.AI intent
And lastly, under the fulfillment tab, make sure to enable the webhook and point it at the right endpoint.

Related

Do we need webhook when our server is interacting with dialogflow?

We are implementing our customized chatbot using dialog flow. When user enters any text, our javascript code sends this text to our python server and the server interacts with google dialog flow and server gets complete response. I just have couple of questions as below.
When server gets the response from dialog flow, it will process the
response and sends some response to UI. Do we still need to have
fulfillment enabled as our server is getting response? Basically if
server is interacting with dialog flow and getting response, what is
the use of webhook?
Is there anyway to enforce the dialog flow intents require at least
one of entities? I went through Can I make Dialogflow intents require atleast one of the trained entities? which says to enable webhook fulfillment for that intent and if no entities were provided, re prompt the user for at least one of a list of entities. So in my case, if webhook is not needed, do I need to do it in the server once server receives response or is there anyway dialog flow will automatically enforce the condition with out server taking the responsibility?
In your case, no, you don't need to use webhook fulfillment.
You may still wish to use it, however, if you want to separate business logic (which would be in the webhook) from UI/UX logic (which would be in your python server and in the javascript client). But there is no requirement that you separate things this way.
Similarly, you can use your python code to enforce "at least one of" the parameters matching - you're moving that logic from the webhook into your existing server.
Either way, this is a bit kludgy. One alternative if you have different entity types is to have multiple Intents, one for each possible type, and to mark the parameter as required. This way the Intent will only match if the parameter is provided. If you then need to report each of these Intents as the "same" Intent, you can add that logic to your python code.

Default Fallback Intent responses + webhook fulfillment

I'm looking to add some logging of utterances that hit the default fallback intent. I imagine that's a pretty common desire, so maybe I'm missing some out of the box functionality that would be the real answer.. if so, please let me know!
That said, I'm trying to make use of the default text Responses section of the dialogflow UI, and additionally use a webhook to log the utterance.
I haven't tried this combination before, and I'm not sure how you are intended to do it. The user gets one of the text responses, and my webhook is called, but it seems that the webhook is expected to return a response as well. I can't simply log the utterance and be done.
Additionally i can't add suggestion pills, b/c a simple response is also required to be paired with that, even though the user already got a simple response from the default text responses.
Is it a valid use case to have your text response come from the intent responses section in the dialogflow UI, and still use a webhook to do "other" stuff? If so what is the correct way to do so?
If you really want to do it this way, don't return a JSON response and Dialogflow will use what you have sent in the response. This is not unusual, although most people tend to switch to doing responses through webhooks.

Dialogflow Integration into Custom CRM

I am trying to wrap my head around using Dialogflow for developing and integrating an SMS chatbot with our custom CRM. The creation of an Intent is pretty powerful and straight forward. However, I am trying to understand best practices for something. If I have an intent used to return the price of a service at a certain location, I can model that very easily within dialog flow. However, when an SMS message comes in, it will be from a new customer or a known existing customer for a certain location. For existing customers, we already know the location and therefore don't want them to have to specify the location value in the intent. Prior to sending the inbound SMS message to the client API to match the intent, how can I pre-set the "location" parameter value in the intent so it does exists even if that inbound SMS message did not include it? For example a known customer in Dallas would just have to say "how much is a xxx" instead of "how much is a xxx in Dallas".
Can you use the API to set a parameter value prior to calling the API to try and match the intent? If so, how do you get do that without a session ID? The reason the "location" is needed is because when we get to the fulfillment, the prices for the same service are different based on the location so I will need to be known but we don't want to make existing customers say the location.
Maybe another option is to have a Location intent with an event that we can trigger through the API. this would have an output context on it called location and fulfillment that sets the parameter value. But even then I struggle with understanding how to pass in values like location, phone number, etc into dialogflow from the calling application so dialogflow has those parameter values to use in fulfillment.
Reading documentation, watching videos and starting to test client API v2
This is certainly possible. What you would want to do is use the Dialogflow API for this. Here you can find the languages for which Google has created client libraries: https://cloud.google.com/dialogflow/docs/reference/libraries/overview
As soon as you have any 'if' in your code you should use the fulfillment: https://dialogflow.com/docs/fulfillment
How I would handle this:
Client sends SMS
You check in your back-end if this user is known. If known -> don't ask location, else you ask the location
Match the user query against the Dialogflow client library
Dialogflow will return the intent if (any) is matched
You should define and implement any logic before calling the Dialogflow library.

How can I trigger a dialogflow intent inside a javascript code?

I have a chatbot using dialogflow agent. I wrote a javascript back end code for its webhook fulfillment. I checked some condition in this code and if the condition is true I want a special intent to be triggered. How can I do this? What is the correct java script code to triggered an intent inside a java script if statement?
What function do you intend to perform by triggering an intent? Diaglogflow has v1 & v2 API references for this. You can do GET/POST/PUT/DELETE operations by triggering an intent using ajax call in your javascript code. Please read more about this here https://dialogflow.com/docs/reference/agent/intents
To make things clearer, DialogFlow intents are triggered by querying using the query API (https://dialogflow.com/docs/reference/agent/query)
Note: V1 of the DialogFlow API will deprecate soon, and will be replaced by V2 which is using gRPC.
This means that if you wish to trigger the intent programmatically and have the fulfilment triggered as well, you should have the following:
Script (can be on JavaScript as you would like to) with REST API request (doable using the Axios library) to the DialogFlow Query API.
DialogFlow will pick up the query, runs it through and triggers the Intent (if the query matches the intent).
In processing the intent, because there is a fulfilment, DialogFlow will run the fulfilment, which is another API/webhook call to your server hosted somewhere.
Your server will pick up the API request, and process accordingly.
Try out the Query API from DialogFlow and see if it works out for you.
Okay, I am late here, but I have used the below approach to encounter such a situation.
You can use custom event and intent chaining.
I followed the below URL and implemented the same. Maybe this help someone.
enter link description here

Server-side query using events or/and context?

I’m coding a bot using PHP-BotMan for complexity reasons and using Dialogflow query api to extract and manipulate the informations from the response. I saw examples and hints from people here and on dialogflow forum suggesting using context or events, some of them mixing both. What is the better way to handle this?
The flow of the application is:
user messages bot
bot queries (text or/and #event?) dialogflow
internally process a reply or return dialogflow slotfilling* request
text response bot reply user with last reply or asking to fill slot
Also, how can I be sure that a slotfilling process is finished with “actionIncomplete” only having two values, NULL or TRUE? The dialogflow query response doesn’t show wich slotfilling parameters are required or not…
Thanks for the help!!
slotfilling is when dialogflow sends a text response requesting required parameters to finish an intent, adding those replied values to the context
I was trying something similar to your scenario, here are few points i found helpful:
When Slotfitting with webhook, i can't use the "Required" params field since i have to control the input parameters via webhook (query database to provide options). Which means actionIncomplete field is not useful anymore.
I personally prefer to use context as it can add/remove params which gives you more control.
Hence the dialog was designed to use webhook to check all required params before move on to next conversation flow. and pop quick replies menu to ease and restrict possible input from users.
HTH.

Resources