Search code examples
websphereibm-watsonnode-redwatson-assistant

TypeError using Watson Assistant V2 in Node-RED


I'm trying to set up Watson Assistant for a Telegram Bot I'm building. Right know I have no problem getting answers from Watson using watson-assistant-v1 (i.e. assistant node), however, I'd like to use watson-assistant-v2 (i.e. assistant V2 node).

I do not know what I am doing wrong. For the API Key in Node-RED I'm using the API Key of the Assitant. However, when I send a message to the bot in debug I get "TypeError: Cannot read property '0' of undefined".

Am I missing something else? Why is this error happening


Solution

  • In the Assistant V1 node, the configuration is the Workspace ID.

    In the Assistant V2 node, the configuration is for the Assistant ID.

    For both input text is expected in msg.payload, though this can be empty to allow a dialog to be initiated. So for the V2 node this is a valid flow that should work

    [{"id":"83dbc697.d416c8","type":"inject","z":"f551e981.a6bf78","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":260,"wires":[["d334bd1c.00abb"]]},{"id":"d334bd1c.00abb","type":"watson-assistant-v2","z":"f551e981.a6bf78","name":"","service-endpoint":"https://gateway.watsonplatform.net/assistant/api","assistant_id":"f6f33980-9b6a-4bdf-92dd-e7974a832586","debug":false,"restart":false,"return_context":true,"alternate_intents":false,"multisession":true,"timeout":"","optout-learning":false,"x":410,"y":260,"wires":[["151a548c.d4f44b","6ad0ad7b.b34414"]]},{"id":"151a548c.d4f44b","type":"debug","z":"f551e981.a6bf78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":240,"wires":[]}]
    

    If you are setting up a dialog then you need to set up a context. Both nodes can manage the context for you, but they need an identifier for the context. For the V2 node this done by setting msg.params.session_id to an arbitrary value that makes sense to your application. eg.

    [{"id":"b30a8bd4.e5f628","type":"inject","z":"f551e981.a6bf78","name":"","topic":"","payload":"Hello","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":300,"wires":[["4c5c73b5.eef91c"]]},{"id":"4c5c73b5.eef91c","type":"function","z":"f551e981.a6bf78","name":"","func":"msg.params = {\n    \"session_id\" : \"ABC\"\n};\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":360,"wires":[["d334bd1c.00abb"]]},{"id":"d334bd1c.00abb","type":"watson-assistant-v2","z":"f551e981.a6bf78","name":"","service-endpoint":"https://gateway.watsonplatform.net/assistant/api","assistant_id":"f6f33980-9b6a-4bdf-92dd-e7974a832586","debug":false,"restart":false,"return_context":true,"alternate_intents":false,"multisession":true,"timeout":"","optout-learning":false,"x":410,"y":260,"wires":[["151a548c.d4f44b","6ad0ad7b.b34414"]]},{"id":"151a548c.d4f44b","type":"debug","z":"f551e981.a6bf78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":240,"wires":[]}]
    

    The node then manages the session and context for you, renewing the session whenever it expires.