From c05d0e8d07305dad5968b7abc7d2ce83528d17c3 Mon Sep 17 00:00:00 2001 From: Kyle Milloy Date: Fri, 30 Aug 2019 13:10:27 -0600 Subject: [PATCH] init --- .gitignore | 6 +++ README.md | 76 +++++++++++++++++++++++++++++++ composer.json | 29 ++++++++++++ src/Channels/SmsChannel.php | 56 +++++++++++++++++++++++ src/Messages/SmsMessage.php | 37 +++++++++++++++ src/SmsChannelServiceProvider.php | 35 ++++++++++++++ 6 files changed, 239 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Channels/SmsChannel.php create mode 100644 src/Messages/SmsMessage.php create mode 100644 src/SmsChannelServiceProvider.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c527f52 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/vendor +/.idea +composer.phar +composer.lock +.DS_Store +Thumbs.db \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..48b16bf --- /dev/null +++ b/README.md @@ -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 +content('Hello world'); + } +} +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..bc33ebc --- /dev/null +++ b/composer.json @@ -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": "kmilloy@nubix.ca" + } + ], + "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" + ] + } + } +} diff --git a/src/Channels/SmsChannel.php b/src/Channels/SmsChannel.php new file mode 100644 index 0000000..1a52544 --- /dev/null +++ b/src/Channels/SmsChannel.php @@ -0,0 +1,56 @@ +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, + ]); + } +} diff --git a/src/Messages/SmsMessage.php b/src/Messages/SmsMessage.php new file mode 100644 index 0000000..1c66a15 --- /dev/null +++ b/src/Messages/SmsMessage.php @@ -0,0 +1,37 @@ +content = $content; + } + + /** + * Set the message content. + * + * @param string $content + * + * @return $this + */ + public function content(string $content) + { + $this->content = trim($content); + + return $this; + } +} diff --git a/src/SmsChannelServiceProvider.php b/src/SmsChannelServiceProvider.php new file mode 100644 index 0000000..9d0d44c --- /dev/null +++ b/src/SmsChannelServiceProvider.php @@ -0,0 +1,35 @@ +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'] + ); + }); + }); + } +}