osonsms/gateway

A simple library for sending SMS via OsonSMS API.

2.0.7 2024-07-30 08:33 UTC

This package is auto-updated.

Last update: 2024-11-30 09:26:59 UTC


README

Latest Version on Packagist Total Downloads

Here you can find a short instruction of how to install and use this package.

Installation

You can install the package via composer:

composer require osonsms/gateway

Usage

Run following command to publish a migration file:

php artisan vendor:publish --provider="OsonSMS\OsonSMSService\OsonSmsServiceProvider" --tag="migrations"

Run php artisan migrate to create a necessary package table.

To create a config file in order to specify OsonSMS credentials run following command:

php artisan vendor:publish --provider="OsonSMS\OsonSMSService\OsonSmsServiceProvider" --tag="config"

Open config/osonsmsservice.php config file and specify following parameters:

  • login - Login from OsonSMS
  • pass_salt_hash - You pass salt hash string given after the registration, it is kind of api token
  • sender_name - SMS Sender Name assigned to you

Below you can find a console command created in Laravel in order to test OsonSMS service. Create a command using php artisan make:command TestOsonSMSServiceCommand in order to create a test command. Find it in the project folder and copy paste following code.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use JsonException;
use OsonSMS\OsonSMSService\OsonSmsService;
use RuntimeException;

class TestOsonSMSServiceCommand extends Command
{
    protected $signature = 'app:test-osonsms-service';
    public function __construct(private readonly OsonSmsService $osonSmsService) {
        parent::__construct();
    }
    public function handle(): void
    {
        try {
            // You need to provide senderName, phonenumber, message and txtId to sendSMS method in order to send SMS.
            $msgId = $this->osonSmsService->sendSMS(
                senderName: config('osonsmsservice.sender_name'),
                phonenumber: '927777777',
                message: 'Hello from Laravel. Your random code: ' . random_int(100, 1000),
                txnId: uniqid('test', true)
            );
            echo "SMS sent successfully with msg_id: $msgId" . PHP_EOL;
            // This is how you can check the sms status using msgId returned from sendSMS method.
            sleep(5); // Intentionally sleeping for 5 seconds in order to get the more accurate sms delivery status
            echo "SMS Status: " .  $this->osonSmsService->getSMSStatus($msgId) . PHP_EOL;
            // To get the balance of your account simply call getBalance() method
            echo "My Balance: " .  $this->osonSmsService->getBalance() . PHP_EOL;
        } catch (RuntimeException|JsonException $exception) {
            echo $exception->getMessage() . PHP_EOL;
        }
    }
}

Run php artisan app:test-osonsms-service in the terminal to see it in action, don't forget to run previous commands to publish migration file and create config file.

You can find the logs of your SMS in the table called osonsms_log.

If you have any further questions or recommendations, feel free to send us an email at info@osonsms.com.

Security

If you discover any security related issues, please email i@bakhtiyor.tj instead of using the issue tracker.

Credits