Data Action to Call POST API

I'm trying to create a data action because Architect doesn't expose calls in queue (or wait time). I have worked out wait time, but I'm having trouble creating and importing my data Action json for calls in queue.

Here is my working API call...

# /api/v2/analytics/queues/observations/query

body:

{
"filter": {
"type": "and",
"predicates": [
{
"type": "dimension",
"dimension": "queueId",
"operator": "matches",
"value": "e0a8ccf2-1bab-4bda-a5ba-blah blah"
}
]
},
"metrics": [
"oWaiting"
]
}

Here is my data action json...

{
  "name": "Get Calls In Queue",
  "integrationType": "purecloud-data-actions",
  "actionType": "custom",
  "config": {
    "request": {
	  "requestUrlTemplate": "/api/v2/analytics/queues/observations/query",
      "requestType": "POST",
	  "body": {
		"filter": {
		"type": "and",
		"predicates": [
		{
		"type": "dimension",
		"dimension": "queueId",
		"operator": "matches",
		"value": "${input.QUEUE_ID}"
		}
		]
		},
		"metrics": [
		"oWaiting"
		]
		},
      "headers": {
        "UserAgent": "PureCloudIntegrations/1.0",
        "Content-Type": "application/x-www-form-urlencoded"
      },
      "requestTemplate": "${input.rawRequest}"
    },
    "response": {
      "translationMap": {
		"contacts_in_queue": "$.results[0]"

      },
      "translationMapDefaults": {},
      "successTemplate": "{\n \"contacts_in_queue\": ${contacts_in_queue}\n}"
    }
  },
  "contract": {
    "input": {
      "inputSchema": {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "title": "Contacts in queue Request",
        "description": "Contacts in queue media type and queue.",
        "type": "object",
        "required": [
          "QUEUE_ID"
        ],
        "properties": {
          "QUEUE_ID": {
            "description": "The queue ID.",
            "type": "string"
          }
        },
        "additionalProperties": true
      }
    },
    "output": {
      "successSchema": {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "title": "Get contacts in queue Response",
        "description": "Returns the number of contacts in queue.",
        "type": "object",
        "properties": {
          "contacts_in_queue": {
            "description": "The number of contacts in queue for the specified media type and queue.",
            "type": "integer"
          }
        },
        "additionalProperties": true
      }
    }
  },
  "secure": false

}

The json imports fine and passes tests right down to "Execute'.

I think it's the "body" array. Because the error when testing/validating the data action is...

* REST call for action execute failed. Message:Request to backend service failed. Response from web service: {"message":"HTTP 415 Unsupported Media Type","code":"unsupported media type","status":415,"contextId":"xxx-1106-4a50-8e1e-xxx","details":[],"errors":[]} [xxx-c474-4a5f-a55a-xxx]

Should I be using escaped body as requestTemplate or requestBodyTemplate?

e.g.

"requestType": "POST", "requestBodyTemplate": "{\"filter\": {\"type\": \"and\",\"predicates\......."

etc

I got past the execution and am now hung up on the output contract. This has been a handful. The escaping is driving me nuts.

This is how I got the request template working...
"requestTemplate" : "{\"filter\": {\"type\": \"and\",\"predicates\": [{\"type\": \"dimension\",\"dimension\": \"queueId\",\"operator\": \"matches\",\"value\": \"${input.QUEUE_ID}\"},{\"type\": \"dimension\",\"dimension\": \"mediaType\",\"operator\": \"matches\",\"value\": \"voice\"} ]}, \"metrics\": [\"oWaiting\"] }",

I just need to work out how to surface the count response for voice and callback in the output...

Here is the API response...

{
"group": {
"queueId": "e0a8ccf2-1bab-4bda-a5ba-xxx",
"mediaType": "callback"
},
"data": [
{
"metric": "oWaiting",
"stats": {
"count": 0
}
}
]
},
{
"group": {
"queueId": "e0a8ccf2-1bab-4bda-a5ba-xxx",
"mediaType": "voice"
},
"data": [
{
"metric": "oWaiting",
"stats": {
"count": 0
}
}
]
}

OK I got this working too. Though I would like some help with how to transpose a json object that has a list in it. The above example includes a list of media types "callback", "voice". Depending on the filter, the list will have one or more values (e.g. email, chat etc). How can I use json transform to appropriately read in the list items?

Currently I'm doing this...

