PHP code example of squareetlabs / laravel-smsup

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

    

squareetlabs / laravel-smsup example snippets


SquareetLabs\LaravelSmsUp\SmsUpServiceProvider::class,

'SmsUp' => SquareetLabs\LaravelSmsUp\Facades\SmsUp::class,

return [   
    ...
    ...
    'smsUp' => [
         'key' => env('SMSUP_KEY'),
         'test_mode' => env('SMSUP_TEST_MODE') // true or false
    ]
    ...

public function via(INotifiable $notifiable)
{
    return ['smsUp'];
}

use SquareetLabs\LaravelSmsUp\SmsUpMessage;
...
public function toSmsUp(INotifiable $notifiable)
{
    $message = new SmsUpMessage();
    $message->to('34xxxxxxxxx') 
        ->from('Foo')
        ->text('Text of your message')
        ->custom('MyMsgID-12345') // Optional. 
        ->link('http://www.google.com'); // Optional

    return $message;
}

/**
 * Route notifications for the SmsUp channel.
 *
 * @return string
 */
public function routeNotificationForSmsUp(): string
{
    return $this->phone;
}

->text('Hi John! See our new offers only available for you: {LINK}');

use SquareetLabs\LaravelSmsUp\SmsUpMessage;
use SquareetLabs\LaravelSmsUp\Facades\SmsUp;
...
$message1 = new SmsUpMessage();
$message->to('34xxxxxxxxx') 
    ->from('Foo')
    ->text('Text of your message')
    ->custom('MyMsgID-12345') // Optional. 
    ->link('http://www.google.com'); // Optional
$message2 = new SmsUpMessage();
$message2->to('34xxxxxxxxx') 
    ->from('Foo')
    ->text('Text of your message')
    ->custom('MyMsgID-12346') // Optional. 
    ->link('http://www.google.com'); // Optional
$messages = [
    $message1->formatData(),
    $message2->formatData()
];
SmsUp::sendMessages($messages);

use SquareetLabs\LaravelSmsUp\Facades\SmsUp;
...
$balance = SmsUp::getBalance();

use SquareetLabs\LaravelSmsUp\Facades\SmsUp;
...
$verify = SmsUp::verifyPhone('34xxxxxxxxx');

use SquareetLabs\LaravelSmsUp\Events\SmsUpMessageWasSent;
use SquareetLabs\LaravelSmsUp\SmsUpMessage;
use SquareetLabs\LaravelSmsUp\SmsUpResponse;
use SquareetLabs\LaravelSmsUp\SmsUpResponseMessage;

class SmsUpMessageSentListener
{
    /**
     * Handle the event.
     *
     * @param  SmsUpMessageWasSent  $event
     * @return void
     */
    public function handle(SmsUpMessageWasSent $event)
    {
        $response = $event->response; // Class SmsUpResponse
        $message = $event->message; // Class SmsUpMessage

        if ($response->getStatus() != 'ok') {
            $yourModel = YourModel::find($message->getCustom());
            $yourModel->sms_status = $response->getStatus();
            $yourModel->sms_error_id = $response->getErrorId();
            $yourModel->sms_error_msg = $response->getErrorMsg();
            $yourModel->save();
        } else {
            foreach ($response->getResult() as $responseMessage) { // class SmsUpResponseMessage
                $yourModel = YourModel::find($responseMessage->getCustom());
                $yourModel->sms_status = $responseMessage->getStatus();
                $yourModel->sms_id = $responseMessage->getSmsId();
                $yourModel->sms_error_id = $responseMessage->getErrorId();
                $yourModel->sms_error_msg = $responseMessage->getErrorMsg();
                $yourModel->save();
            }
        }
    }
}

protected $listen = [
        ...
        'SquareetLabs\LaravelSmsUp\Events\SmsUpMessageWasSent' => [
            'App\Listeners\SmsUpMessageSentListener',
        ],
    ];

use SquareetLabs\LaravelSmsUp\Events\SmsUpReportWasReceived;
use SquareetLabs\LaravelSmsUp\SmsUpReportResponse;
use SquareetLabs\LaravelSmsUp\SmsUpReportResponseMessage;

class SmsUpReportReceivedListener
{
    /**
     * Handle the event.
     *
     * @param  SmsUpReportWasReceived  $event
     * @return void
     */
    public function handle(SmsUpReportWasReceived $event)
    {
        $response = $event->response; // Class SmsUpReportResponse
        
        foreach ($response->getResponseMessages() as $responseMessage) { // Class SmsUpReportResponseMessage
            $yourModel = YourModel::find($responseMessage->getCustom());
            $yourModel->sms_status = $responseMessage->getStatus();
            $yourModel->sms_delivery_at = $responseMessage->getDlrDate();
            $yourModel->save();
        }
    }
}

protected $listen = [
        ...
        'SquareetLabs\LaravelSmsUp\Events\SmsUpReportWasReceived' => [
            'App\Listeners\SmsUpReportReceivedListener',
        ],
    ];
bash
php artisan make:notification someNotification