PHP code example of mizbanha / laravel-sms

1. Go to this page and download the library: Download mizbanha/laravel-sms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mizbanha / laravel-sms example snippets


'tables' => [
    'gateways'          => 'sms_core_gateways',
    'templates'         => 'sms_core_templates',
    'template_gateways' => 'sms_core_template_gateways',
    'messages'          => 'sms_core_messages',
    'attempts'          => 'sms_core_attempts',
],

use Mizbanha\Sms\Models\SmsGateway;

SmsGateway::create([
    'key' => 'kavenegar-main',
    'label' => 'Kavenegar',
    'driver' => 'kavenegar',          // a key of config('laravel-sms.drivers')
    'sender' => '30001234',
    'priority' => 10,                 // lower goes first
])->forceFill([
    'credentials' => ['api_key' => '...'],   // encrypted at rest
    'is_enabled' => true,                    // gateways are created disabled
])->save();

use Mizbanha\Sms\Enums\CountryPolicy;

// Everywhere. The default; a gateway that ignores this behaves as it always did.
$gateway->country_policy = CountryPolicy::All;

// Only these countries.
$gateway->country_policy = CountryPolicy::Allow;
$gateway->countries = ['IR'];

// Everywhere except these. Twilio, for instance, documents that it does not
// deliver to Iran, Syria or Cuba.
$gateway->country_policy = CountryPolicy::Deny;
$gateway->countries = ['IR', 'SY', 'CU'];

use Mizbanha\Sms\Enums\DeliveryMode;
use Mizbanha\Sms\Models\SmsTemplate;
use Mizbanha\Sms\Models\SmsTemplateGateway;

$template = SmsTemplate::create([
    'key' => 'order-created',
    'name' => 'Order created',
    'body' => 'Hi {customer_name}, order {order_number} for {total} is placed.',
]);

SmsTemplateGateway::create([
    'sms_template_id' => $template->id,
    'sms_gateway_id' => $kavenegar->id,
    'mode' => DeliveryMode::Pattern,
    'pattern_code' => 'order-created',     // what THIS provider calls it
    // The parameters IN ORDER. Kavenegar numbers rather than names them, so the
    // position of an entry is what decides which token a value becomes.
    'parameter_map' => [
        ['provider' => 'token', 'variable' => 'customer_name'],
        ['provider' => 'token2', 'variable' => 'order_number'],
        ['provider' => 'token3', 'variable' => 'total'],
    ],
]);

SmsTemplateGateway::create([
    'sms_template_id' => $template->id,
    'sms_gateway_id' => $smsir->id,
    'mode' => DeliveryMode::Pattern,
    'pattern_code' => '100200',
    // The same message at a provider that names its parameters instead.
    'parameter_map' => [
        ['provider' => 'CUSTOMER', 'variable' => 'customer_name'],
        ['provider' => 'ORDER_NO', 'variable' => 'order_number'],
        ['provider' => 'AMOUNT', 'variable' => 'total'],
    ],
]);

'parameter_map' => [
    ['variable' => 'customer_name'],
    ['variable' => 'order_number'],
],

SmsTemplate::create([
    'key' => 'login-otp',
    'name' => 'Login code',
    'body' => 'Your login code is {code}.',
    'is_sensitive' => true,
]);

Sms::to($phone)->template('login-otp')->with([...])->sensitive()->send();

use Mizbanha\Sms\Facades\Otp;
use Mizbanha\Sms\Otp\OtpStatus;

$result = Otp::send($phone, 'login-otp');

match ($result->status) {
    OtpStatus::Sent       => 'ask for the code',
    OtpStatus::Cooldown   => "wait {$result->retryAfter}s",
    OtpStatus::Unknown    => 'it may have arrived; ask for the code',
    OtpStatus::Failed     => 'try again',
    OtpStatus::Suppressed => 'sending is off in this environment',
};

