/SinchBundle

📦 Provides integration with Sinch.com SMS API.

Primary LanguagePHPMIT LicenseMIT

SinchBundle

📦 Provides integration with Sinch.com SMS API.

Currently in development process! Things can be changed at any moment!

Sinch Logo

Scrutinizer Quality Score Build Status CodeCov License Latest Stable Version Total Downloads StyleCI Gitter

SensioLabsInsight knpbundles.com

Requirements

  • PHP 7.1 and later
  • Symfony 4.0 and later

Installation

Create application for Sinch

Sing up in Sinch.com and create a new app.

Add dependency via Composer

composer req fresh/sinch-bundle='dev-master'

Add key and secret to parameters.yml

Add the following lines to your parameters.yml.dist file:

parameters:
    sinch.key: EnterKeyForYourSinchApp
    sinch.secret: EnterSecretForYourSinchApp

During the composer update you have to enter your own key and secret for your Sinch application, which you can find in your Sinch dashboard.

Update config.yml

Add the following lines to config.yml file:

fresh_sinch:
    key: "%sinch.key%"
    secret: "%sinch.secret%"

Usage

Example of sending SMS

use Fresh\SinchBundle\Service\Sinch;

class Foo {
    public function bar(Sinch $sinch) {
        // Set the outbound number where you want to send the SMS
        $messageId = $sinch->sendSMS('+13155555552', 'Your message');
        
        // If success then the ID of sent message is returned (it is an integer value)
        echo $messageId;
    }
}

Example of checking SMS status

use Fresh\SinchBundle\Service\Sinch;

class Foo {
    public function bar(Sinch $sinch) {
        $messageId = 123456789; // The ID of Sinch message you get after successful SMS sending
        
        // Status is a string with one of these values: pending, successful, faulted, unknown
        $status = $sinch->getStatusOfSMS($messageId);
        
        // Other helper methods, return true of false
        $sinch->smsIsSentSuccessfully($messageId);
        $sinch->smsIsPending($messageId);
        $sinch->smsIsFaulted($messageId);
        $sinch->smsInUnknownStatus($messageId);
    }
}

Catching and processing Sinch exceptions

use Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException;
use Fresh\SinchBundle\Service\Sinch;

class Foo {
    public function bar(Sinch $sinch) {
        try {
            $messageId = $sinch->sendSMS($phoneNumber, 'Your message');
            // Some logic related to SMS processing...
        } catch (SinchPaymentRequiredException $e) {
            $logger->error('SMS was not sent. Looks like your Sinch account run out of money.');
            // Here you can, for example, send urgent emails to admin users
            // to notify that your Sinch account run out of money
        }
    }
}

Contributing

See CONTRIBUTING file.