Bot to Bot communication using REST Endpoints – Cognigy.AI Help Center

Bot to Bot communication using REST Endpoints

Cognigy Support
March 24, 2022

This article explains how to create a REST based agent to agent interaction that enables the user to switch to a different agent mid-conversation.

Concept: User > Proxy Agent > Target Agent

How to setup:

Step 1: Create a REST endpoint in the Target Agent

Step 2: In your Proxy Agent Create a new flow

Step 3: Create a Code node that generates random session details

Example code:

context.userId = `USERID${Math.floor(Math.random()*100000000)}`
context.sessionId = `SESSIONID${Math.floor(Math.random()*100000000)}`
context.URLToken = `URLTOKEN${Math.floor(Math.random()*100000000)}`

Step 4: Create an HTTP Request node

Example payload:

{
    "userId": "{{context.userId}}",
    "sessionId": "{{context.sessionId}}",
    "URLToken": "{{context.URLToken}}",
    "text": "{{input.text}}",
    "data": {}
}

Step 5: Create a Code node that delivers the output stack

Example payload:

if (input.httprequest?.result?.outputStack) {
    for (let output of input.httprequest.result.outputStack) {
        actions.output(output.text, output.data);
    }
}

Each time an input message is received from the end users, it is forwarded to the REST endpoint for the target virtual agent. This target agent, creates the output that is returned as a response to the REST call and that response is used as an answer in the current session.

NB: There is a max timeout of 8 seconds when using HTTP REST, so refrain from using sleep nodes in your target flow.

Here is a package containing this example flow. Feel free to use this as a starting point for your own project by importing via the packaging feature:

Here is what it could look like: