jsonnode.jswatson

How receive username in watson from outside the dialog Node.js


I'm creating a chatbot using the webApplication from IBM with Node.js.

The chatbot will migrate to a "blog" in a intranet that requires login. So I want to pass the username as a context to Watson before the chat start, getting it from the website.

Input a new context on json though the code.

  "context": {
    "conversation_id": "8a433bbf-8e27-42ba-bdac-9341f5b16fcf",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1,
      "dialog_request_counter": 1,
      "_node_output_map": {
        "node_1_1528906594423": [
          0
        ],
        "node_14_1527085355836": [
          0,
          0
        ]
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    },
    "timezone": "America/Sao_Paulo",
    "nome_bot": "Malu"
    "username": variable
  }

So in the end, the conversation starts as:

User logged as John.

Watson: "Hello, $username." / "Hello, John."


Solution

  • Solved the case! In 'public/script.js' is where the code call the .json, so we need to make the change there!

    // Calling server and get the watson output
    const getWatsonMessageAndInsertTemplate = async (text = '') => {
      const uri = 'https://localhost/conversation/';
      const response = await (await fetch(uri, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          text,
          context,
        }),
      })).json();
    

    So if we add to the context the variable that get the username from database and put it on the context 'username', the result is as expected!

    ...
    context: { 'username': variable },
    ...