Creating a Custom Attribute
How to create your own attributes
There ar\e two ways to create a custom attribute. You can create one through the interface or do it entirely programmatically.

Through the interface

Visit admin/dynamic-attributes and create a new attribute selecting one of the existing types.
Creating a custom attribute
Dynamic Attributes
The user interface has yet to be updated to match the 1.x style but the functionality will remain the same πŸ™‚.
Now, that you have a custom attribute you can refer to it through the Conversation Designer
Using custom attributes

Programmatically

To programmatically create an attribute you will need to provide an Attribute implementation.
Before starting an attribute ensure that you publish the AttributeEngine configurations to the main OpenDialog application.
From the root of your app call:
1
php artisan vendor:publish --provider="OpenDialogAi\AttributeEngine\AttributeEngineServiceProvider"
Copied!
This will publish the Attribute config file attribute_engine in config\opendialog .
Let's have a look at the config file.
attribute_engine.php
1
<?php
2
​
3
return [
4
/**
5
* Register your application specific attribute types here. They should be registered as:
6
* [
7
* Fully/Qualified/ClassName,
8
* Fully/Qualified/ClassName2,
9
* ...
10
* ]
11
*
12
* Where ClassName is an implementation of @see \OpenDialogAi\AttributeEngine\Attributes\AttributeInterface
13
*/
14
'custom_attribute_types' => [
15
// \OpenDialogAi\AttributeEngine\Tests\ExampleCustomAttributeType::class
16
],
17
​
18
/**
19
* Register your application specific attributes here. They should be registered with:
20
* {attribute_name} => Fully/Qualified/ClassName
21
*
22
* Where ClassName is an implementation of @see \OpenDialogAi\AttributeEngine\Attributes\AttributeInterface
23
*/
24
'custom_attributes' => [
25
// 'attribute_name' => AttributeTypeClass::class
26
],
27
];
28
Copied!
There are two types of things we can register.

Custom attribute types

A custom attribute type is useful if you are introducing a type not already supported. You can see in the AttributeEngine config that we already support the following:
1
'supported_attribute_types' => [
2
BooleanAttribute::class,
3
FloatAttribute::class,
4
IntAttribute::class,
5
StringAttribute::class,
6
TimestampAttribute::class,
7
UserAttribute::class,
8
UtteranceAttribute::class,
9
FormDataAttribute::class,
10
ArrayDataAttribute::class,
11
UserHistoryRecord::class,
12
BasicCompositeAttribute::class,
13
],
Copied!
You can use a custom type to finely control how the attribute is transformed to a string when displayed in messages or how it is serialised for storage.
Here is an example of a custom attribute type
1
<?php
2
​
3
​
4
namespace App\Bot\Attributes;
5
​
6
use OpenDialogAi\AttributeEngine\Attributes\StringAttribute;
7
use OpenDialogAi\Core\Components\ODComponentTypes;
8
​
9
/**
10
* ExperienceAttribute implementation.
11
*/
12
class ExperienceAttribute extends StringAttribute
13
{
14
protected static string $componentId = 'attribute.app.experience';
15
protected static string $componentSource = ODComponentTypes::APP_COMPONENT_SOURCE;
16
}
17
​
Copied!
It is stored in our application namespace.
We can then register it in the config\opendialog\attribute_engine.php config file.
1
'custom_attribute_types' => [
2
// \OpenDialogAi\AttributeEngine\Tests\ExampleCustomAttributeType::class
3
\App\Bot\Attributes\ExperienceAttribute::class
4
],
Copied!

Custom Attribute

A custom attribute can use an existing type or a custom attribute type.
For example, to register a custom attribute that uses the type above we can add it to our configuration.
1
'custom_attributes' => [
2
// 'attribute_name' => AttributeTypeClass::class
3
'experience_from_code' => \App\Bot\Attributes\ExperienceAttribute::class
4
],
Copied!
Now, when we try to access attributes we will see our new attribute show up!
​
Last modified 5mo ago