问题
I'm creating sample application using Watson conversation API in nodejs. I'm trying to get username and send them to Watson and then I want to say hello "$username", also I want to keep that throughout the conversation so that I can if the user ask if I remember the name, Watson can say "yes, "$username"".
Can someone help me with sample code, how to use intent in this use case.
// Start conversation with Hello message.
conversation.message({
input: { text: "Hello" },
}, processResponse);
// Process the conversation response.
function processResponse(err, response) {
if (err) {
console.error(err); // something went wrong
return;
}
// If an intent was detected, log it out to the console.
if (response.intents.length > 0) {
//console.log('Detected intent: #' + response.intents[0].intent);
console.log(response.context)
}
// Display the output from dialog, if any.
if (response.output.text.length != 0) {
console.log("Watson : "+response.output.text[0]);
}
// Prompt for the next round of input.
var newMessageFromUser = prompt('Me : ');
// Send back the context to maintain state.
conversation.message({
input: { text: newMessageFromUser },
context : response.context,
}, processResponse)
}
回答1:
You can use context variables for that.
To extract the name, request the name in 1º node
, and in the next
node, add this JSON:
{
"context": {
"name": "<? input.text ?>"
},
"output": {
"text": {
"values": [
"Hello $name."
],
"selection_policy": "sequential"
}
}
}
The input.text
capture all user has typed.
You can use $
for set and get the context variable with the name: $name
will show what user has typed.
In other case, if user typed my name is XXXXX, you can use regex inside your context variable. Check this example for use REGEX inside context variables:
"name" : "<? input.text.extract('^[A-Z][-a-zA-Z]+$') ?>"
@MichelBida did one example with regex, but with another request for user, check this link.
EDIT:
Use:
context.myproperty = "Hello World";
And now have the System Entity @sys-person
, this entity extracts names from the user's input. Names are not normalized by the system, so "Will" is not treated as "William", nor vice versa, see official documentation. This entity can be active within Entity -> System Entities -> @sys-person
来源:https://stackoverflow.com/questions/43229538/using-watson-conversation-how-to-handle-customer-account-number-etc