Watson Conversation: Check and act on existence of context variable? - ibm-cloud

I am using IBM Watson Conversation service and slots within a dialog node. In the response I want to return the value of a context variable that is gathered with a slot. However, I cannot be sure that the variable exists because the user could have cancelled the input process and because the variable is optional. How can I check the existence and act depending on that check?
Using this
<? $myVariable ?>
gives an error when the variable is not present.

The Conversation service uses Spring Expression Language (SpEL) to process variables and conditions in its responses. There is a special check that can be applied. I have taken the example from this useful collection of use cases:
"<? context.myVariable? 'Great. I have the following: '+context.myVariable+'.' :
'No information present' ?>"
You can access context variables via context followed by the variable name. The question mark (?) checks for existence. The first response is taken in the case the variable is present, else the second response. The colon (:) separates the two answer options.
In the above example is the answer is either "Great. I have the following: VALUE" or "No information present".

Related

DialogFlow CX how to declare parameters in the start page?

I need to declare a parameter to store username as input in the first message of dialogflow session, "My name is John" will be the first message the user type to dialogflow cx,
I noticed it's not possible to create a parameter on the Start page, is there is a way to detect parameters using EntityType from the first message of the session ?
To detect parameter values from the first end-user input, you can use intent parameters.
First, create an intent and add training phrases like "My name is John" and similar. See how the intent may look like.
Then, add an intent route on the Start page and define agent response in fulfillment. See how the route and a simulator test may look like.
You can also check the general agent design best practices for more information.

How to perform slot validation in webhook in Dialogflow?

How to perform slot validation in an intent using backend code (webhook).
I have seen how to perform slot filling using the webhooks but I want to know how to validate the slot data and re-prompt the user if validation fails.
Example:
User: I want to know the your services in London.
Bot: We do not provide service in London, please enter some other city name.
In short: If validation fails, reset the dialog contexts, trigger your intent again, and optionally use default values to keep other parameters that were actually valid (so you don't need to re-prompt the user for those again).
You don't need to declare an incoming context on that intent to achieve this. Note that you can use contexts in intents, even though they are not declared as incoming/outgoing contexts on that intent.
In this example, I'm requesting 2 parameters from the user (car make and model). Of course, Enable webhook call for slot filling needs to be set in your intent.
Steps:
On Dialogflow, in the intent, declare an Event. This can be used to trigger this intent from your fulfillment code:
In your parameters, declare a Default Value for each parameter you want to be able to keep after resetting the intent:
Set the Default Value to a parameter in a helper context. If this helper context does exist, the default value will be set, otherwise, it'll be kept empty. This will allow you to reset the intent and keep other parameters you already had. In this example, I'm using the context show-car-details-data, and setting the default value of parameter model to _model in that incoming context:
In your slot-filling fulfillment method, you can validate your parameter and re-prompt the user by reseting the intent. To do that, you need to 1) clear the current dialog contexts, 2) call setFollowupEvent to trigger your intent again, and 3) optionally set up some helper context you can use to assign default values (so you don't need to re-prompt user for those that were valid).
I'm using Dialogflow Fulfillment Node.js Library:
// clear dialog contexts:
agent.contexts.forEach( e => {
if ( e.name.endsWith('_id_dialog_context') ) agent.context.delete(e.name);
});
// workaround bug: https://github.com/dialogflow/dialogflow-fulfillment-nodejs/issues/160
agent.add('');
// set follow up event: this triggers your intent again
agent.setFollowupEvent('show-car-details');
// optionally set helper context to set default values and avoid re-prompt of already valid values
// in this example, keep make since it was a valid parameter
// clear model because it was invalid. User will be prompted again on model, but not on make
agent.context.set('show-car-details-data', 1, { '_make': make, '_model': '' });
Steps
Enable webhook for slot filing in Dialog flow with Entity set as "Required".
Get entity from arguments.
Programmatically check if it is not null and as per your requirement.
If it is correct, proceed further.
Else, RESET THE CONTEXT and ask the question as per the wrong slot input. Programmatically, set the output context same as input so that user can again respond back slot/entity. If the correct context is not present, the slot will not get captured. When a slot is captured as per set Entity, the context for that slot captured is finished and to recapture it you need to increase the lifespan or create it again.

Watson Conversation Service - Quit Parameter for Slots/ Entities

There is a new feature in the conversation service where you can define slots/ entities for specific intents to extract the relevant information from the user input like currencies or specific string inputs. Those slots can be set mandatory in case you need them to proceed and the user will be asked for missing slots until he provides them.
Is it possible to define sth. like quit parameter so I can easily interrupt this conversation? The general documentation does not provide any information regarding this problem.
https://console.bluemix.net/docs/services/conversation/entities.html#defining-entities
You can do it by adding node-level handler which will listen to your cancellation intent and fill the slots with dummy values.
You can read more about this approach in the documentation: https://console.bluemix.net/docs/services/conversation/dialog-build.html (paragraph "Handle requests to exit the process")

IBM Watson Conversation: How to dynamically create context variables?

In IBM Watson Conversation, context variables can be used to carry information. A value can be derived from the evaluation of expressions as shown:
"context": {
"mycity": "<? entities['city'].value ?>"
}
What I tried is to dynamically compose the variable name:
"context": {
"my<? #animals ?>": "my new value"
}
However, the expression in the variable name is not evaluated. How can I define a variable name based on available data such as intents, entities or other context variables?
One solution seems to be to do it in the application:
Receive response object including all the metadata.
Extract necessary data
Compose context object to be passed back into Watson Conversation for next message processing. Create new variables as part of this preparation.
Call message API with context object which includes new variable.
New variable is available during Watson Conversation-internal processing.

Client context validation of data

As for tracking in AEM I am using CQ_Analytics for a scenario. We have a requirement like, I have to capture a value called "sort type" which is on the page when a user clicks on a button on that page and store it in ClientContext. I have written the below Javascript function which accepts a name argument. Using some code I am able to get hold of sort type value and passing it to the below function. Now my query is, how do I validate whether the name variable is assigned to the Client Context???
I have kept an alert statement and tried checking with multiple combinations but I am unable to figure out what is the correct way to conclude that my name value has been assigned to Client Context or not. Please help with my query.
function myFunction(name) {
CQ_Analytics.record({event: 'sorttype',
values: {'sortSelectedOption': name },
componentPath: '<%=resource.getResourceType()%>'
});
alert(CQ_Analytics.record.sorttype.sortSelectedOption);
}
You can see this post how to make your custom client context and how to store your data. http://blogs.adobe.com/aemtutorials/2013/07/24/customize-the-client-context/
After you create your client context, you have in the example CQ_Analytics.CustomStoreMgr.setTraitValue function that will save your parameter into client context.