0

I have a small chatbot, which reacts to the word: "test". On reading this word, he is supposed to call an IBM Cloud Function which in return tries to register the user returning a token. The Problem is, that the authentication API takes about 30s to respond and the IBM Cloud function executes immediately. So my response is always like this

    {
        "body": {},
        "headers": {"Content-Type": "application/json" },
        "status": 200
    }

But it should actually return the token in the body
    {
        "body": { "token": ... },
    ...
    }

I have already tried setting the timeout to 5min, but this did not help. I tried aswell in Postman, there I get the correct response. Then I exported the request for javascript and modified it to work in the Cloud Function but again it returns an empty body.

var request = require("request");

function main({id=123}) {
    var options = { method: 'POST',
      url: 'ip:port/v1/authentication',
      headers: 
       {
         "Host": "ip:port",
         "Content-Type": "application/json" },
      body: { username: '---', password: '---' },
      json: true };

    var result = { error: "No result was received" };
    request(options, function (error, response, body) {
      if (error) throw new Error(error);
      console.log(`Response body is: ${response.body}`);
      result = { token: response.body.token };
    });


    return {
        statusCode: 200,
        headers: { "Content-Type": "application/json" },
        body: { token: result["token"] }
    };
}

And below you can see the messages I get

Activation ID:
...
Results:
{
  "body": {},
  "headers": {
    "Content-Type": "application/json"
  },
  "statusCode": 200
}
Logs:
[]

Sadly the logs show no error message since everything is logically working.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
Patrick.H
  • 535
  • 3
  • 6
  • 21

2 Answers2

1

The overall time for the roundtrip when calling a Cloud Function or webhook has a limit of 5 seconds. This is imposed by Watson Assistant, so changing any timeouts in Cloud Functions does not help.

You can either optimize the flow (why is it taking that long?) or split it in two actions. The first call would kick of the authentication, the second call check that it succeeded. There are a dialog options to tell the user that something is happening without waiting for user input (skip user input). Use that to bridge the time until the user is authenticated.

data_henrik
  • 16,724
  • 2
  • 28
  • 49
  • sadly the optimization is not possible due to it being a slow test server, thanks for the clarification! Just one last thing, your source to the limit of 5s is not working. Do you have an updated link? – Patrick.H Aug 03 '19 at 17:13
1

This is a rookie Node.js error.

request is an asynchronous call. The body

      if (error) throw new Error(error);
      console.log(`Response body is: ${response.body}`);
      result = { token: response.body.token };

executes when the request is completed. The return

    return {
        statusCode: 200,
        headers: { "Content-Type": "application/json" },
        body: { token: result["token"] }
    };

should be in side this body. Where you have it will return immediately without the token, before the request for the token has completed.

So your code should be -


    request(options, function (error, response, body) {
      if (error) {
        return {
          statusCode: 500,
          body: { error: error }
        };
      }
      console.log(`Response body is: ${response.body}`);
      result = { token: response.body.token };

      return {
        statusCode: 200,
        headers: { "Content-Type": "application/json" },
        body: { token: result["token"] }
      };

    });


chughts
  • 4,210
  • 2
  • 14
  • 27