rennokki/laravel-sns-events

A description for laravel-sns-events.

Installs: 1 112

Dependents: 0

Suggesters: 0

Stars: 60

Watchers: 1

Forks: 2

Open Issues: 2

Type:package

1.0.0 2019-05-29 15:53 UTC

README

Build Status codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

PayPal

Laravel SNS Events

Laravel SNS Events allow you to listen to SNS webhooks via Laravel Events. It implements a controller that is made to properly listen to SNS HTTP(s) webhooks and trigger events on which you can listen to in Laravel.

If you are not familiar with Laravel Events & Listeners, make sure you check the documentation section on laravel.com because this package will need you to understand this concept.

Install

$ composer require rennokki/laravel-sns-events

Usage

The package comes with two event classes:

  • Rennokki\LaravelSnsEvents\Events\SnsEvent - triggered on each SNS message
  • Rennokki\LaravelSnsEvents\Events\SnsSubscriptionConfirmation - triggered when the subscription is confirmed

This package comes with a controller that will handle the response for you. Register the route in your web.php:

...

// you can choose any route
Route::any('/aws/sns', 'Rennokki\LaravelSnsEvents\Http\Controllers\SnsController@handle');

SNS sends data through POST, so you will need to whitelist your route in your VerifyCsrfToken.php:

protected $except = [
    ...
    'aws/sns/',
];

You will need an AWS account and register a SNS Topic and set up a subscription for HTTP(s) protocol that will point out to the route you just registered.

If you have registered the route and created a SNS Topic, you should register the URL and click the confirmation button from the AWS Dashboard. In a short while, if you implemented the route well, you'll be seeing that your endpoint is registered.

To process the events, you should add the events in your app/Providers/EventServiceProvider.php:

use Rennokki\LaravelSnsEvents\Events\SnsEvent;
use Rennokki\LaravelSnsEvents\Events\SnsSubscriptionConfirmation;

...

protected $listen = [
    ...
    SnsEvent::class => [
        // add your listeners here for SNS events
    ],
    SnsSubscriptionConfirmation::class => [
        // add your listeners here in case you want to listen to subscription confirmation
    ],
]

You will be able to access the SNS message from your listeners like this:

class MyListener
{
    ...

    public function handle($event)
    {
        // $event->message is an array
    }
}

For example, synthetizing an AWS Polly Text-To-Speech would return in $event->message an array like this:

[
    'taskId' => '...',
    'taskStatus' => 'FAILED',
    'taskStatusReason' => 'Error occurred while trying to upload file to S3. Please verify that the bucket existsin this region and you have permission to write objects to the specified bucket.',
    'outputUri' => 's3://...',
    'creationTime' => '2019-05-29T15:27:31.231Z',
    'requestCharacters' => 58,
    'snsTopicArn' => '...',
    'outputFormat' => 'Mp3',
    'sampleRate' => '22050',
    'speechMarkTypes' => [],
    'textType' => 'Text',
    'voiceId' => 'Joanna',
]

Testing

Run the tests with:

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email DummyAuthorEmail instead of using the issue tracker.

License

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