From 456af6c299a3569b6375062912e3d8809db17fc3 Mon Sep 17 00:00:00 2001 From: Rene Roscher Date: Fri, 16 Feb 2024 02:56:55 +0100 Subject: [PATCH] initial commit --- .github/workflows/tests.yml | 32 ----- .gitignore | 1 + CHANGELOG.md | 2 +- LICENSE.md | 2 +- README.md | 139 +++++++++++--------- composer.json | 30 +++-- phpunit.xml.dist | 29 ---- src/ClickSendServiceProvider.php | 52 ++++++++ src/ClickSendSmsChannel.php | 51 +++++++ src/ClickSendVoiceChannel.php | 55 ++++++++ src/Exceptions/CouldNotSendNotification.php | 11 -- src/Models/ClickSendSmsMessage.php | 61 +++++++++ src/Models/ClickSendVoiceMessage.php | 77 +++++++++++ src/ServiceNameChannel.php | 31 ----- src/ServiceNameMessage.php | 10 -- src/ServiceNameServiceProvider.php | 40 ------ tests/ExampleTest.php | 14 -- 17 files changed, 394 insertions(+), 243 deletions(-) delete mode 100644 .github/workflows/tests.yml delete mode 100644 phpunit.xml.dist create mode 100644 src/ClickSendServiceProvider.php create mode 100644 src/ClickSendSmsChannel.php create mode 100644 src/ClickSendVoiceChannel.php delete mode 100644 src/Exceptions/CouldNotSendNotification.php create mode 100644 src/Models/ClickSendSmsMessage.php create mode 100644 src/Models/ClickSendVoiceMessage.php delete mode 100644 src/ServiceNameChannel.php delete mode 100644 src/ServiceNameMessage.php delete mode 100644 src/ServiceNameServiceProvider.php delete mode 100644 tests/ExampleTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml deleted file mode 100644 index f9ff88d..0000000 --- a/.github/workflows/tests.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: PHPUnit tests - -on: - - push - - pull_request - -jobs: - tests: - runs-on: ubuntu-latest - strategy: - fail-fast: true - matrix: - php: [7.2, 7.3, 7.4, 8.0] - - name: Tests on PHP ${{ matrix.php }} - ${{ matrix.stability }} - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - tools: composer:v2 - coverage: none - - - name: Install dependencies - run: composer update --prefer-source --no-interaction --no-progress - - - name: Execute tests - run: vendor/bin/phpunit --verbose diff --git a/.gitignore b/.gitignore index 616e6d3..cb67e9e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.phar composer.lock .DS_Store .phpunit.result.cache +.idea diff --git a/CHANGELOG.md b/CHANGELOG.md index fd83ef6..ee08311 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -All notable changes to `:package_name` will be documented in this file +All notable changes to `clicksend-laravel` will be documented in this file ## 1.0.0 - 201X-XX-XX diff --git a/LICENSE.md b/LICENSE.md index 8bc4a36..6d3cc20 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ # The MIT License (MIT) -Copyright (c) :author_name <:author_email> +Copyright (c) Renรฉ Roscher > Permission is hereby granted, free of charge, to any person obtaining a copy > of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 849f09a..c7bf98b 100644 --- a/README.md +++ b/README.md @@ -1,78 +1,91 @@ -Please see [this repo](https://github.com/laravel-notification-channels/channels) for instructions on how to submit a channel proposal. - -# A Boilerplate repo for contributions - -[![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/:package_name.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/:package_name) -[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) -[![Build Status](https://img.shields.io/travis/laravel-notification-channels/:package_name/master.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/:package_name) -[![StyleCI](https://styleci.io/repos/:style_ci_id/shield)](https://styleci.io/repos/:style_ci_id) -[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/:sensio_labs_id.svg?style=flat-square)](https://insight.sensiolabs.com/projects/:sensio_labs_id) -[![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/:package_name.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/:package_name) -[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/:package_name/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/:package_name/?branch=master) -[![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/:package_name.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/:package_name) - -This package makes it easy to send notifications using [:service_name](link to service) with Laravel 5.5+, 6.x and 7.x - -**Note:** Replace ```:channel_namespace``` ```:service_name``` ```:author_name``` ```:author_username``` ```:author_website``` ```:author_email``` ```:package_name``` ```:package_description``` ```:style_ci_id``` ```:sensio_labs_id``` with their correct values in [README.md](README.md), [CHANGELOG.md](CHANGELOG.md), [CONTRIBUTING.md](CONTRIBUTING.md), [LICENSE.md](LICENSE.md), [composer.json](composer.json) and other files, then delete this line. -**Tip:** Use "Find in Path/Files" in your code editor to find these keywords within the package directory and replace all occurences with your specified term. - -This is where your description should go. Add a little code example so build can understand real quick how the package can be used. Try and limit it to a paragraph or two. - - - -## Contents - -- [Installation](#installation) - - [Setting up the :service_name service](#setting-up-the-:service_name-service) -- [Usage](#usage) - - [Available Message methods](#available-message-methods) -- [Changelog](#changelog) -- [Testing](#testing) -- [Security](#security) -- [Contributing](#contributing) -- [Credits](#credits) -- [License](#license) +# Laravel ClickSend Notification Channel +This Laravel package integrates ClickSend for sending SMS and voice messages within Laravel applications, leveraging ClickSend's capabilities for notifications. ## Installation -Please also include the steps for any third-party service setup that's required for this package. - -### Setting up the :service_name service - -Optionally include a few steps how users can set up the service. - -## Usage - -Some code examples, make it clear how to use the package - -### Available Message methods - -A list of all available options - -## Changelog +Install via composer: -Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. - -## Testing - -``` bash -$ composer test +``` +composer require rene-roscher/clicksend-laravel ``` -## Security +## Configuration -If you discover any security related issues, please email :author_email instead of using the issue tracker. +Add ClickSend credentials in `.env` and in `config/services.php`: -## Contributing +.env +``` +CLICKSEND_USERNAME=username +CLICKSEND_PASSWORD=password +``` -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. +config/services.php +``` +'clicksend' => [ + 'username' => env('CLICKSEND_USERNAME'), + 'password' => env('CLICKSEND_PASSWORD'), +], +``` -## Credits +## Usage -- [:author_name](https://github.com/:author_username) -- [All Contributors](../../contributors) +### SMS Notification + +Simply create a new notification and use the preferred class to send a notification. + +Note: Make sure to replace all spaces in the phone number with an empty string. + +```php +class TestNotificationClickSend extends Notification +{ + /** + * Get the notification's delivery channels. + * + * @return array + */ + public function via(object $notifiable): array + { + return ['clicksend-voice', 'clicksend-sms']; // All channels are automatically registered by default + } + + /** + * Get the mail representation of the notification. + */ + public function toClicksendVoice(object $notifiable) // Voice + { + return ClickSendVoiceMessage::create( + message: 'Your Verification Code is: 1234 - I repeat: 1234 - Goodbye!', + to: $notifiable->phone_number + ); + // Or + return 'Your Verification Code is: 1234 - I repeat: 1234 - Goodbye!'; + } + + public function toClicksendSms(object $notifiable) // SMS + { + // Default + return ClickSendSmsMessage::create( + message: 'Your Verification was approved. Thank you! ๐ŸŽ‰', + to: $notifiable->phone_number + ); + + // Or a single message + return 'Your Verification was approved. Thank you! ๐ŸŽ‰'; + + // Or multiple messages at once + return ClickSendSmsMessage::create( + message: 'Your Verification was approved. Thank you! ๐ŸŽ‰', + to: $notifiable->phone_number + )->addMessage( + message: 'Welcome to our platform! ๐ŸŽ‰', + to: $notifiable->phone_number + ); + } + +} +``` ## License -The MIT License (MIT). Please see [License File](LICENSE.md) for more information. +Licensed under the MIT license. diff --git a/composer.json b/composer.json index 4321e81..c3b1e3d 100644 --- a/composer.json +++ b/composer.json @@ -1,20 +1,21 @@ { - "name": "laravel-notification-channels/:package_name", - "description": ":package_description", - "homepage": "https://github.com/laravel-notification-channels/:package_name", + "name": "rene-roscher/clicksend-laravel", + "description": "A Laravel package to create SMS messages & Voice calls using ClickSend", + "homepage": "https://github.com/rene-roscher/clicksend-laravel", "license": "MIT", "authors": [ { - "name": ":author_name", - "email": ":author_email", - "homepage": ":author_website", + "name": "Renรฉ Roscher", + "email": "roscher794@gmail.com", + "homepage": "https://livck.com", "role": "Developer" } ], "require": { - "php": ">=7.2", - "illuminate/notifications": "~6.0 || ~7.0 || ~8.0", - "illuminate/support": "~6.0 || ~7.0 || ~8.0" + "php": ">=8.2", + "illuminate/notifications": "~5.0 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0", + "illuminate/support": "~5.0 || ~6.0 || ~7.0 || ~8.0 || ~9.0 || ~10.0", + "clicksend/clicksend-php": "^5.0" }, "require-dev": { "mockery/mockery": "^1.0", @@ -22,12 +23,12 @@ }, "autoload": { "psr-4": { - "NotificationChannels\\:channel_namespace\\": "src" + "NotificationChannels\\ClickSend\\": "src" } }, "autoload-dev": { "psr-4": { - "NotificationChannels\\:channel_namespace\\Test\\": "tests" + "NotificationChannels\\ClickSend\\Test\\": "tests" } }, "scripts": { @@ -36,5 +37,12 @@ }, "config": { "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "NotificationChannels\\ClickSend\\ClickSendServiceProvider" + ] + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 7042c52..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,29 +0,0 @@ - - - - - tests - - - - - src/ - - - - - - - - - - diff --git a/src/ClickSendServiceProvider.php b/src/ClickSendServiceProvider.php new file mode 100644 index 0000000..5df66b6 --- /dev/null +++ b/src/ClickSendServiceProvider.php @@ -0,0 +1,52 @@ +app->singleton(Configuration::class, function ($app) { + $config = $app['config']['services.clicksend']; + + if (empty($config['username']) && empty($config['api_key'])) { + throw new \InvalidArgumentException('ClickSend configuration requires either "username" and "password" or "api_key".'); + } + + $configuration = new Configuration(); + + if (!empty($config['username']) && !empty($config['password'])) { + $configuration->setUsername($config['username']) + ->setPassword($config['password']); + } + + if (!empty($config['api_key'])) { + $configuration->setApiKey('Authorization', 'Bearer ' . $config['api_key']); + } + + return $configuration; + }); + + // Extending Laravel's Notification Channels + Notification::extend('clicksend-sms', function ($app) { + return $app->make(ClickSendSmsChannel::class); + }); + Notification::extend('clicksend-voice', function ($app) { + return $app->make(ClickSendVoiceChannel::class); + }); + } + + /** + * Register the application services. + */ + public function register() + { + } +} diff --git a/src/ClickSendSmsChannel.php b/src/ClickSendSmsChannel.php new file mode 100644 index 0000000..c34b168 --- /dev/null +++ b/src/ClickSendSmsChannel.php @@ -0,0 +1,51 @@ +routeNotificationFor('clicksend'); + + if (!$to) { + return; + } + + $message = $notification->toClickSendSms($notifiable); + + if (!$message) { + return; + } + + if (is_string($message)) { + $message = ClickSendSmsMessage::create( + message: $message, + to: $to + ); + } + + (new SMSApi( + config: $this->configuration + ))->smsSendPost($message->toMessages()); + } +} diff --git a/src/ClickSendVoiceChannel.php b/src/ClickSendVoiceChannel.php new file mode 100644 index 0000000..4113fe4 --- /dev/null +++ b/src/ClickSendVoiceChannel.php @@ -0,0 +1,55 @@ +routeNotificationFor('clicksend'); + + if (!$to) { + return; + } + + $message = $notification->toClickSendVoice($notifiable); + + if (!$message) { + return; + } + + if (is_string($message)) { + $message = ClickSendVoiceMessage::create( + message: $message, + to: $to + ); + } + + (new VoiceApi( + config: $this->configuration + ))->voiceSendPost($message->toMessages()); + } +} diff --git a/src/Exceptions/CouldNotSendNotification.php b/src/Exceptions/CouldNotSendNotification.php deleted file mode 100644 index f9dc566..0000000 --- a/src/Exceptions/CouldNotSendNotification.php +++ /dev/null @@ -1,11 +0,0 @@ -messages = [ + [ + 'message' => $this->message, + 'from' => $this->from, + 'to' => $this->to, + ] + ]; + } + + public static function create($message = '', $from = null, $to = null): self + { + return new static( + message: $message, + from: $from, + to: $to + ); + } + + public function addMessage(string $message, ?string $from = null, ?string $to = null): self + { + $this->messages[] = [ + 'message' => $message, + 'from' => $from, + 'to' => $to, + ]; + + return $this; + } + + public function toMessages(): SmsMessageCollection + { + $smsMessages = array_map(function ($msg) { + return tap(new SmsMessage(), function (SmsMessage $smsMessage) use ($msg) { + $smsMessage->setBody($msg['message']); + $smsMessage->setFrom($msg['from']); + $smsMessage->setTo($msg['to']); + }); + }, $this->messages); + + return tap(new SmsMessageCollection(), function (SmsMessageCollection $smsMessageCollection) use ($smsMessages) { + $smsMessageCollection->setMessages($smsMessages); + }); + } + +} diff --git a/src/Models/ClickSendVoiceMessage.php b/src/Models/ClickSendVoiceMessage.php new file mode 100644 index 0000000..cd5768e --- /dev/null +++ b/src/Models/ClickSendVoiceMessage.php @@ -0,0 +1,77 @@ +messages = [ + [ + 'message' => $this->message, + 'voice' => $this->voice, + 'to' => $this->to, + 'country' => $this->country, + 'lang' => $this->lang, + 'custom_string' => $this->customString, + ] + ]; + } + + public static function create($message = '', ?string $voice = 'female', ?string $country = 'DE', ?string $lang = 'de-de', ?string $customString = null, $to = null): self + { + return new static( + message: $message, + voice: $voice, + country: $country, + lang: $lang, + customString: $customString, + to: $to + ); + } + + public function addMessage(string $message, ?string $voice = 'female', ?string $country = 'DE', ?string $lang = 'de-de', ?string $customString = null, ?string $to = null): self + { + $this->messages[] = [ + 'message' => $message, + 'voice' => $voice, + 'to' => $to, + 'country' => $country, + 'lang' => $lang, + 'custom_string' => $customString, + ]; + + return $this; + } + + public function toMessages(): VoiceMessageCollection + { + $messages = array_map(function ($msg) { + return tap(new VoiceMessage(), function (VoiceMessage $message) use ($msg) { + $message->setBody($msg['message']); + $message->setTo($msg['to']); + $message->setVoice($msg['voice']); + $message->setCountry($msg['country']); + $message->setLang($msg['lang']); + $message->setCustomString($msg['custom_string']); + }); + }, $this->messages); + + return tap(new VoiceMessageCollection(), function (VoiceMessageCollection $voiceMessageCollection) use ($messages) { + $voiceMessageCollection->setMessages($messages); + }); + } + +} diff --git a/src/ServiceNameChannel.php b/src/ServiceNameChannel.php deleted file mode 100644 index a5ea1b5..0000000 --- a/src/ServiceNameChannel.php +++ /dev/null @@ -1,31 +0,0 @@ -error) { // replace this by the code need to check for errors -// throw CouldNotSendNotification::serviceRespondedWithAnError($response); -// } - } -} diff --git a/src/ServiceNameMessage.php b/src/ServiceNameMessage.php deleted file mode 100644 index 940a27f..0000000 --- a/src/ServiceNameMessage.php +++ /dev/null @@ -1,10 +0,0 @@ -app->when(Channel::class) - ->needs(Pusher::class) - ->give(function () { - $pusherConfig = config('broadcasting.connections.pusher'); - - return new Pusher( - $pusherConfig['key'], - $pusherConfig['secret'], - $pusherConfig['app_id'] - ); - }); - */ - - } - - /** - * Register the application services. - */ - public function register() - { - } -} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index c0d6672..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -assertTrue(true); - } -}