GithubHelp home page GithubHelp logo

laravelsmsupch's Introduction

SmsUp.ch API Integration for Laravel 8+

Installation

Install Package

You can install this package via composer:

composer require husnet/laravel-smsupch

Configuration

Add your SmsUp API key to your config/services.php file:

return [   
    ...
    ...
    'smsUpCh' => [
         'token' => env('SMSUPCH_TOKEN'),
         'simulate' => env('SMSUPCH_SIMULATE'), // true or false
         'sender' => env('SMSUPCH_SENDER_NAME', config('app.name')),
    ]
    ...
];

Set simulate to true if you want to simulate submitting messages, it's perfect for testing and debugging, it has no cost.

Usage

Using Laravel Notification

Use artisan to create a notification:

php artisan make:notification someNotification

Return [smsUpCh] in the public function via($notifiable) method of your notification:

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

Add the method public function toSmsUp($notifiable) to your notification, and return an instance of SmsUpChMessage:

use Husnet\LaravelSmsUpCh\SmsUpChMessage;
...
public function toSmsUpCh($notifiable)
{
    $message = new SmsUpChMessage();
    $message->text($this->text);
    return $message;
}

If you don't indicate the parameter to, make sure your notifiable entity has routeNotificationForSmsUpCh method defined:

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

Using SmsUp Facade

Send messages

use Husnet\LaravelSmsUpCh\SmsUpChMessage;
use Husnet\LaravelSmsUpCh\Facades\SmsUpCh;
...
    try {
        $phone_owner->notify(new ClientNotification($message_text));
    }
    catch (\Exception $e) {
        session()->flash('error', __('Error: SMS API Timeout'));
    }

Available Events

LaravelSmsUp comes with handy events which provides the required information about the SMS messages.

Messages Was Sent

Triggered when one or more messages are sent.

Example:

use Husnet\LaravelSmsUpCh\Events\SmsUpChMessageWasSent;

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

        // flashing to session
        session()->flash('message.to', $message->getTo());
        session()->flash('message.text', $message->getText());

        session()->flash('response.status', $response->getStatus());
        session()->flash('response.message', $response->getMessage());
        session()->flash('response.credits', $response->getCredits());
        session()->flash('response.invalid', $response->getInvalid());

    }
}

In your EventServiceProvider:

protected $listen = [
        ...
        'Husnet\LaravelSmsUpCh\Events\SmsUpChMessageWasSent' => [
            'App\Listeners\SmsUpChMessageSentListener',
        ],
    ];

SmsUp.ch API Documentation

Visit SmsUp.ch API Documentation for more information.

Support

Feel free to post your issues in the issues section.

Credits

License

The MIT License (MIT). Please see License File for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.