Skip to content

Docs ‐ Action

Isaac Sai edited this page Jan 20, 2024 · 1 revision

Introduction to Action

USSD action let you introduce branching into your application based on some conditions. When transitioning between state, there are times you may want to transition based on certain condition. Action let you do that easily.

Creating an action

php artisan ussd:action MakePaymentAction

You can use action as the first initial state by using the init option in the create command.

php artisan ussd:action MenuAction --init

Write your logic to return the next state or action.

<?php

namespace App\Ussd\Actions;

use App\Models\Customer;
use App\Ussd\States\CustomerMenuState;
use App\Ussd\States\GuestMenuState;
use Sparors\Ussd\Context;
use Sparors\Ussd\Contracts\InitialAction;

class MenuAction implements InitialAction
{
    public function execute(Context $context): string
    {
        $isRegistered = Customer::query()
            ->where('phone_number', $context->get('phone_number'))
            ->exists();

        return $isRegistered ? CustomerMenuState::class : GuestMenuState::class;
    }
}
Clone this wiki locally