Open lee-zhg opened 3 years ago
Weird invisible special characters caused the syntax errors. I got rid of the most of them except one. So, I had to remove it manually.
The code did not work even after removed the special characters. Code below is working on my system now. Thank you for the sample code.
BTW, I did not clone the repo. So, I won't create any pull request.
/*
*/
let rp = require("request-promise"); let _ = require("lodash"); function main(params) { console.log("Cloud Function PreMessageTranslation started"); console.log(JSON.stringify(params));
if (params.payload.input.text !== "") { const options = { method: "POST", url: "https://api.us-south.language-translator.watson.cloud.ibm.com/instances/4dddb3-9eb5-431e-8de5-b38371a4b50b/v3/identify?version=2018-05-01", auth: { username: "apikey", password: "UUXbj5MS4sEIhycQbATF", }, headers: { "Content-Type": "text/plain", }, body: [params.payload.input.text], json: true, }; return rp(options).then((res) => { var defaultDialogLanguageCode = "en";
const confidence = _.get(res, "languages[0].confidence");
console.log("confidence " + confidence);
const language =
confidence > 0.5 ? _.get(res, "languages[0].language") : defaultDialogLanguageCode;
_.set(params, 'payload.context.skills["main skill"].user_defined["language"]', language);
if (res.languages[0].language !== defaultDialogLanguageCode) {
const options = {
method: "POST",
url:
"https://api.us-south.language-translator.watson.cloud.ibm.com/instances/4777b0b3-9eb5-431e-8de5-ba4b50b/v3/translate?version=2018-05-01",
auth: {
username: "apikey",
password: "UUXbj5MB7UGhS4sEIhycQbATF",
},
body: {
text: [params.payload.input.text],
target: defaultDialogLanguageCode,
source: language
},
json: true,
};
return rp(options).then((res) => {
console.log("PRE-Translate - translating");
console.log(JSON.stringify(res));
params.payload.context.skills["main skill"].user_defined["original_input"] =
params.payload.input.text;
params.payload.input.text = res.translations[0].translation;
console.log(JSON.stringify(params));
// const result = {
// body: params,
// };
return {
body: params,
};
});
} else {
console.log(JSON.stringify(params));
// const result = {
// body: params,
// };
return {
body: params,
};
}
});
} else { params.payload.context.skills["main skill"].user_defined["language"] = "none"; // const result = { // body: params, // }; return { body: params, }; } }
Tried to run code IdentifyTranslate.js for pre-webhook and received the error below:
ul 29 07:13:12 functions crn:v1:bluemix:public:functions:us-south:a/2c426324cc934f71a2a053c9185791ff::: {“error”:“Initialization has failed due to: SyntaxError: Invalid or unexpected token\n at initializeActionHandler (/nodejsAction/runner.js:63:80)\n at doInit (/nodejsAction/src/service.js:174:16)\n at initCode (/nodejsAction/src/service.js:93:24)\n at /nodejsAction/platform/platform.js:125:17\n at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)\n at next (/node_modules/express/lib/router/route.js:137:13)\n at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)\n at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)\n at /node_modules/express/lib/router/index.js:281:22\n at Function.process_params (/node_modules/express/lib/router/index.js:335:12)“}