Skip to content

Docs ‐ Context

Isaac Sai edited this page Dec 28, 2023 · 1 revision

Introduction to Context

The context of a USSD is the main pillar that a USSD application runs on. Although USSD is stateless, the context can provide us with enough information to identify the actor involved in a USSD section. There are 3 key values needed to create a USSD context:

UID: This refers to a unique value used to identify a USSD session. A USSD session starts when you dial a short code until it ends. There are occasions where the session may terminate before the USSD ends due to network errors. This is usually the session ID that comes from the USSD provider.

GID: This refers to a unique value used to identify the user using the USSD. This is normally the phone number of the user using the USSD application since phone numbers are unique and only linked to one particular user.

Input: USSD applications expect users to enter input except for the very first and very last view. These inputs are used to navigate the web of possible paths the user may pave. This should be the last input provided by the user after viewing a view.

Aside from these 3, a user may need to pass extra data provided by the telco which they may need during the running of their application. These 3 are used internally to ensure the smooth running of your application, in the event you may need to use a value you passed for any of these 3, you may need to pass it as an additional parameter so you can later retrieve them when needed.

Creating a Context

use Sparors\Ussd\Context;

// Creating a new context with the 3 required parameters
$context = Context::create(
    uid: request('sessionId'),
    gid: request('phoneNumber'),
    input: request('text')
);

// Passing extra parameters that will be needed
$context->with([
    'phone_number' => request('phoneNumber'),
    'network' => request('serviceCode')
]);
Clone this wiki locally