Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Milloy committed Aug 30, 2019
0 parents commit c05d0e8
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor
/.idea
composer.phar
composer.lock
.DS_Store
Thumbs.db
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# AWS SNS SMS Notifications Channel for Laravel

## Installation

`composer require itsnubix/aws-sns-sms-channel`

In your `config/services.php` file enter:

```php
'sns' => [
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('SNS_DEFAULT_REGION'),
],
```

Be sure that the user who owns your access key and secret has at least the following policy on AWS IAM:

```json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowSendingSMSMessages",
"Effect": "Allow",
"Action": [
"sns:Publish",
"sns:SetSMSAttributes",
"sns:CheckIfPhoneNumberIsOptedOut"
],
"Resource": ["*"]
}
]
}
```

Now in your notifications you can do the following:

```php
<?php
namespace App\Notifications;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Nubix\Notifications\Messages\SmsMessage;

class SendHelloText extends Notification
{
use Queueable;

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return array
*/
public function via($notifiable)
{
return ['sms'];
}
/**
* Get the SMS representation of the notification.
*
* @param mixed $notifiable
*
* @return \App\Channels\Messages\SmsMessage
*/
public function toSms($notifiable)
{
return (new SmsMessage())
->content('Hello world');
}
}
```
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "itsnubix/aws-sns-sms-channel",
"description": "AWS SNS SMS Notifications Channel for Laravel",
"license": "MIT",
"authors": [
{
"name": "Kyle Milloy",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": "^7.1.3",
"illuminate/notifications": "~5.8.0|^6.0|^7.0",
"aws/aws-sdk-php": "^3.110"
},
"autoload": {
"psr-4": {
"Nubix\\Notifications\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Nubix\\Notifications\\SmsChannelServiceProvider"
]
}
}
}
56 changes: 56 additions & 0 deletions src/Channels/SmsChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Nubix\Notifications\Channels;

use Aws\Sns\SnsClient;
use Illuminate\Notifications\Notification;
use Nubix\Notifications\Messages\SmsMessage;

class SmsChannel
{
/**
* The SNS client instance.
*
* @var \Aws\Sns\SnsClient
*/
protected $sns;

public function __construct(SnsClient $sns)
{
$this->sns = $sns;
}

/**
* Send the given notification.
*
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return \Aws\Result
*/
public function send($notifiable, Notification $notification)
{
if (!$to = $notifiable->routeNotificationFor('sms', $notification)) {
return;
}

if (!$result = $this->sns->checkIfPhoneNumberIsOptedOut(['phoneNumber' => $to])) {
return;
}

if ($result['isOptedOut']) {
return;
}

$message = $notification->toSms($notifiable);

if (is_string($message)) {
$message = new SmsMessage($message);
}

return $this->sns->publish([
'Message' => $message->content,
'PhoneNumber' => $to,
]);
}
}
37 changes: 37 additions & 0 deletions src/Messages/SmsMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Nubix\Notifications\Messages;

class SmsMessage
{
/**
* The message content.
*
* @var string
*/
public $content;

/**
* Create a new message instance.
*
* @param string|null $content
*/
public function __construct($content = '')
{
$this->content = $content;
}

/**
* Set the message content.
*
* @param string $content
*
* @return $this
*/
public function content(string $content)
{
$this->content = trim($content);

return $this;
}
}
35 changes: 35 additions & 0 deletions src/SmsChannelServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Nubix\Notifications;

use Aws\Sns\SnsClient;
use Aws\Credentials\Credentials;
use Illuminate\Support\ServiceProvider;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\Facades\Notification;
use Nubix\Notifications\Channels\SmsChannel;

class SmsChannelServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register()
{
Notification::resolved(function (ChannelManager $service) {
$service->extend('sms', function ($app) {
return new SmsChannel(
new SnsClient([
'version' => '2010-03-31',
'credentials' => new Credentials(
$this->app['config']['services.sns.key'],
$this->app['config']['services.sns.secret']
),
'region' => $this->app['config']['services.sns.region'],
]),
$this->app['config']['services.sns.topic']
);
});
});
}
}

0 comments on commit c05d0e8

Please sign in to comment.