Transform Virtual Agent API request and response
You can transform Virtual Agent API request and response into supported formats through the scripted extension points provided in Virtual Agent API.
Before you begin
Role required: admin
Procedure
-
Navigate to All > System Extensions Points > Scripted Extension Points
- The sn_va_as_service.va_api_payload_transformation extension point transforms primary bot payload into standard Virtual Agent API supported format before processing the request. The sn_va_as_service.va_api_output_transformation extension point transforms Virtual Agent API response into a format supported by the primary bot.
- Both the scripted extension points have two parameters: shouldTransform and transform. shouldTransform is used to determine whether the implementation is executed. transform is used to transform the request or response.
-
If you want to transform the request payload, complete the following steps.
- Select sn_va_as_service.va_api_payload_transformation extension point
- In the Related Links section, select Create implementation.
-
Update the extension point script according to your requirements.
In sn_va_as_service.va_api_payload_transformation extension point, the request parameter is an object with two keys: payload and headers. payload is the request payload received from the primary bot, and headers are the headers sent in the request payload. The following is a sample implementation.
var va_api_payload_transformation = Class.create(); va_api_payload_transformation.prototype = { initialize: function() {}, shouldTransform: function(request) { return request.payload.caller === "examplebot"; }, /* return object for transform function should be in the following format transformedRequest = { payload: request.payload, headers: request.headers }; */transform: function(request) { var payload = request.payload; var headers = request.headers; var vaAPIPayload = { "userId": payload.userId, "emailId": payload.email, "message": { "text": payload.message, "typed": true }, "contextVariables": { "requester_session_language": payload.user_language }, "appInboundId": "custom_1" }; return { payload: vaAPIPayload, headers: headers }; },Figure 1. sn_va_as_service.va_api_payload_transformation extension point - Select Update.
-
If you want to transform the response payload, complete the following steps.
- Select sn_va_as_service.va_api_output_transformation extension point
- In the Related Links section, select Create implementation.
-
Update the extension point script according to your requirements.
In sn_va_as_service.va_api_output_transformation extension point, the response parameter is an object with two keys: inputPayload and responsePayload. inputPayload is the request payload received from Virtual Agent API, and responsePayload is the response from Virtual Agent API which you can use to build the transformed response. Use the appInboundId field of the request payload to determine whether to return true or false. The following is a sample implementation.
var va_api_output_transformation = Class.create(); va_api_output_transformation.prototype = { initialize: function() {}, shouldTransform: function(response) { return (response.inputPayload.appInboundId === 'custom_1'); }, /* input object param for transform function should be in the following format response = { inputPayload: response.inputPayload, responsePayload: response.responsePayload }; */ transform: function(response) { //return only transformed payload return { 'custom_structure': response.responsePayload }; }, type: 'va_api_output_transformation' };Figure 2. sn_va_as_service.va_api_output_transformation extension point - Select Update.