Creating a Custom Context

Contexts store relevant information and make it available to the rest of a conversational application in the form of attributes.

There are three types of contexts in OpenDialog Core. Persistent contexts that are stored together with the user's conversational state, and non-persistent contexts that only last for the duration of a single interaction.

You can also create a completely custom context that handles its own storage needs. This is a great way to integrate with outside information sources.

Currently custom context are global (i.e. all users share the same information) - so they are useful to pull in information that the bot uses across all users. If you want to store information against a single user it is best to just use

In this section we setup a completely custom context.

First, we should ensure that we can register contexts through the context configuration.

php artisan vendor:publish --provider="OpenDialogAi\ContextEngine\ContextEngineServiceProvider"

This will publish the context configuration in config\opendialog\context_engine.php

context_engine.php
<?php
return [
/**
* Register your custom contexts here. Custom contexts must extend
* @see \OpenDialogAi\ContextEngine\Contexts\Custom\AbstractCustomContext
*
* Custom contexts are used to make available application specific attributes that are externally managed
*/
'custom_contexts' => [
// \OpenDialogAi\ContextEngine\tests\contexts\DummyCustomContext::class
]
];

Now in app\Bot\Contexts we will create an application context.

MyCustomContext.php
<?php
namespace App\Bot\Contexts;
use OpenDialogAi\AttributeEngine\Attributes\StringAttribute;
use OpenDialogAi\ContextEngine\Contexts\Custom\AbstractCustomContext;
class MyCustomContext extends AbstractCustomContext
{
public static string $componentId = 'my_custom_context';
public static ?string $componentName = 'MyCustomContext';
public function loadAttributes(): void
{
$this->addAttribute(new StringAttribute('lyric_one', 'Ga Ga'));
$this->addAttribute(new StringAttribute('lyric_two', 'Goo Goo'));
}
}

Finally, we should register the new Context and also the attributes.

For the attributes in config\opendialog\attribute_engine.php

'custom_attributes' => [
// 'attribute_name' => AttributeTypeClass::class
'lyric_one' => \OpenDialogAi\AttributeEngine\Attributes\StringAttribute::class,
'lyric_two' => \OpenDialogAi\AttributeEngine\Attributes\StringAttribute::class
],
];

Finally, in config\opendialog\context_engine.php

'custom_contexts' => [
// \OpenDialogAi\ContextEngine\tests\contexts\DummyCustomContext::class
\App\Bot\Contexts\GlobalContext::class,
\App\Bot\Contexts\MyCustomContext::class
]

With all of the above in place you are now able to use the attributes where attributes are possible - including messages.

<message disable_text="false" hide_avatar="false">
<text-message>All we hear is Radio {my_custom_context.lyric_one}.</text-message>
<text-message>Radio {my_custom_context.lyric_two}.</text-message>
</message>

Giving you an output such as: