watsonx Assistant

 View Only
  • 1.  Update context variable with JSON

    Posted Tue September 22, 2020 07:17 PM
    I have a problem for which I have spent several hours looking for a solution on the Internet but I have not found any literature or tutorial to help me. If anyone here can help me I would be very grateful.

    The situation is: I have a Node where I define a list of options, You can see below  the Node and it JSON content.



    { "output": { "generic": [ { "title": "aaaaa", "options": [ { "label": "aaaaa", "value": { "input": { "text": "a" } } }, { "label": "bbbbb", "value": { "input": { "text": "b" } } } ], "response_type": "option" } ] } }​

    In this NODE I also have a WebHook that calls an API that returns another JSON like the one below.

    { "output": { "generic": [ { "title": "Relação de Cidades", "options": [ { "label": "São Paulo", "value": { "input": { "text": "SP" } } }, { "label": "Rio de janeiro", "value": { "input": { "text": "RJ" } } }, { "label": "Bahia", "value": { "input": { "text": "BA" } } } ], "response_type": "option" } ] }​

    How do I update the JSON that is in this NODE with the JSON that came from the API ..? and where do i do that ..?

    Thank You in advance.

    ------------------------------
    Eduardo Gameiro
    ------------------------------

    #WatsonAssistant


  • 2.  RE: Update context variable with JSON

    Posted Thu September 24, 2020 06:10 AM
    additionally :

    Or could I send this JSON that returns from the API straight into the conversation in such a way that it assembles the list of options that JSON creates ...?

    I've tried to put this JSON in a variable and show the content of that variable in the conversation, but it shows the content of the JSON instead of assembling the list that JSON defines.



    ------------------------------
    Eduardo Gameiro
    ------------------------------



  • 3.  RE: Update context variable with JSON

    Posted Thu September 24, 2020 01:07 PM

    Its not a super straight forward answer to add data from a webhook to the options response type, but here is a tutorial we wrote how it can be done:

    https://medium.com/@jan.vystrcil/e14c5e08beca



    ------------------------------
    Mitch Mason
    ------------------------------



  • 4.  RE: Update context variable with JSON

    Posted Thu October 01, 2020 09:02 AM
    Hello Mitch..
    Thank you for your replay.
    I am trying to reproduce the example of your tutorial but I am facing a error.
    I copy/paste your snippet node.js code.. by the way is it node.js..?

    I pasted exactly your code but received this error :


    ID de ativação:
    6d9ff769cbca44069ff769cbca1406a7
    Resultados:

    {
    "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)"
    }

    Logs:

    [
    "2020-10-01T12:59:25.015649Z stderr: Error during initialization: SyntaxError: Invalid or unexpected token",
    "2020-10-01T12:59:25.015701Z stderr: at initializeActionHandler (/nodejsAction/runner.js:63:80)",
    "2020-10-01T12:59:25.015706Z stderr: at doInit (/nodejsAction/src/service.js:174:16)",
    "2020-10-01T12:59:25.015711Z stderr: at initCode (/nodejsAction/src/service.js:93:24)",
    "2020-10-01T12:59:25.015715Z stderr: at /nodejsAction/platform/platform.js:125:17",
    "2020-10-01T12:59:25.015719Z stderr: at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)",
    "2020-10-01T12:59:25.015722Z stderr: at next (/node_modules/express/lib/router/route.js:137:13)",
    "2020-10-01T12:59:25.015726Z stderr: at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)",
    "2020-10-01T12:59:25.015730Z stderr: at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)",
    "2020-10-01T12:59:25.015734Z stderr: at /node_modules/express/lib/router/index.js:281:22",
    "2020-10-01T12:59:25.015738Z stderr

    Could you help in correct it..??
    Thank you in advance.

    ------------------------------
    Eduardo Gameiro
    ------------------------------



  • 5.  RE: Update context variable with JSON

    Posted Thu October 01, 2020 09:31 AM
    I already found out ...
    I replaced the double quotes with single quotes and it worked.

    I will continue with the tests and then put the result here.

    ------------------------------
    Eduardo Gameiro
    ------------------------------



  • 6.  RE: Update context variable with JSON

    Posted Thu October 01, 2020 11:26 AM
    Hello Mitch..

    Once again I would like to thank you very much for your replay.
    I had implemented you tutorial and it work perfectly.
    Once again Thank you.

    Now I have to upgrade the Function (node.js) and add to it the select query on my database in order to produce the results that my App need.

    But this is the "Episode II - Attack of the Clones" rsrsrs...

    Best Regards.


    ------------------------------
    Eduardo Gameiro
    ------------------------------