-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcontext.js
72 lines (72 loc) · 3.34 KB
/
context.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const varsToUpdateBeforeWatson = {
/*variableName: {
value: false OR value, //Set a particular value on every call, set to false to ignore this field.
forceIfUndefined: true, //If the context variable does not exist yet, tells if it should be created
function: function(answerText, context, key) { //Different sets of actions depending on the answerText, can access the whole context, must not update context. @return the new context[key] value.
return new_context[key]_value;
}
}*/
};
const varsToUpdateAfterWatson = {
noCommande: {
value: false, //Set a particular value on every call, set to false to ignore this field.
forceIfUndefined: true, //If the context variable does not exist yet, tells if it should be created
function: function(answerText, context, key) { //Different sets of actions depending on the answerText, can access the whole context, must not update context. @return the new context[key] value.
var returnValue = 0;
if (answerText.indexOf('Tout est bon pour vous ?') !== -1) {
returnValue = Math.floor(Math.random() * 175000 + 100000); //context[key] + 1;
} else if (context[key] !== returnValue) {
returnValue = context[key];
}
return returnValue;
}
}
};
module.exports = {
/**
* Returns context before it's sent to Watson Conversation API.
* The rules to update variables are in the static array varsToUpdateBeforeWatson.
* @param {Object} inMemoryContext current context
* @param {string} messageText user text message
* @return {Object} modified context
*/
setContextToWatson: function(inMemoryContext, messageText) {
if (Object.keys(varsToUpdateBeforeWatson).length !== 0) {
for (key in varsToUpdateBeforeWatson) {
var currentUpdate = varsToUpdateBeforeWatson[key];
if (typeof inMemoryContext[key] !== 'undefined' || currentUpdate.forceIfUndefined) {
if (currentUpdate.value !== false && currentUpdate.value !==
inMemoryContext[key]) {
inMemoryContext[key] = currentUpdate.value;
} else if (currentUpdate.function !== false) {
inMemoryContext[key] = currentUpdate.function(messageText,
inMemoryContext, key);
}
}
}
}
return inMemoryContext;
},
/**
* Updates context after it has been returned from Watson Conversation API.
* The rules to update variables are in the static array varsToUpdateAfterWatson.
* @param {Object} watsonUpdate return from Watson Conversation API which contains output, context, and all sorts of data.
*/
setContextAfterWatson: function(watsonUpdate) {
if (Object.keys(varsToUpdateAfterWatson).length !== 0) {
for (key in varsToUpdateAfterWatson) {
var currentUpdate = varsToUpdateAfterWatson[key];
if (watsonUpdate.context && typeof watsonUpdate.context[key] !== 'undefined' ||
currentUpdate.forceIfUndefined) {
if (currentUpdate.value !== false && currentUpdate.value !==
watsonUpdate.context[key]) {
watsonUpdate.context[key] = currentUpdate.value;
} else if (currentUpdate.function !== false) {
watsonUpdate.context[key] = currentUpdate.function(watsonUpdate
.output.text, watsonUpdate.context, key);
}
}
}
}
}
}