├── .styleci.yml ├── CHANGELOG.md ├── .editorconfig ├── src ├── Exceptions │ ├── InvalidConfiguration.php │ └── CouldNotSendNotification.php ├── MessagebirdServiceProvider.php ├── MessagebirdMessage.php ├── MessagebirdClient.php └── MessagebirdChannel.php ├── LICENSE.md ├── composer.json ├── CONTRIBUTING.md └── README.md /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `Messagebird` will be documented in this file 4 | 5 | ## 3.0.0 - 2019-09-18 6 | 7 | - Laravel 8 support 8 | 9 | ## 1.0.0 - 2016-08-27 10 | 11 | - initial release 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /src/Exceptions/InvalidConfiguration.php: -------------------------------------------------------------------------------- 1 | getCode()}: {$exception->getMessage()}'"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/MessagebirdServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->when(MessagebirdChannel::class) 17 | ->needs(MessagebirdClient::class) 18 | ->give(function () { 19 | $config = config('services.messagebird'); 20 | 21 | if (is_null($config)) { 22 | throw InvalidConfiguration::configurationNotSet(); 23 | } 24 | 25 | return new MessagebirdClient(new Client(), $config['access_key']); 26 | }); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Peter Steenbergen 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-notification-channels/messagebird", 3 | "description": "MessageBird notification channel for Laravel 5.x", 4 | "homepage": "https://github.com/laravel-notification-channels/messagebird", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Peter Steenbergen", 9 | "email": "psteenbergen@gmail.com", 10 | "homepage": "https://3ws.nl", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": "^8.0|^8.1", 16 | "illuminate/notifications": "^9.0|^10.0|^11.0|^12.0", 17 | "illuminate/support": "^9.0|^10.0|^11.0|^12.0", 18 | "illuminate/queue": "^9.0|^10.0|^11.0|^12.0", 19 | "guzzlehttp/guzzle": "^7.2", 20 | "ext-json": "*" 21 | }, 22 | "require-dev": { 23 | "mockery/mockery": "^1.4.4", 24 | "phpunit/phpunit": "^9.5.10|^10.0" 25 | }, 26 | "autoload": { 27 | "psr-4": { 28 | "NotificationChannels\\Messagebird\\": "src" 29 | } 30 | }, 31 | "autoload-dev": { 32 | "psr-4": { 33 | "NotificationChannels\\Messagebird\\Test\\": "tests" 34 | } 35 | }, 36 | "scripts": { 37 | "test": "vendor/bin/phpunit" 38 | }, 39 | "config": { 40 | "sort-packages": true 41 | }, 42 | "minimum-stability": "dev", 43 | "extra": { 44 | "laravel": { 45 | "providers": [ 46 | "NotificationChannels\\Messagebird\\MessagebirdServiceProvider" 47 | ] 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/MessagebirdMessage.php: -------------------------------------------------------------------------------- 1 | body = trim($body); 23 | } 24 | } 25 | 26 | public function setBody($body) 27 | { 28 | $this->body = trim($body); 29 | 30 | return $this; 31 | } 32 | 33 | public function setOriginator($originator) 34 | { 35 | $this->originator = $originator; 36 | 37 | return $this; 38 | } 39 | 40 | public function setRecipients($recipients) 41 | { 42 | if (is_array($recipients)) { 43 | $recipients = implode(',', $recipients); 44 | } 45 | 46 | $this->recipients = $recipients; 47 | 48 | return $this; 49 | } 50 | 51 | public function setReference($reference) 52 | { 53 | $this->reference = $reference; 54 | 55 | return $this; 56 | } 57 | 58 | public function setDatacoding($datacoding) 59 | { 60 | $this->datacoding = $datacoding; 61 | 62 | return $this; 63 | } 64 | 65 | public function setReportUrl($reportUrl) 66 | { 67 | $this->reportUrl = $reportUrl; 68 | 69 | return $this; 70 | } 71 | 72 | public function toJson() 73 | { 74 | return json_encode($this); 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/MessagebirdClient.php: -------------------------------------------------------------------------------- 1 | client = $client; 22 | $this->access_key = $access_key; 23 | } 24 | 25 | /** 26 | * Send the Message. 27 | * @param MessagebirdMessage $message 28 | * @return 29 | * @throws CouldNotSendNotification 30 | */ 31 | public function send(MessagebirdMessage $message) 32 | { 33 | if (empty($message->originator)) { 34 | $message->setOriginator(config('services.messagebird.originator')); 35 | } 36 | if (empty($message->recipients)) { 37 | $message->setRecipients(config('services.messagebird.recipients')); 38 | } 39 | if (empty($message->datacoding)) { 40 | $message->setDatacoding('auto'); 41 | } 42 | 43 | try { 44 | $response = $this->client->request('POST', 'https://rest.messagebird.com/messages', [ 45 | 'body' => $message->toJson(), 46 | 'headers' => [ 47 | 'Authorization' => 'AccessKey '.$this->access_key, 48 | ], 49 | ]); 50 | 51 | return json_decode($response->getBody()->__toString()); 52 | } catch (Exception $exception) { 53 | throw CouldNotSendNotification::serviceRespondedWithAnError($exception); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/MessagebirdChannel.php: -------------------------------------------------------------------------------- 1 | client = $client; 19 | $this->dispatcher = $dispatcher; 20 | } 21 | 22 | /** 23 | * Send the given notification. 24 | * 25 | * @param mixed $notifiable 26 | * @param \Illuminate\Notifications\Notification $notification 27 | * @return object with response body data if succesful response from API | empty array if not 28 | * 29 | * @throws \NotificationChannels\MessageBird\Exceptions\CouldNotSendNotification 30 | */ 31 | public function send($notifiable, Notification $notification) 32 | { 33 | $message = $notification->toMessagebird($notifiable); 34 | 35 | $data = []; 36 | 37 | if (is_string($message)) { 38 | $message = MessagebirdMessage::create($message); 39 | } 40 | 41 | if ($to = $notifiable->routeNotificationFor('messagebird', $notification)) { 42 | $message->setRecipients($to); 43 | } 44 | 45 | try { 46 | $data = $this->client->send($message); 47 | 48 | if ($this->dispatcher !== null) { 49 | $this->dispatcher->dispatch('messagebird-sms', [$notifiable, $notification, $data]); 50 | } 51 | } catch (CouldNotSendNotification $e) { 52 | if ($this->dispatcher !== null) { 53 | $this->dispatcher->dispatch( 54 | new NotificationFailed( 55 | $notifiable, 56 | $notification, 57 | 'messagebird-sms', 58 | $e->getMessage() 59 | ) 60 | ); 61 | } 62 | } 63 | 64 | return $data; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Messagebird notifications channel for Laravel 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/messagebird.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/messagebird) 4 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 5 | [![Build Status](https://img.shields.io/travis/laravel-notification-channels/messagebird/master.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/messagebird) 6 | [![StyleCI](https://styleci.io/repos/65683649/shield)](https://styleci.io/repos/65683649) 7 | [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/357bb8d3-2163-45be-97f2-ce71434a4379.svg?style=flat-square)](https://insight.sensiolabs.com/projects/357bb8d3-2163-45be-97f2-ce71434a4379) 8 | [![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/messagebird.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/messagebird) 9 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/messagebird/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/messagebird/?branch=master) 10 | [![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/messagebird.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/messagebird) 11 | 12 | This package makes it easy to send [Messagebird SMS notifications](https://github.com/messagebird/php-rest-api) with Laravel. 13 | 14 | ## Contents 15 | 16 | - [Requirements](#requirements) 17 | - [Installation](#installation) 18 | - [Setting up your Messagebird account](#setting-up-your-messagebird-account) 19 | - [Usage](#usage) 20 | - [Changelog](#changelog) 21 | - [Testing](#testing) 22 | - [Security](#security) 23 | - [Contributing](#contributing) 24 | - [Credits](#credits) 25 | - [License](#license) 26 | 27 | ## Requirements 28 | 29 | - [Sign up](https://www.messagebird.com/en/signup) for a free MessageBird account 30 | - Create a new access_key in the developers sections 31 | 32 | ## Installation 33 | 34 | You can install the package via composer: 35 | 36 | ``` bash 37 | composer require laravel-notification-channels/messagebird 38 | ``` 39 | 40 | for Laravel 5.4 or lower, you must add the service provider to your config: 41 | 42 | ```php 43 | // config/app.php 44 | 'providers' => [ 45 | ... 46 | NotificationChannels\Messagebird\MessagebirdServiceProvider::class, 47 | ], 48 | ``` 49 | 50 | ## Setting up your Messagebird account 51 | 52 | Add the environment variables to your `config/services.php`: 53 | 54 | ```php 55 | // config/services.php 56 | ... 57 | 'messagebird' => [ 58 | 'access_key' => env('MESSAGEBIRD_ACCESS_KEY'), 59 | 'originator' => env('MESSAGEBIRD_ORIGINATOR'), 60 | 'recipients' => env('MESSAGEBIRD_RECIPIENTS'), 61 | ], 62 | ... 63 | ``` 64 | 65 | Add your Messagebird Access Key, Default originator (name or number of sender), and default recipients to your `.env`: 66 | 67 | ```php 68 | // .env 69 | ... 70 | MESSAGEBIRD_ACCESS_KEY= 71 | MESSAGEBIRD_ORIGINATOR= 72 | MESSAGEBIRD_RECIPIENTS= 73 | ], 74 | ... 75 | ``` 76 | 77 | Notice: The originator can contain a maximum of 11 alfa-numeric characters. 78 | 79 | ## Usage 80 | 81 | Now you can use the channel in your `via()` method inside the notification: 82 | 83 | ``` php 84 | use NotificationChannels\Messagebird\MessagebirdChannel; 85 | use NotificationChannels\Messagebird\MessagebirdMessage; 86 | use Illuminate\Notifications\Notification; 87 | 88 | class VpsServerOrdered extends Notification 89 | { 90 | public function via($notifiable) 91 | { 92 | return [MessagebirdChannel::class]; 93 | } 94 | 95 | public function toMessagebird($notifiable) 96 | { 97 | return (new MessagebirdMessage("Your {$notifiable->service} was ordered!")); 98 | } 99 | } 100 | ``` 101 | 102 | Additionally you can add recipients (single value or array) 103 | 104 | ``` php 105 | return (new MessagebirdMessage("Your {$notifiable->service} was ordered!"))->setRecipients($recipients); 106 | ``` 107 | 108 | In order to handle a status report you can also set a reference 109 | 110 | ``` php 111 | return (new MessagebirdMessage("Your {$notifiable->service} was ordered!"))->setReference($id); 112 | ``` 113 | 114 | ## Changelog 115 | 116 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 117 | 118 | ## Testing 119 | 120 | ``` bash 121 | $ composer test 122 | ``` 123 | 124 | ## Security 125 | 126 | If you discover any security related issues, please email psteenbergen@gmail.com instead of using the issue tracker. 127 | 128 | ## Contributing 129 | 130 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 131 | 132 | ## Credits 133 | 134 | - [Peter Steenbergen](https://3ws.nl) 135 | - [Tonko Mulder](https://tonkomulder.nl) 136 | - [All Contributors](../../contributors) 137 | 138 | ## License 139 | 140 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 141 | --------------------------------------------------------------------------------