Bot to Bot communication using REST Endpoints

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

  • Save the URL of the rest endpoint

Step 2: In your Proxy Agent Create a new flow 

  • This flow contains the nodes required to communicate with the target agent.
  • A routing agent will find the endpoint URL via Lexicon and forward the conversation to this flow to handle the interaction.
  • [For testing purposes, add a code node that adds your endpoint URL to the context]

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

  • UserId, sessionId, URLToken
  • These will be used to create a parallel session with the Target Agent
  • House this in a once node "on first time" so the same session details can be re-used for each message but are regenerated on a new session

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

  • This node will receive the endpoint URL for the Target Agent Rest Endpoint.
  • It will use the Random session creds from the previous code node.
  • Use "start" for the first message, then use {{input.text}} for further messages (handle with logic).
  • Don't forget to add a Cognigy API key to the Authentication params of the request.
  • Save the response to context.response to match code node example below.

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

  • This node accesses the response from the HTTP request with all of the messages

Example payload:

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

Here is what it could look like:

Capture.PNG

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:

 


Comments

0 comments

Please sign in to leave a comment.

Was this article helpful?
0 out of 0 found this helpful