PHP code example of openbackend / nepali-sms

1. Go to this page and download the library: Download openbackend/nepali-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/ */

    

openbackend / nepali-sms example snippets


return [
    'default' => env('NEPALI_SMS_DEFAULT_GATEWAY', 'sparrow'),
    
    'gateways' => [
        'sparrow' => [
            'driver' => 'sparrow',
            'token' => env('SPARROW_SMS_TOKEN'),
            'from' => env('SPARROW_SMS_FROM', 'SPARROW'),
        ],
        
        'sparrow_a2p' => [
            'driver' => 'sparrow_a2p',
            'client_id' => env('SPARROW_A2P_CLIENT_ID'),
            'client_secret' => env('SPARROW_A2P_CLIENT_SECRET'),
            'from' => env('SPARROW_A2P_FROM'),
        ],
        
        'ebeema' => [
            'driver' => 'ebeema',
            'username' => env('EBEEMA_USERNAME'),
            'password' => env('EBEEMA_PASSWORD'),
            'from' => env('EBEEMA_FROM'),
        ],
    ],
    
    'otp' => [
        'length' => 6,
        'expiry' => 300, // 5 minutes
        'template' => 'Your OTP code is: {otp}. Valid for {expiry} minutes.',
    ],
    
    'rate_limit' => [
        'enabled' => true,
        'max_attempts' => 5,
        'decay_minutes' => 1,
    ],
];

use OpenBackend\NepaliSms\Facades\NepaliSms;

// Send SMS using default gateway
NepaliSms::to('9841234567')
    ->message('Hello from Nepal!')
    ->send();

// Send SMS using specific gateway
NepaliSms::gateway('sparrow')
    ->to('9841234567')
    ->message('Hello from Sparrow!')
    ->send();

$recipients = ['9841234567', '9851234567', '9861234567'];

NepaliSms::to($recipients)
    ->message('Bulk message to all recipients')
    ->send();

// Generate and send OTP
$otp = NepaliSms::generateOtp('9841234567');

// Verify OTP
$isValid = NepaliSms::verifyOtp('9841234567', '123456');

if ($isValid) {
    // OTP is valid
} else {
    // Invalid or expired OTP
}

// Create a template
NepaliSms::createTemplate('welcome', 'Welcome {name} to our platform!');

// Send using template
NepaliSms::to('9841234567')
    ->template('welcome', ['name' => 'John'])
    ->send();

// Send SMS asynchronously
NepaliSms::to('9841234567')
    ->message('This will be sent via queue')
    ->queue();

// Send with delay
NepaliSms::to('9841234567')
    ->message('This will be sent after 5 minutes')
    ->delay(now()->addMinutes(5))
    ->queue();

php artisan make:notification WelcomeMessage

use OpenBackend\NepaliSms\Channels\NepaliSmsChannel;
use OpenBackend\NepaliSms\Messages\NepaliSmsMessage;

class WelcomeMessage extends Notification
{
    public function via($notifiable)
    {
        return [NepaliSmsChannel::class];
    }

    public function toNepaliSms($notifiable)
    {
        return NepaliSmsMessage::create()
            ->message('Welcome to our platform!')
            ->gateway('sparrow');
    }
}

$user->notify(new WelcomeMessage());

$message = NepaliSms::to('9841234567')
    ->message('Track this message')
    ->send();

// Check delivery status
$status = NepaliSms::getDeliveryStatus($message->id);

// Get detailed delivery report
$report = NepaliSms::getDeliveryReport($message->id);

// Get SMS statistics for today
$stats = NepaliSms::getStatistics();

// Get statistics for specific date range
$stats = NepaliSms::getStatistics(
    from: now()->subDays(7),
    to: now()
);

// Get gateway-wise statistics
$gatewayStats = NepaliSms::getGatewayStatistics('sparrow');

// Check if user can send SMS
if (NepaliSms::canSendSms('9841234567')) {
    NepaliSms::to('9841234567')
        ->message('Rate limited message')
        ->send();
} else {
    // Rate limit exceeded
}

// Configure multiple gateways with priority
NepaliSms::gateways(['sparrow', 'ebeema', 'easysms'])
    ->to('9841234567')
    ->message('This will try multiple gateways if one fails')
    ->send();

Event::listen(MessageSent::class, function ($event) {
    Log::info('SMS sent successfully', [
        'to' => $event->message->to,
        'gateway' => $event->message->gateway,
        'id' => $event->message->id,
    ]);
});
bash
php artisan vendor:publish --provider="OpenBackend\NepaliSms\NepaliSmsServiceProvider"
bash
php artisan migrate
bash
php artisan nepali-sms:test --gateway=sparrow --to=9841234567
bash
php artisan nepali-sms:otp --generate --phone=9841234567
bash
php artisan nepali-sms:otp --verify --phone=9841234567 --code=123456
bash
php artisan nepali-sms:stats
bash
php artisan nepali-sms:cleanup --days=30