问题
I have created a new app in wit.ai. In stories, after the 'User says', I have added a function getReply() using 'Bot executes' and added two context keys with branch in it. If both the keys are available, I'm sending a reply to the user using the 'Bot says' and it will go to the next step else it will ask the missing key to the user.
The issue is, in the reply I'm using only one of the context key. The flow works if that key is available. It doesn't considers about the other key. It is checking both keys only if we add both the keys in the reply.
In the code I'm checking those keys in the function getReply() and then adding it to the context.
const actions = {
send(request, response) {
...
...
...
return new Promise(function(resolve, reject) {
...
return resolve();
})
.then()) => null)
.catch((err) => {
'Error occurred',
id,
':',
err.stack || err
);
});
},
function getReply({context, entities}) {
return new Promise(function(resolve, reject) {
...
...
...
context.key1 = value1;
context.key2 = value2;
return resolve(context);
}
}
Is everything correct or am I missing something? Why the context key is not initiated if its not in the reply.
Thanks.
回答1:
Hmm in your example i dont see you add or remove keys in the context, depending one some logic. I did a little example how i should validate an email given by an user in Python and how to handle with context keys the case where the user gives an invalid email. I hope it will give you some idea on how to use context keys.
def save_email(br, cc):
"""Save email on context.
Arguments:
- br (dict) Bot Response
- cc (dict) Conversation Context
Returns an updated context
"""
context = br.get('context', {})
entities = br['entities']
email = first_entity_value(entities, 'email') # Use wit/email builtin entity
# First clean the context, its possible you saved one of those two keys in
# previous iterations, so clean them all just to be safe. As we are going to
# run email validation in the code below its safe to remove this keys/clean the
# context at the top of this method.
for key in ['email', 'bad-email']:
if key in context:
del context[key]
# Now validate that email is valid. If its valid, we add an 'email' key
# to the context. If email is not valid, we add a 'bad-email' key to the
# context. In this way, we can handle then at Wit.ai, depending which key
# is present, what the bot should do next.
validator = EmailValidator()
if validator(email).is_valid():
context['email'] = email
else:
context['bad-email'] = 1 # 1, 0, any value. What Wit will pay attention is
# that the 'bad-key' exists in the context.
# Finally, return the updated context
return context
As you can see it doesnt have sense to put both email and bad-email on context. I'm not sure what would Wit do in that case. As a reminder, its with context keys that we can modify how Wit predicts the following action.
I hope this clarifies and helps you solve your problem,
Best,
Emiliano.
来源:https://stackoverflow.com/questions/40927548/the-context-key-is-not-initialised-if-it-s-not-in-reply-wit-ai