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
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
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};
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);
}
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();