PHP code example of omerfayyaz / laravel-sms-gateway

1. Go to this page and download the library: Download omerfayyaz/laravel-sms-gateway 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/ */

    

omerfayyaz / laravel-sms-gateway example snippets


use OmerFayyaz\SmsGateway\Facades\SmsGateway;
use OmerFayyaz\SmsGateway\SmsMessage;

$message = SmsGateway::send('+923001234567', 'Your code is 482913');

$message->id;     // the gateway's id, for find() and webhook events
$message->status; // MessageStatus::Pending
$message->from;   // the number the gateway chose to send from

SmsGateway::send(
    SmsMessage::create('The mehndi starts at 7pm tomorrow.')
        ->to('+923001234567')
        ->from('+923454805588')                  // one of the app's numbers
        ->sendAt(now()->addHours(3))             // held on the gateway until then
        ->expiresAt(now()->addHours(5))          // dropped if not sent by then
        ->idempotencyKey("mehndi-reminder-{$event->id}-{$guest->id}"),
);

use OmerFayyaz\SmsGateway\SmsMessage;

class InvitationAccepted extends Notification implements ShouldQueue
{
    use Queueable;

    public function via(object $notifiable): array
    {
        return ['mail', 'sms-gateway'];
    }

    public function toSmsGateway(object $notifiable): SmsMessage|string
    {
        return "{$this->guest->name} accepted your invitation.";
    }
}

public function routeNotificationForSmsGateway(Notification $notification): ?string
{
    return $this->phone;
}

Notification::route('sms-gateway', '+923001234567')->notify(new InvitationAccepted($guest));

$message = SmsGateway::find($id); // null if the app has no message with this id

foreach (SmsGateway::messages(['status' => MessageStatus::Failed]) as $message) {
    // pages are fetched as you go, newest first
}

use OmerFayyaz\SmsGateway\Events\MessageReceived;

class RecordRsvpReply implements ShouldQueue
{
    public function handle(MessageReceived $event): void
    {
        Guest::where('phone', $event->message->from)->first()?->recordReply($event->message->content);
    }
}

SmsGateway::send(SmsMessage::create('Thanks, we got your reply.')
    ->to($event->message->from)
    ->idempotencyKey('reply-'.$event->eventId));

use OmerFayyaz\SmsGateway\Webhooks\VerifyWebhookSignature;
use OmerFayyaz\SmsGateway\Webhooks\WebhookController;

Route::post('hooks/sms', WebhookController::class)->middleware(VerifyWebhookSignature::class);

use OmerFayyaz\SmsGateway\Facades\SmsGateway;

$sms = SmsGateway::fake();

// ... code that sends, directly or by notification ...

$sms->assertSentTo('+923001234567', fn (SmsMessage $message) => str_contains($message->content, 'accepted'));
$sms->assertSentCount(1);
$sms->assertNothingSent();

use OmerFayyaz\SmsGateway\Webhooks\WebhookSignature;

$body = json_encode(['id' => 'evt_1', 'type' => 'message.received', 'created_at' => now()->toIso8601String(), 'data' => [/* message */]]);

$this->call('POST', '/sms-gateway/webhook', server: [
    'CONTENT_TYPE' => 'application/json',
    'HTTP_X_GATEWAY_SIGNATURE' => WebhookSignature::header($body, config('sms-gateway.webhook.secret')),
], content: $body)->assertNoContent();
bash
php artisan sms-gateway:send +923001234567
bash
php artisan vendor:publish --tag=sms-gateway-config