// The third argument is the PURPOSE, which defaults to the template key on send.
if (Otp::verify($phone, $typed, 'login-otp')) {
    // your application decides what that means
}

Otp::send($phone, 'login-otp', purpose: 'login');
Otp::send($phone, 'confirm-otp', purpose: 'withdrawal');

use Mizbanha\Sms\Facades\Sms;

Sms::to('09121234567')
    ->template('order-created')
    ->with([
        'customer_name' => 'Amid',
        'order_number' => 'CF-1204',
        'total' => '1,850,000',
    ])
    ->about($order)   // optional context, recorded and never interpreted
    ->queue();        // or ->send() to deliver during this request

Sms::to('09121234567')
    ->template('connectivity-check')
    ->with(['customer_name' => 'Amid'])
    ->viaGateway('kavenegar-main')   // a gateway key, or an SmsGateway
    ->send();

use Mizbanha\Sms\Enums\RoutingStrategy;
use Mizbanha\Sms\Models\SmsTemplate;

// A login code should start at the most reliable line, every single time.
SmsTemplate::where('key', 'login-otp')->first()
    ->update(['routing_strategy' => RoutingStrategy::Priority]);

// Order notifications are ordinary traffic worth spreading over the accounts
// you are paying for either way.
SmsTemplate::where('key', 'order-created')->first()
    ->update(['routing_strategy' => RoutingStrategy::WeightedRoundRobin]);

$template->gatewayBindings()->where('sms_gateway_id', $kavenegar->id)->update(['weight' => 5]);
$template->gatewayBindings()->where('sms_gateway_id', $smsir->id)->update(['weight' => 3]);
$template->gatewayBindings()->where('sms_gateway_id', $ippanel->id)->update(['weight' => 2]);

$breaker = app(Mizbanha\Sms\Health\CircuitBreaker::class);

$breaker->status($gateway);   // state (closed|open|half_open), failures, openUntil
$breaker->reset($gateway);    // clears the observation - and nothing else

Sms::refreshDelivery($message);   // or an SmsAttempt, when you know which handover

$message->delivery_status;        // null | pending | sent | delivered | failed | unknown
$message->delivery_confirmed_at;  // when WE learned it arrived - not the handset's clock

SmsGateway::create([
    'key' => 'twilio',
    'label' => 'Twilio',
    'driver' => 'twilio',
    'sender' => '+15551234567',        // -> From, in E.164
    'priority' => 50,
])->forceFill([
    'credentials' => ['account_sid' => 'AC...', 'auth_token' => '...'],
    'is_enabled' => true,
])->save();

$gateway->options = ['messaging_service_sid' => 'MG...'];

$gateway->options = ['parameter_separator' => ','];

$gateway->forceFill([
    'credentials' => ['api_key' => '...'],   // sent as a bare Authorization header
    'options' => ['url' => 'https://api.another-host.example/v1'],
])->save();

SmsGateway::create([
    'key' => 'iranpayamak',
    'label' => 'IranPayamak',
    'driver' => 'iranpayamak',
    'sender' => '2191307530',          // -> line_number, digits only
    'priority' => 90,
])->forceFill([
    'credentials' => ['api_key' => '...'],   // sent as the Api-Key header
    'is_enabled' => true,
])->save();

$gateway->options = ['number_format' => 'fa'];

use Mizbanha\Sms\Gateways\GatewayRegistry;

app(GatewayRegistry::class)->registered();               // ['log', 'kavenegar', …]
app(GatewayRegistry::class)->capabilitiesFor('twilio');  // [Capability::Text, Capability::DeliveryReport]
bash
composer vendor:publish --tag=laravel-sms-config
php artisan vendor:publish --tag=laravel-sms-migrations
php artisan migrate
bash
php artisan vendor:publish --provider="Mizbanha\Sms\SmsServiceProvider"
text
sms_gateways   sms_templates   sms_template_gateways   sms_messages   sms_attempts