-
Notifications
You must be signed in to change notification settings - Fork 42
Docs ‐ Configurator
papamarfo edited this page Jul 30, 2026
·
3 revisions
When building USSD application, you may want to group related configuration to make them simple and easy to reuse. Configurators help us do that. For instance, you an have your configurator define the response class to use, the exception handler to use, the initial state to use and many more.
php artisan ussd:configurator AfricasTalkingConfiguratorImplement want configuration it should load.
<?php
namespace App\Ussd\Configurators;
use App\Ussd\BriefExceptionHandler;
use App\Ussd\VerboseExceptionHandler;
use Speso\Ussd\Context;
use Speso\Ussd\Contracts\Configurator;
use Speso\Ussd\Responses\AfricasTalkingResponse;
use Speso\Ussd\Ussd;
class AfricasTalkingConfigurator implements Configurator
{
public function configure(Ussd $ussd): void
{
$lastText = request('text') ?? '';
if (strlen($lastText) > 0) {
$lastText = explode('*', $lastText);
$lastText = end($lastText);
}
$ussd->useResponse(AfricasTalkingResponse::class)
->useContext(
Context::create(
request('sessionId'),
request('phoneNumber'),
$lastText
)->with([
'phone_number' => request('phoneNumber')
])
)->unless(
config('app.env') === 'production',
callback: function (Ussd $ussd) {
$ussd->useExceptionHandler(VerboseExceptionHandler::class);
},
fallback: function (Ussd $ussd) {
$ussd->useExceptionHandler(BriefExceptionHandler::class);
}
);
}
}