1

I am new to OpenWhisk / IBM Cloud Functions. I was trying to build a basic chat bot application using IBM Watson Assistant. So what I have is a cloud functions action which is being invoked from my Node.js server, the action has all the credentials to interact with the Watson service, I am using "watson-developer-cloud" npm package as a dependency. Everything works as expected when I am run on local machine, however, when I zip the directory and upload it as an OpenWhisk web action it is not able to install the dependencies.

The procedure I followed is:

  1. run npm install
  2. compress all the files within the current directory (including node_modules)
  3. upload action using the following command
    bx wsk action create chataction --kind nodejs:8 chatactionzip.zip
    (here chatactionzip is the compressed file name).

Can anyone help me get this working? I am uploading the screenshots of the directory structure.

package.json is like this

`

{
  "name": "chataction",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "watson-developer-cloud": "^3.13.0"
  }
}

`

this is my code (i am removing some credentials rest is as it is) `

const AssistantV1 = require('watson-developer-cloud/assistant/v1');

function main(params) {

    var inputText = params.inputText || 'input was not sent';
    //return {result: inputText}
    var assistant = new AssistantV1({
        username: '',
        password: '',
        url: '',
        //api_key: '',
        version: '2018-11-26'
    });

    var inputMessageParams = {
        input: {
            text: inputText
        },
        workspace_id: ''
    }


    assistant.message(inputMessageParams, function(err, result, response) {
        if(err) {
            console.log(err);
            return {err: err}
        }
        else {
            // console.log(response);
            // console.log(response.body.output.text);
            // console.log(response.data);
            return {result: response.body.output.text[0]}
        }
    });

    //return {notHit: 'npm not working'}
}

exports.main = main;

`

invoking code is like this `

const openwhisk = require('openwhisk');

options = {
    apihost: 'openwhisk.eu-gb.bluemix.net',
    api_key: ''
}

var ow =  openwhisk(options);
var params = {inputText: 'Hello'}
var name = 'chataction';
var blocking = true;
var result = true;
ow.actions.invoke({name, blocking, result, params})
.then((result) => {
    console.log(result);
});

`

directory structure for your reference.

Hasneet Singh
  • 363
  • 3
  • 10
  • can you clarify what you mean by "not able to install dependencies"? Is there a specific error message that you see? Generally I'd say the steps you're doing check out... Could you share some code? – tpei Nov 26 '18 at 09:08
  • What the error you are seeing on your console? Did you try invoking the action? Also, Can you provide your `package.json`? – Vidyasagar Machupalli Nov 26 '18 at 09:24
  • Check the node version in your `package.json`. – Vidyasagar Machupalli Nov 26 '18 at 09:31
  • (Not) Unrelated to the problem: Why are you using Cloud Functions to invoke Watson Assistant? Can't this be done from your node app directly? – data_henrik Nov 26 '18 at 09:57
  • @tpei what i mean is that while i am running the code on as a normal nodejs application i was able to send a text msg and get a response from the watson services. but when i put that same code into an action and invoke it i get an empty object in return.. – Hasneet Singh Nov 26 '18 at 11:03
  • @VidyasagarMachupalli i am running both on node@8 – Hasneet Singh Nov 26 '18 at 11:06
  • Add the code snippets and your package.json – data_henrik Nov 26 '18 at 11:07
  • @data_henrik i have added the action code and package.json. please check now. – Hasneet Singh Nov 26 '18 at 11:14
  • @data_henrik it can be and doing by that way it is working just fine. but i am trying to implement serverless archetecture here that is why but i am missing onto something here i guess. Should i provide anything to help you direct me towards the correct way? – Hasneet Singh Nov 26 '18 at 11:30

3 Answers3

4

Serverless Actions are async, you need to wrap your code in a Promise or use try/catch if the API your using already returning a Promise

You main function is ending before your method assistant.message() call is done

    return new Promise((resolve, reject) =>{
       assistant.message(inputMessageParams, function(err, result, response) {
          if(err) {
            console.log(err);
            reject({err: err})
         }
         else {
           // console.log(response);
           // console.log(response.body.output.text);
           // console.log(response.data);
           resolve( {result: response.body.output.text[0]})
         }
       });
    });

More info on asynchronous javascript in the docs here: https://console.bluemix.net/docs/openwhisk/openwhisk_reference.html#openwhisk_ref_javascript

csantanapr
  • 5,002
  • 2
  • 19
  • 15
1

A couple things:

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • okay so i am using the built-in watson-developer-cloud module of node 8 .. Thanx a lot for this recommendation :) it works fine now – Hasneet Singh Nov 26 '18 at 19:00
0

At the first glance,The action you created is chataction but you are invoking ChatActionZip

Vidyasagar Machupalli
  • 2,737
  • 1
  • 19
  • 29
  • I am sorry that was a typo i made while pasting here from a different file. it is "chataction" only in the code. i have edited the post now. – Hasneet Singh Nov 26 '18 at 11:43