👨‍🚀 Setup of JS handlers for each DialogFlow intent - ponchotitlan/ciscoCVP_googleDialogflow_dance GitHub Wiki

                                         +------------------------+                       
                                         |                        |                       
                                         |                        |                       
                                         |    Google DialogFlow   |                       
                                         |                        |                       
                                         |                        |                       
                                         +------------------------+                       
                                                 |        |                               
                                                 |        |    Webhooks                   
                     -> Intent params            |        |                               
                     <- API results/flags/prompt |        |                               
                                         +------------------------+                       
                                         |                        |                       
                                         |    NodeJS Middleware   |                       
                                         |    (Intent Handler)    |                       
                                         |                        |                       
                                         +------------------------+                       
                                           |          |         |                         
                                           |          |         |                         
                                   +--------+    +--------+    +------------+            
                                   |  CRM   |    |  API   |    |  3rd Party |             
                                   +--------+    +--------+    |    API     |             
                                                               +------------+             

🪐 DialogFlow agent and intents design - Good practices

A good practice is to have a shared context between all the intents of interest within your agent, so that all variables (entities & parameters) are easily retrieved.

If you are using parent-child contexts, a good tip is to give a big lifespan to the first context, so that it is present trhoughout the call interaction. This way, you make sure that all the parameters retrieved will be stored in this context:

CVP General Diagram CVP General Diagram

Once you have all the data that you need, you can invoke the Fulfillment layer by turning on the switch in the specified intent:

CVP General Diagram

You also need to enable Fulfillment in your agent through the following screen:

CVP General Diagram

🪐 Middleware Data retrieval from DialogFlow

From now onwards, we will refer to the scripts my_intent_X.js located in the /intent-handlers/ folder of this repo. These are biolerplates for modular intent handling.

In order to extract your DialogFlow parameters, adjust the following code snippet:

    let MY_PARAM_1,MY_PARAM_2,MY_PARAM_3 = ''
    agent.contexts.forEach(
        element => {
            if (element.name == 'session_data') {
                MY_PARAM_1 = element.parameters.myparam1;
                MY_PARAM_2 = element.parameters.myparam2;
                MY_PARAM_3 = element.parameters.myparam3;
                ///...
            }
        }
    );

The non-expired contexts are stored in the agent.contexts dictionary within the agent instance. IMPORTANT! The parameter's names are case sensitive. They must match those defined in the Web platform.

🪐 Middleware Data retrieval from Cisco CVP

All variables defined in the DialogFlow block in CallStudio (later deployed in CVA) are retrieved from the originalRequest.payload portion of the agent instance.

If there are no variables passed (for example, when the agent is invoked from another source different from a phone call, like a chat agent), the originalRequest.payload portion will be undefined.

Adjust the following code snippet for gathering your variables, as passed from Cisco CVP:

    let SucSuscriptor = agent.originalRequest.payload.payload; 
    if (SucSuscriptor === undefined) {
        // No variables were passed from Cisco CVP, or the intent was invoked via a different source than a phone call, such as a chat agent
    }

🪐 Sending data back from the Middleware to Cisco CVP

It is possible to send the data retrieved back to Cisco CVP for further call processing, or a flag value indicating the success or failure of the API operation in order to continue with the call flow accordingly.

A custom context must be created and assigned to the agent instance. Within the context, it is possible to define several parameters as required.

The following example creates a context named return_context and assigns the parameters myvalue and myaction with their corresponding values:

             let contextSubs = new Object();
             contextSubs = {
                 name: 'return_context',
                 lifespan: 3,
                 parameters: {
                     myvalue : '<my_retrieved_value_from_api>',
                     myaction: '<my_custom_flag>'
                 }
             };
             agent.context.set(contextSubs);

This is later gathered in Cisco CVP with a Set Variable block, as mentioned is this repo's Wiki entry.

🪐 Trigger an event within DialogFlow

The setFollowupEvent method triggers a specific intent within the agent. The intent to trigger must have the tag specified in the "name" parameter of this function within the "event" field in the Web platform.

Additionally, this triggered intent can render dynamic text. It is necessary to create the parameter in the return object (as shown below in the "mymessage" parameter), and invoke it as a variable with the "$mymessage" notation in the "Text or SSML Response" field instead of a fixed text.

CVP General Diagram CVP General Diagram

🪐 Plug-n-play: Add a new block to the Web server

Once your intent handler is ready, it is necessary to refer to the handler.js file and add the corresponding references. First of all, add your new script as a module in the following code snippet:

const MY_INTENT_A = require('./intent-handlers/my_intent_A');
...

Then, add the mapping to your intent in the following code snippet:

    ...
    intentMap.set('name_of_my_intent_A_in_dialogflow', MY_INTENT_A);
    ...

It is very important that the string in the first part of this function matches the name of your intent in DialogFlow. This value is case sensitive! Now, your new handler is enabled to operate and manage all POST requests with data from different sources.