-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathResponseLookup.js
130 lines (128 loc) · 4.1 KB
/
ResponseLookup.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
*
* main() will be run when you invoke this action
*
* @param Cloud Functions actions accept a single parameter, which must be a JSON object.
*
* @return The output of this action, which must be a JSON object.
*
* {
"payload" : {
"output" :
{
"generic" :
[
{
"response_type": "text",
"text": "WELCOME"
}
]
}
},
"context": {
"skills": {
"main skill": {
"user_defined": {
"language": "es"
}
}
}
}
}
*
*/
let rp = require("request-promise");
let _ = require("lodash");
function main(params) {
console.log("Input Params from pre-webhook");
console.log(JSON.stringify(params));
const CloudantSDK = require("@cloudant/cloudant");
const cloudant = new CloudantSDK({
url: "<CLOUDANT_URL>",
plugins: { iamauth: { iamApiKey: "<CLOUDANT_APIKEY>" } },
});
var defaultDialogLanguageCode = "en";
const databaseName = "mydb";
//We are going to check first in the AnswerStore DB if the text is there, otherwise we can translate it
if (
params.payload.output.generic[0].text !== "" &&
params.payload.context.skills["main skill"].user_defined &&
params.payload.context.skills["main skill"].user_defined.language !==
defaultDialogLanguageCode &&
params.payload.context.skills["main skill"].user_defined.language !== "none"
) {
var languageCode =
params.payload.context.skills["main skill"].user_defined.language ||
defaultDialogLanguageCode; //language code from the WLT identify OR by default "en" English language code to search for
var searchKey = params.payload.output.debug.nodes_visited[0].dialog_node;
console.log("searchkey " + searchKey);
return new Promise(function (resolve, reject) {
cloudant
.use(databaseName)
.get(searchKey)
.then((answerUnit) => {
if (answerUnit) {
console.log("Returning the answer from the DB using the search key: " + searchKey);
console.log(answerUnit.answerText);
if (languageCode == "es") {
params.payload.output.generic[0].text = answerUnit.es;
} else if (languageCode == "fr") {
params.payload.output.generic[0].text = answerUnit.fr;
} else {
//default to "en"
params.payload.output.generic[0].text = answerUnit.en;
}
const response = {
body: params,
};
resolve(response);
}
})
.catch((error) => {
console.log("error from DB " + error);
// If there is no data from the DB, we translate the output using WLT
const options = {
method: "POST",
url:
"https://<WATSON_LANGUAGE_TRANSLATOR_URL>/instances/<YOUR_INSTANCE_ID>/v3/translate?version=2018-05-01",
auth: {
username: "apikey",
password: "<WATSON_LANGUAGE_TRANSLATOR_APIKEY>",
},
body: {
text: [params.payload.output.generic[0].text],
target: params.payload.context.skills["main skill"].user_defined.language,
},
json: true,
};
resolve(
rp(options).then((res) => {
console.log(
"Translating Output to " +
params.payload.context.skills["main skill"].user_defined.language
);
params.payload.context.skills["main skill"].user_defined["original_output"] =
params.payload.output.generic[0].text;
params.payload.output.generic[0].text = res.translations[0].translation;
const response = {
body: params,
};
return response;
})
);
});
});
} else {
const response = {
body: params,
};
return response;
}
}