Skip to main content

Uniphore Customer Portal

Custom Services: Examples

This section presents several examples that demonstrate the value and versatility of Custom Services. Each example features a different node.js module and shows how required code components should be written.

Parsing XML

This example shows how to convert an XML string into a format that can be used by the X‑Platform (a JSON object). The xml2js module is invoked by calling the require function, and the return values are the prices of a list of items.

(function(context) {

    var parseString = require('xml2js').parseString;
 
    var xmlText="<items><item name='BookA'><price>10</price><count>1</count></item><item name='BookB'><price>20</price><count>4</count></item></items>";
q();
    parseString(xmlText, function(err, result) {
context.complete(200, result.items.item[0].price[0]);
 
    });
 
 
})(context); 
Converting Temperature

This example shows how to connect to a SOAP web service by invoking the soap node.js module. In this case, the service converts a temperature in Celsius to Fahrenheit, and the result returned is a simple single value.

(function(context) {
 
    var soap = require('soap');
 
soap.createClient("http://www.w3schools.com/webservices/tempconvert.asmx?wsdl", function(err, client) {
        client.CelsiusToFahrenheit({"Celsius ":context.params.celsius}, function(err, result){
context.complete(200, result); 
        });
    });
 
})(context);

Although this example is a simple one, it demonstrates the ability of a Custom Service to help achieve limitless business capabilities by utilizing complex SOAP services.

Getting Weather

In this more complex example, the input parameter is the zip code of a city in the United States, and the response values are the name of the city and the current temperature in the city. As the temperature is returned in degrees Kelvin, an additional calculation is written in the code so the final result is returned in degrees Fahrenheit.

The example is explained in detail below the figure.

201902029-GetWeatherExample.png
  • Line 3: The request-promise module is invoked by calling the require function.

  • Lines 5-6: The URL to call the postal code search is built. The input parameter name of postal is the name defined on the Request page of the New Custom Integration Point wizard.

  • Lines 8-10: The API is called, data is returned, and the name of the city is extracted from the returned data.

  • Line 11: The trace function is called to write the name of the city to the console.

  • Line 12: The URL to call the weather map is built. The returned city name is added to the URL.

  • Lines 13-15: The API is called and data is received.

  • Lines 16-18: An object is created to hold the returned values. The object contains the name of the city and the temperature (the returned temperature converted to degrees Fahrenheit).

  • Line 19: The complete function is called to end the request by returning HTTP status and the data object.

  • Lines 21-22: Error handling.