{
"translationMap": {
"contacts_in_queue": "$.results[0].data[0].stats.count"
},
"translationMapDefaults": {},
"successTemplate": "{\n "contacts_in_queue": ${contacts_in_queue}\n}"
}

Which just refers to the first item in a list, sublist. That's fine if your filter guarantees a one item list. But I really need to be walking the list, collecting (in this case) the call count integer and presenting it in a format than can be consumed by architect. e.g. I'd like to see an output for each media type that was returned in the search query. Even if it can't be dynamic, and I'd have to define all media types beforehand, I still am not sure how to iterate through the json to flatten the output.

Any tips for walking/iterating through lists in data actions?

Hello,

Sorry for the delay. I was off last week and didn't notice that your post was still pending an answer.

You can use the following with or without a filter on mediaType.
I mean that if you add a filter on a certain mediaType, you can keep the Input Contract with variables for each media. They will return 0 for the mediaTypes which are not included.

The principle here is to use a JSONPath expression to look for an entry with a specific property value in the results array (JSONPath is supported in Data Actions). Something that checks the mediaType value.
One important thing to mention is that the JSONPath expression will return an array (with a single value in it in this specific scenario).
That's why I am using a Velocity macro - successTemplateUtils.firstFromArray to extract the integer value from the array.

For the data, you could use data[0] to access the oWaiting count. But I have also defined a JSONPath expression there to look for the oWaiting metric (in case later on you want to request oWaiting and oInteracting metrics - as an example).

My Input Contract:

{
  "type": "object",
  "properties": {
    "QUEUE_ID": {
      "type": "string"
    }
  },
  "additionalProperties": true
}

My Output Contract:

{
  "type": "object",
  "properties": {
    "nbWaitingCalls": {
      "type": "integer"
    },
    "nbWaitingChats": {
      "type": "integer"
    },
    "nbWaitingCallbacks": {
      "type": "integer"
    },
    "nbWaitingEmails": {
      "type": "integer"
    },
    "nbWaitingMessages": {
      "type": "integer"
    }
  },
  "additionalProperties": true
}

My Request Configuration:

{
  "requestUrlTemplate": "/api/v2/analytics/queues/observations/query",
  "requestType": "POST",
  "headers": {
    "Content-Type": "application/json"
  },
  "requestTemplate": "{\n \"filter\": {\n \"type\": \"and\",\n \"predicates\": [\n {\n \"type\": \"dimension\",\n \"dimension\": \"queueId\",\n \"operator\": \"matches\",\n \"value\": \"${input.QUEUE_ID}\"\n }\n ]\n },\n \"metrics\": [\n \"oWaiting\"\n ]\n}"
}

My Response Configuration:

{
  "translationMap": {
    "oNbWaitingCalls": "$.results[?(@.group.mediaType=='voice')].data[?(@.metric=='oWaiting')].stats.count",
    "oNbWaitingChats": "$.results[?(@.group.mediaType=='chat')].data[?(@.metric=='oWaiting')].stats.count",
    "oNbWaitingCallbacks": "$.results[?(@.group.mediaType=='callback')].data[?(@.metric=='oWaiting')].stats.count",
    "oNbWaitingEmails": "$.results[?(@.group.mediaType=='email')].data[?(@.metric=='oWaiting')].stats.count",
    "oNbWaitingMessages": "$.results[?(@.group.mediaType=='message')].data[?(@.metric=='oWaiting')].stats.count"
  },
  "translationMapDefaults": {
    "oNbWaitingCalls": "[]",
    "oNbWaitingChats": "[]",
    "oNbWaitingCallbacks": "[]",
    "oNbWaitingEmails": "[]",
    "oNbWaitingMessages": "[]"
  },
  "successTemplate": "{\"nbWaitingCalls\": ${successTemplateUtils.firstFromArray(\"${oNbWaitingCalls}\",\"0\")},\"nbWaitingChats\": ${successTemplateUtils.firstFromArray(\"${oNbWaitingChats}\",\"0\")},\"nbWaitingCallbacks\": ${successTemplateUtils.firstFromArray(\"${oNbWaitingCallbacks}\",\"0\")},\"nbWaitingEmails\": ${successTemplateUtils.firstFromArray(\"${oNbWaitingEmails}\",\"0\")},\"nbWaitingMessages\": ${successTemplateUtils.firstFromArray(\"${oNbWaitingMessages}\",\"0\")}}"
}

Regards,

2 Likes

This is very useful thanks! Will give it a go :slight_smile:

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.