-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
119 lines (95 loc) · 3.84 KB
/
index.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
const AssistantV2 = require('ibm-watson/assistant/v2');
const { IamAuthenticator } = require('ibm-watson/auth');
const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
function main(params) {
// Watson Assistant (WA) credentials
let wa_apikey = "{your-watson-assisatant-api-key}";
let wa_url = "{watson-assistant-url}";
let wa_version = "{watson-assistant-version}";
let assistantId = "{assistant-id-to-translate}";
// Language Translator (LT) credentials
let lt_apikey = "{your-language-translator-api-key}";
let lt_url = "{language-translator-url}";
let lt_version = "{language-translator-version}";
// variables for debugging
let originalInput = "";
let translatedInput = "";
let originalOutput = "";
let translatedOutput = "";
// instantiates LT and WA authenticators and objects
const authenticatorWA = new IamAuthenticator({ apikey: wa_apikey });
const authenticatorLT = new IamAuthenticator({ apikey: lt_apikey });
const assistant = new AssistantV2({
version: wa_version,
authenticator: authenticatorWA,
url: wa_url,
});
const languageTranslator = new LanguageTranslatorV3({
version: lt_version,
authenticator: authenticatorLT,
url: lt_url,
});
let session_id = "";
// assign variables from input parameters, passed from Watson Assistant
session_id = params.session_id; // ? params.session_id : '';
userUtter = params.user_utterance;
originalInput = userUtter;
language = params.language;
// LT parameters
const translateParams = {
text: userUtter,
source: language,
target: "en",
};
return new Promise((resolve, reject) => {
assistant.createSession({
assistantId: assistantId
}).then(res => {
session_id = res.result.session_id;
return languageTranslator.translate(translateParams);
}).then(function (translationResult) {
// if no session ID needs to be created
// languageTranslator.translate(translateParams).then(function (translationResult) {
// receive english translation from LT service
let englishTransl = translationResult.result.translations[0].translation;
console.log("English input: ", englishTransl);
translatedInput = englishTransl;
// send english translated input to WA
return assistant.message({
assistantId: assistantId,
sessionId: session_id,
input: {
'message_type': 'text',
'text': englishTransl
}
});
}).then(function (res) {;
// receive english translated response from WA
let englishResp = res.result.output.generic[0].text;
console.log("English output: ", englishResp);
originalOutput = englishResp;
// parameters for converting response to user language
const translateParams = {
text: res.result.output.generic[0].text,
source: "en",
target: language,
};
return languageTranslator.translate(translateParams);
}).then(function (translationResult) {
// receive bot response in user language
console.log("Output translation: ", translationResult.result.translations[0].translation);
translatedOutput = translationResult;
// return only the translated message
// return{"message":translationResult.result.translations[0].translation};
// returns translations at each step to the chatbot
resolve({ "message": translationResult.result.translations[0].translation, "originalInput": originalInput, "translatedInput": translatedInput, "originalOutput": originalOutput, "translatedOutput": translatedOutput });
}).catch(function (err) {
// if an error occurs, return the error
reject({ "error": err });
console.log("****error: ", err);
});
})
}
exports.main = main;
// This is for local testing
// main({session_id: "", user_utterance: "¿Que puedes hacer?", language: "es"});