Enneo allows you to add your own code in the form of “User Defined Functions” and use it in different areas. These functions can be used especially in the business logic of rule-based AI agents, in webhooks or event implementations - i.e. anywhere where your own code or SDKs are used.
Administration of User Defined Functions
User-defined functions are managed in the Integration of surrounding systems section in the settings. Any number of custom functions can be stored here.
Functions are saved as code fragments that implement specific application logic and can be called in various
integration scenarios.
Areas of Application
User Defined Functions offer a wide range of applications. Examples:
- System Interface Calls: For example for ERP, task management, or archive systems.
- Connecting Output Management Systems: For instance, for the dispatch of notifications.
- Outsourcing Complex Procedures: As reusable units.
With these functionalities, Enneo expands adaptability and allows the integration of specific company logic into AI-based customer service.
Calling User Defined Functions
Enneo can store and use user defined functions. The calls can be made as follows, based on a User-Defined-Function named ‘my-udf’:
Within Enneo using Python SDK:
sdk.ApiEnneo.executeUdf('my-udf', {"parameter1": "value1", "parameter2": "value2"}, 'serviceWorker')
Within Enneo using PHP SDK:
\EnneoSDK\ApiEnneo::executeUdf('my-udf', ['parameter1' => 'value1', 'parameter2' => 'value2'],'serviceWorker');
Via external web request:
POST https://your-subdomain.enneo.ai/api/mind/executor/execute/my-udf
Payload: '{"parameter1": "value1", "parameter2": "value2"}'
Storage object for User Defined Functions
An arbitrary JSON object can be used as an Enneo-internal storage object for user-defined functions. This allows data to be stored and retrieved using the Enneo SDK.
\EnneoSDK\Setting::set('udfStorage', ['my' => 'data']);
$storedData = \EnneoSDK\Setting::get('udfStorage');
Example: Calling an API of a third-party system
First, necessary data is defined in the storage object for user-defined functions:
{
"thirdParty": {
"oauth": {
"clientId": "123456789",
"grantType": "client_credentials",
"clientSecret": "abcdefg.....hijklmnop",
"tokenEndpoint": "https://token-url/oauth2/token",
"baseUrl": "https://third-party-url/api"
}
}
}
In the second step, a user-defined function named fetch-thirdparty-token can be implemented to load the credentials. This method is particularly useful when the loading of credentials can be reused in multiple other processes.
<?php
// Load Enneo SDK
require(getenv()['SDK']);
// Load storage object for user-defined functions
$udfStorage = \EnneoSDK\Setting::get('udfStorage');
// Load Token
$clientId = $udfStorage->thirdParty->oauth->clientId;
$clientSecret = $udfStorage->thirdParty->oauth->clientSecret;
$result = \EnneoSDK\Api::call(
method: 'POST',
url: $udfStorage->thirdParty->oauth->tokenEndpoint,
headers: [
'Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret),
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
],
params: http_build_query(['grant_type' => $udfStorage->thirdParty->oauth->grantType ?? 'client_credentials',
'scope' => $udfStorage->thirdParty->oauth->scope ?? null])
);
// Output is in JSON format and can be used after the call in the corresponding code
echo json_encode(['accessToken' => $result->access_token,
'accessTokenExpiresAt' => date("Y-m-d H:i:s T", time() + $result->expires_in),
"baseUrl" => $udfStorage->thirdParty->baseUrl,
"headers" => $udfStorage->thirdParty->headers ?? [],
"apiName" => $udfStorage->thirdParty->apiName ?? "Customer-API",
]);
Next, the function third-party-api-call can be implemented for the actual API call:
<?php
// Load SDK
require(getenv()['SDK']);
// Load Token using the previously defined "fetch-thirdparty-token" function
$credentials = \EnneoSDK\ApiEnneo::executeUdf('fetch-thirdparty-token', []);
// Load input parameters
$in = \EnneoSDK\Input::load();
// Prepare header for the request of the Third-Party API
$headers = [
'Authorization: Bearer '. $credentials->accessToken,
'Accept: application/json'
];
foreach (($cred->headers ?? []) as $key => $val) {
$headers[] = "$key: $val";
}
// Call API and output
$res = \EnneoSDK\Api::call(
method: $in->method,
url: $credentials->baseUrl.'/'.$in->api,
headers: $headers,
params: $in->params
);
// Output result
echo json_encode($res);
This structure allows flexible use and integration of custom-code within Enneo, to implement specific business logic or seamlessly connect external systems.
Invocation of Custom Functions
The invocation of custom functions in the code is done using executeUdf:
<?php
// Load Enneo SDK
require(getenv()['SDK']);
// Prepare payload for the API call of the third-party system
$payload = [
'ticket-id' => 123,
'key' => 'value'
];
// Invocation of the custom function "third-party-api-call"
// this function receives as input parameter an object:
// [
// 'method' => 'POST',
// 'api' => 'item',
// 'params' => [
// 'ticket-id' => 123,
// 'key' => 'value'
// ]
// ]
EnneoSDK\ApiEnneo::executeUdf(
'third-party-api-call',
['method' => 'POST', 'api' => 'item', 'params' => $payload]
);