Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax error when running IdentifyTranslate.js #2

Open
lee-zhg opened this issue Jul 29, 2021 · 1 comment
Open

syntax error when running IdentifyTranslate.js #2

lee-zhg opened this issue Jul 29, 2021 · 1 comment

Comments

@lee-zhg
Copy link
Contributor

lee-zhg commented Jul 29, 2021

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)“}

@lee-zhg
Copy link
Contributor Author

lee-zhg commented Jul 29, 2021

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.

/**
*

  • 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.

*/

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,
    };

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant