PHP code example of kasunsampath / laravel-dialog-esms

1. Go to this page and download the library: Download kasunsampath/laravel-dialog-esms 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/ */

    

kasunsampath / laravel-dialog-esms example snippets


use KasunSampath\DialogEsms\Facades\DialogEsms;

DialogEsms::send('0772345678', 'Your verification code is 4821');

use KasunSampath\DialogEsms\Facades\DialogEsms;

$result = DialogEsms::send('0772345678', 'Your code is 4821');

$result->successful;   // true
$result->recipient;    // "94772345678" — normalised
$result->reference;    // local reference; Dialog gives you nothing to store

DialogEsms::usingSender('OTHERMASK')->send('0772345678', 'Hello');

$result = DialogEsms::sendBulk(
    ['0772345678', '0771234567', '0112345678'],
    'Scheduled maintenance tonight at 11pm.',
);

$result->acceptedCount();  // 2
$result->invalid;          // ['0112345678'] — a landline, filtered locally
$result->successful();     // true

$estimate = DialogEsms::estimate($message, recipients: 10_000);

$estimate->encoding;            // Encoding::Ucs2
$estimate->segments;            // 3
$estimate->billableMessages;    // 30000
$estimate->summary();           // "UCS-2 (Unicode), 200 chars, 3 segments x 10000 recipients = 30000 billable messages"

$estimate = DialogEsms::estimate("Don’t miss out — sale ends Friday");

$estimate->isAccidentallyUnicode();  // true
$estimate->nonGsmCharacters;         // ['’', '—']

$balance = DialogEsms::balance();

$balance->available;        // true
$balance->amount;           // 1234.56
$balance->formatted();      // "LKR 1,234.56"
$balance->isBelow(500.0);   // alerting threshold

DialogEsms::validate('0772345678');   // true
DialogEsms::validate('0112345678');   // false — landline, eSMS cannot deliver

use KasunSampath\DialogEsms\Facades\DialogEsms;

$campaign = DialogEsms::campaign('October flash sale')
    ->message('Sale ends Friday. Reply STOP to unsubscribe.')
    ->to($numbers)
    ->promotional()
    ->dispatch();

DialogEsms::campaign('October flash sale')
    ->message($body)
    ->to($numbers)
    ->promotional()
    ->estimate();     // recipient count already reflects opt-outs and invalid numbers

DialogEsms::send($phone, $body, ['message_type' => MessageType::Promotional]);

use KasunSampath\DialogEsms\Models\OptOut;

OptOut::add('0772345678', reason: 'replied STOP');
OptOut::add('0772345678', scope: 'newsletter');   // this list only
OptOut::remove('0772345678');

OptOut::has('+94772345678');   // true — matching is on the normalised number

$campaign->suppressed_count;     // 143
$campaign->suppressed_numbers;   // the actual list, for answering "why didn't X get it?"
$campaign->invalid_count;        // separate: malformed numbers and landlines

$campaign->report();
// ['delivered' => 8912, 'failed' => 47, 'awaiting_receipt' => 1041, 'delivery_rate' => 89.1, ...]

$campaign->cancel();

use KasunSampath\DialogEsms\Models\SmsTemplate;

SmsTemplate::create([
    'name' => 'otp',
    'label' => 'OTP verification',
    'template' => 'Welcome to {app_name}, {code} is your verification code',
    'type' => MessageType::Transactional,
    'variables' => ['app_name', 'code'],
]);

SmsTemplate::named('otp')->render(['app_name' => 'Jothishya', 'code' => '4821']);

DialogEsms::campaign('Weekly offers')->template('weekly_offers', ['name' => $name])->to($numbers)->dispatch();

SmsTemplate::named('order_confirmed')->estimate(['ref' => $ref], recipients: 500);

use KasunSampath\DialogEsms\Exceptions\DialogEsmsException;
use KasunSampath\DialogEsms\Enums\ResponseCode;

try {
    DialogEsms::send($phone, $message);
} catch (DialogEsmsException $e) {
    if ($e->isBillingIssue()) {
        // Out of credit. Alert finance — retrying will not help.
    }

    if ($e->responseCode === ResponseCode::InvalidKey) {
        // Wrong esmsqk value — or a misspelled parameter name.
    }

    if ($e->isRetryable()) {
        // Transient. Worth another attempt.
    }
}

$e->responseCode?->isConfigurationIssue();  // true for 2012, 2007, 2006
$e->isBillingIssue();                       // false — topping up will not help

use KasunSampath\DialogEsms\Models\SmsWebhook;

SmsWebhook::uncorrelated()->latest()->get();

use KasunSampath\DialogEsms\Events\{SmsSent, SmsFailed, SmsDelivered, ReceiptReceived};

Event::listen(function (SmsFailed $event) {
    if ($event->exception->isBillingIssue()) {
        Notification::route('mail', '[email protected]')->notify(new SmsCreditExhausted());
    }
});

use KasunSampath\DialogEsms\Notifications\DialogEsmsMessage;

class VerificationCode extends Notification
{
    public function __construct(private string $code) {}

    public function via($notifiable): array
    {
        return ['dialog-esms'];
    }

    public function toDialogEsms($notifiable): DialogEsmsMessage
    {
        return DialogEsmsMessage::make("Your code is {$this->code}");
    }
}

class User extends Authenticatable
{
    public function routeNotificationForDialogEsms(): string
    {
        return $this->mobile;
    }
}

use KasunSampath\DialogEsms\Facades\DialogEsms;

public function test_it_sends_a_verification_code(): void
{
    $fake = DialogEsms::fake();

    $this->post('/register', ['mobile' => '0772345678']);

    $fake->assertSentTo('0772345678', 'Your code is');
    $fake->assertSentCount(1);
}

DialogEsms::fake()->shouldFailWith(ResponseCode::InsufficientBalance);

use KasunSampath\DialogEsms\Models\SmsLog;

SmsLog::delivered()->recent(7)->count();
SmsLog::failed()->recent(30)->get();

// Sent, but no receipt ever came back. A growing number here usually means
// the push URL is unreachable, not that messages are failing.
SmsLog::awaitingReceipt()->where('created_at', '<', now()->subHours(6))->get();
bash
php artisan vendor:publish --tag=dialog-esms-config
bash
php artisan vendor:publish --tag=dialog-esms-migrations
bash
php artisan migrate
bash
php artisan dialog-esms:balance
bash
php artisan dialog-esms:test 0772345678
bash
php artisan dialog-esms:estimate "Sale ends Friday" --recipients=5000 --rate=0.35
dotenv
DIALOG_ESMS_QUIET_START=21:00
DIALOG_ESMS_QUIET_END=08:00
DIALOG_ESMS_TIMEZONE=Asia/Colombo