├── .editorconfig ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json └── src ├── Exceptions └── CouldNotSendNotification.php ├── RocketChat.php ├── RocketChatAttachment.php ├── RocketChatMessage.php ├── RocketChatServiceProvider.php └── RocketChatWebhookChannel.php /.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 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change log 2 | 3 | All notable changes to `laravel-notification-channels/rocket-chat` will be documented in this file 4 | 5 | ## [Unreleased] 6 | 7 | ## [0.3.0] - 2020-09-09 8 | 9 | ### Added 10 | 11 | - ([#17]) Added Laravel 8 support 12 | 13 | ## [0.2.0] - 2020-04-26 14 | 15 | ### Added 16 | 17 | - ([#14]) Added Laravel 7 support 18 | - ([#7]) Method `getChannel` added to `NotificationChannels\RocketChat\RocketChatMessage` class 19 | - ([#7]) Method `getFrom` added to `NotificationChannels\RocketChat\RocketChatMessage` class 20 | 21 | ### Changed 22 | 23 | - ([#7]) Method `channel` renamed to `getDefaultChannel` in `NotificationChannels\RocketChat\RocketChat` class 24 | - ([#7]) Method `token` renamed to `getToken` in `NotificationChannels\RocketChat\RocketChat` class 25 | - ([#7]) Method `setFromArray` renamed to `setPropertiesFromArray` in `NotificationChannels\RocketChat\RocketChatAttachment` class 26 | 27 | ### Fixed 28 | 29 | - ([#9]) Allow the use of `DateTimeImmutable` argument in `timestamp` method of `NotificationChannels\RocketChat\RocketChatAttachment` class 30 | 31 | ### Removed 32 | 33 | - ([#7]) Method `url` removed from `NotificationChannels\RocketChat\RocketChat` class 34 | - ([#10]) Method `clearAttachments` removed from `NotificationChannels\RocketChat\RocketChatMessage` class 35 | 36 | ## 0.1.0 - 2020-02-21 37 | 38 | - Initial release 39 | 40 | [Unreleased]: https://github.com/laravel-notification-channels/rocket-chat/compare/v0.3.0...master 41 | [0.3.0]: https://github.com/cybercog/laravel-love/compare/v0.2.0...v0.3.0 42 | [0.2.0]: https://github.com/cybercog/laravel-love/compare/v0.1.0...v0.2.0 43 | 44 | [#17]: https://github.com/laravel-notification-channels/rocket-chat/pull/17 45 | [#14]: https://github.com/laravel-notification-channels/rocket-chat/pull/14 46 | [#10]: https://github.com/laravel-notification-channels/rocket-chat/pull/10 47 | [#9]: https://github.com/laravel-notification-channels/rocket-chat/pull/9 48 | [#7]: https://github.com/laravel-notification-channels/rocket-chat/pull/7 49 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) Anton Komarev 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rocket.Chat Laravel Notifications Channel 2 | 3 | ![cog-laravel-rocket-chat-notification-channel](https://user-images.githubusercontent.com/1849174/74969369-87649980-542d-11ea-9692-c6f7ba68e2bf.png) 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/laravel-notification-channels/rocket-chat.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/rocket-chat) 6 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 7 | [![Build Status](https://img.shields.io/travis/laravel-notification-channels/rocket-chat/master.svg?style=flat-square)](https://travis-ci.org/laravel-notification-channels/rocket-chat) 8 | [![StyleCI](https://styleci.io/repos/241828511/shield)](https://styleci.io/repos/241828511) 9 | [![Quality Score](https://img.shields.io/scrutinizer/g/laravel-notification-channels/rocket-chat.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/rocket-chat) 10 | [![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/laravel-notification-channels/rocket-chat/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/laravel-notification-channels/rocket-chat/?branch=master) 11 | [![Total Downloads](https://img.shields.io/packagist/dt/laravel-notification-channels/rocket-chat.svg?style=flat-square)](https://packagist.org/packages/laravel-notification-channels/rocket-chat) 12 | 13 | ## Introduction 14 | 15 | This package makes it easy to send notifications using [RocketChat](https://rocket.chat/) with Laravel 5.6+. 16 | 17 | ## Contents 18 | 19 | - [Installation](#installation) 20 | - [Setting up the RocketChat service](#setting-up-the-rocketchat-service) 21 | - [Usage](#usage) 22 | - [Available Message methods](#available-message-methods) 23 | - [Changelog](#changelog) 24 | - [Testing](#testing) 25 | - [Security](#security) 26 | - [Contributing](#contributing) 27 | - [Credits](#credits) 28 | - [Change log](#changelog) 29 | - [License](#license) 30 | 31 | ## Installation 32 | 33 | You can install the package via composer: 34 | 35 | ```shell script 36 | $ composer require laravel-notification-channels/rocket-chat 37 | ``` 38 | 39 | ### Setting up the RocketChat service 40 | 41 | In order to send message to RocketChat channels, you need to obtain [Webhook](https://rocket.chat/docs/administrator-guides/integrations#how-to-create-a-new-incoming-webhook). 42 | 43 | Add your RocketChat API server's base url, incoming Webhook Token and optionally the default channel to your `config/services.php`: 44 | 45 | ```php 46 | // config/services.php 47 | ... 48 | 'rocketchat' => [ 49 | // Base URL for RocketChat API server (https://your.rocketchat.server.com) 50 | 'url' => env('ROCKETCHAT_URL'), 51 | 'token' => env('ROCKETCHAT_TOKEN'), 52 | // Default channel (optional) 53 | 'channel' => env('ROCKETCHAT_CHANNEL'), 54 | ], 55 | ... 56 | ``` 57 | 58 | ## Usage 59 | 60 | You can use the channel in your `via()` method inside the notification: 61 | 62 | ```php 63 | use Illuminate\Notifications\Notification; 64 | use NotificationChannels\RocketChat\RocketChatMessage; 65 | use NotificationChannels\RocketChat\RocketChatWebhookChannel; 66 | 67 | class TaskCompleted extends Notification 68 | { 69 | public function via($notifiable): array 70 | { 71 | return [ 72 | RocketChatWebhookChannel::class, 73 | ]; 74 | } 75 | 76 | public function toRocketChat($notifiable): RocketChatMessage 77 | { 78 | return RocketChatMessage::create('Test message') 79 | ->to('channel_name') // optional if set in config 80 | ->from('webhook_token'); // optional if set in config 81 | } 82 | } 83 | ``` 84 | 85 | In order to let your notification know which RocketChat channel you are targeting, add the `routeNotificationForRocketChat` method to your Notifiable model: 86 | 87 | ```php 88 | public function routeNotificationForRocketChat(): string 89 | { 90 | return 'channel_name'; 91 | } 92 | ``` 93 | 94 | ### Available methods 95 | 96 | `from()`: Sets the sender's access token. 97 | 98 | `to()`: Specifies the channel id to send the notification to (overridden by `routeNotificationForRocketChat` if empty). 99 | 100 | `content()`: Sets a content of the notification message. Supports Github flavoured markdown. 101 | 102 | `alias()`: This will cause the message’s name to appear as the given alias, but your username will still display. 103 | 104 | `emoji()`: This will make the avatar on this message be an emoji. (e.g. ':see_no_evil:') 105 | 106 | `avatar()`: This will make the avatar use the provided image url. 107 | 108 | `attachment()`: This will add an single attachment. 109 | 110 | `attachments()`: This will add multiple attachments. 111 | 112 | `clearAttachments()`: This will remove all attachments. 113 | 114 | ### Adding Attachment 115 | 116 | There are several ways to add one ore more attachments to a message 117 | 118 | ```php 119 | public function toRocketChat($notifiable) 120 | { 121 | return RocketChatMessage::create('Test message') 122 | ->to('channel_name') // optional if set in config 123 | ->from('webhook_token') // optional if set in config 124 | ->attachments([ 125 | RocketChatAttachment::create()->imageUrl('test'), 126 | RocketChatAttachment::create(['image_url' => 'test']), 127 | new RocketChatAttachment(['image_url' => 'test']), 128 | [ 129 | 'image_url' => 'test' 130 | ] 131 | ]); 132 | } 133 | ``` 134 | 135 | #### Available methods 136 | 137 | `color()`: The color you want the order on the left side to be, any value background-css supports. 138 | 139 | `text()`: The text to display for this attachment, it is different than the message’s text. 140 | 141 | `timestamp()`: Displays the time next to the text portion. ISO8601 Zulu Date or instance of any `\DateTime` 142 | 143 | `thumbnailUrl()`: An image that displays to the left of the text, looks better when this is relatively small. 144 | 145 | `messageLink()`: Only applicable if the ts is provided, as it makes the time clickable to this link. 146 | 147 | `collapsed()`: Causes the image, audio, and video sections to be hiding when collapsed is true. 148 | 149 | `author($name, $link, $icon)`: shortcut for author methods 150 | 151 | `authorName()`: Name of the author. 152 | 153 | `authorLink()`: Providing this makes the author name clickable and points to this link. 154 | 155 | `authorIcon()`: Displays a tiny icon to the left of the Author’s name. 156 | 157 | `title()`: Title to display for this attachment, displays under the author. 158 | 159 | `titleLink()`: Providing this makes the title clickable, pointing to this link. 160 | 161 | `titleLinkDownload()`: When this is true, a download icon appears and clicking this saves the link to file. 162 | 163 | `imageUrl()`: The image to display, will be “big” and easy to see. 164 | 165 | `audioUrl()`: Audio file to play, only supports what html audio does. 166 | 167 | `videoUrl()`: Video file to play, only supports what html video does. 168 | 169 | `fields()`: An array of Attachment Field Objects. 170 | 171 | ```php 172 | [ 173 | [ 174 | 'short' => false, // Whether this field should be a short field. Default: false 175 | 'title' => 'Title 1', //The title of this field. Required 176 | 'value' => 'Value 1' // The value of this field, displayed underneath the title value. Required 177 | ], 178 | [ 179 | 'short' => true, 180 | 'title' => 'Title 2', 181 | 'value' => 'Value 2' 182 | ], 183 | 184 | ]; 185 | ``` 186 | 187 | ## Changelog 188 | 189 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 190 | 191 | ## Testing 192 | 193 | ```shell script 194 | $ vendor/bin/phpunit 195 | ``` 196 | 197 | ## Security 198 | 199 | If you discover any security related issues, please email open@cybercog.su instead of using the issue tracker. 200 | 201 | ## Contributing 202 | 203 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 204 | 205 | ## Credits 206 | 207 | - [Anton Komarev] 208 | - [Nicholas] 209 | - [atymic] 210 | - [All Contributors](../../contributors) 211 | 212 | ## Change log 213 | 214 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 215 | 216 | ## License 217 | 218 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 219 | 220 | ## About CyberCog 221 | 222 | [CyberCog] is a Social Unity of enthusiasts. Research best solutions in product & software development is our passion. 223 | 224 | ![cybercog-logo](https://cloud.githubusercontent.com/assets/1849174/18418932/e9edb390-7860-11e6-8a43-aa3fad524664.png) 225 | 226 | [Anton Komarev]: https://komarev.com 227 | [Nicholas]: https://github.com/Funfare 228 | [atymic]: https://github.com/atymic 229 | [CyberCog]: https://cybercog.su 230 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-notification-channels/rocket-chat", 3 | "description": "Rocket.Chat Notifications channel for Laravel 5.6+", 4 | "homepage": "https://github.com/laravel-notification-channels/rocket-chat", 5 | "license": "MIT", 6 | "keywords": [ 7 | "laravel", 8 | "notifications", 9 | "notification-channel", 10 | "rocketchat", 11 | "rocket.chat", 12 | "chat" 13 | ], 14 | "authors": [ 15 | { 16 | "name": "Anton Komarev", 17 | "email": "anton@komarev.com", 18 | "homepage": "https://komarev.com", 19 | "role": "Developer" 20 | } 21 | ], 22 | "require": { 23 | "php": "^7.2.0|^8.0", 24 | "guzzlehttp/guzzle": "^6.3|^7.0", 25 | "illuminate/notifications": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 26 | "illuminate/queue": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 27 | "illuminate/support": "^5.6|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0" 28 | }, 29 | "require-dev": { 30 | "mockery/mockery": "^1.3.3", 31 | "phpunit/phpunit": "^11.0" 32 | }, 33 | "autoload": { 34 | "psr-4": { 35 | "NotificationChannels\\RocketChat\\": "src" 36 | } 37 | }, 38 | "autoload-dev": { 39 | "psr-4": { 40 | "NotificationChannels\\RocketChat\\Test\\": "tests" 41 | } 42 | }, 43 | "scripts": { 44 | "test": "vendor/bin/phpunit" 45 | }, 46 | "config": { 47 | "sort-packages": true 48 | }, 49 | "extra": { 50 | "laravel": { 51 | "providers": [ 52 | "NotificationChannels\\RocketChat\\RocketChatServiceProvider" 53 | ] 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Exceptions/CouldNotSendNotification.php: -------------------------------------------------------------------------------- 1 | getResponse()->getBody(); 42 | $code = $exception->getResponse()->getStatusCode(); 43 | 44 | return new static("RocketChat responded with an error `{$code} - {$message}`"); 45 | } 46 | 47 | /** 48 | * Thrown when we're unable to communicate with RocketChat. 49 | * 50 | * @param \Exception $exception 51 | * @return static 52 | */ 53 | public static function couldNotCommunicateWithRocketChat(Exception $exception): self 54 | { 55 | return new static("The communication with RocketChat failed. Reason: {$exception->getMessage()}"); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/RocketChat.php: -------------------------------------------------------------------------------- 1 | http = $http; 33 | $this->url = rtrim($url, '/'); 34 | $this->token = $token; 35 | $this->defaultChannel = $defaultChannel; 36 | } 37 | 38 | /** 39 | * Returns RocketChat token. 40 | * 41 | * @return string 42 | */ 43 | public function getToken(): string 44 | { 45 | return $this->token; 46 | } 47 | 48 | /** 49 | * Returns default channel id or name. 50 | * 51 | * @return string|null 52 | */ 53 | public function getDefaultChannel(): ?string 54 | { 55 | return $this->defaultChannel; 56 | } 57 | 58 | /** 59 | * Send a message. 60 | * 61 | * @param string $to 62 | * @param array $message 63 | * @return void 64 | */ 65 | public function sendMessage(string $to, array $message): void 66 | { 67 | $url = sprintf('%s/hooks/%s', $this->url, $this->token); 68 | 69 | $this->post($url, [ 70 | 'json' => array_merge($message, [ 71 | 'channel' => $to, 72 | ]), 73 | ]); 74 | } 75 | 76 | /** 77 | * Perform a simple post request. 78 | * 79 | * @param string $url 80 | * @param array $options 81 | * @return void 82 | */ 83 | private function post(string $url, array $options): void 84 | { 85 | $this->http->post($url, $options); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/RocketChatAttachment.php: -------------------------------------------------------------------------------- 1 | setPropertiesFromArray($data); 69 | } 70 | 71 | /** 72 | * Create a new instance of RocketChatAttachment. 73 | * 74 | * @param array $data 75 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 76 | */ 77 | public static function create(array $data = []) 78 | { 79 | return new self($data); 80 | } 81 | 82 | /** 83 | * @param string $color 84 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 85 | */ 86 | public function color(string $color): self 87 | { 88 | $this->color = $color; 89 | 90 | return $this; 91 | } 92 | 93 | /** 94 | * @param string $text 95 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 96 | */ 97 | public function text(string $text): self 98 | { 99 | $this->text = $text; 100 | 101 | return $this; 102 | } 103 | 104 | /** 105 | * @param string|\DateTimeInterface $timestamp 106 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 107 | */ 108 | public function timestamp($timestamp): self 109 | { 110 | if (! ($timestamp instanceof DateTimeInterface) && ! is_string($timestamp)) { 111 | $invalidType = is_object($timestamp) 112 | ? get_class($timestamp) 113 | : gettype($timestamp); 114 | 115 | throw new InvalidArgumentException(sprintf( 116 | 'Timestamp must be string or DateTime, %s given.', 117 | $invalidType 118 | )); 119 | } 120 | 121 | if ($timestamp instanceof DateTimeInterface) { 122 | $timestamp = $timestamp->format(DateTimeInterface::RFC3339); 123 | } 124 | 125 | $this->timestamp = $timestamp; 126 | 127 | return $this; 128 | } 129 | 130 | /** 131 | * @param string $thumbnailUrl 132 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 133 | */ 134 | public function thumbnailUrl(string $thumbnailUrl): self 135 | { 136 | $this->thumbnailUrl = $thumbnailUrl; 137 | 138 | return $this; 139 | } 140 | 141 | /** 142 | * @param string $messageLink 143 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 144 | */ 145 | public function messageLink(string $messageLink): self 146 | { 147 | $this->messageLink = $messageLink; 148 | 149 | return $this; 150 | } 151 | 152 | /** 153 | * @param bool $collapsed 154 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 155 | */ 156 | public function collapsed(bool $collapsed): self 157 | { 158 | $this->collapsed = $collapsed; 159 | 160 | return $this; 161 | } 162 | 163 | /** 164 | * @param string $name 165 | * @param string $link 166 | * @param string $icon 167 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 168 | */ 169 | public function author(string $name, string $link = '', string $icon = ''): self 170 | { 171 | $this->authorName($name); 172 | $this->authorLink($link); 173 | $this->authorIcon($icon); 174 | 175 | return $this; 176 | } 177 | 178 | /** 179 | * @param string $authorName 180 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 181 | */ 182 | public function authorName(string $authorName): self 183 | { 184 | $this->authorName = $authorName; 185 | 186 | return $this; 187 | } 188 | 189 | /** 190 | * @param string $authorLink 191 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 192 | */ 193 | public function authorLink(string $authorLink): self 194 | { 195 | $this->authorLink = $authorLink; 196 | 197 | return $this; 198 | } 199 | 200 | /** 201 | * @param string $authorIcon 202 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 203 | */ 204 | public function authorIcon(string $authorIcon): self 205 | { 206 | $this->authorIcon = $authorIcon; 207 | 208 | return $this; 209 | } 210 | 211 | /** 212 | * @param string $title 213 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 214 | */ 215 | public function title(string $title): self 216 | { 217 | $this->title = $title; 218 | 219 | return $this; 220 | } 221 | 222 | /** 223 | * @param string $titleLink 224 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 225 | */ 226 | public function titleLink(string $titleLink): self 227 | { 228 | $this->titleLink = $titleLink; 229 | 230 | return $this; 231 | } 232 | 233 | /** 234 | * @param bool $titleLinkDownload 235 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 236 | */ 237 | public function titleLinkDownload(bool $titleLinkDownload): self 238 | { 239 | $this->titleLinkDownload = $titleLinkDownload; 240 | 241 | return $this; 242 | } 243 | 244 | /** 245 | * @param string $imageUrl 246 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 247 | */ 248 | public function imageUrl(string $imageUrl): self 249 | { 250 | $this->imageUrl = $imageUrl; 251 | 252 | return $this; 253 | } 254 | 255 | /** 256 | * @param string $audioUrl 257 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 258 | */ 259 | public function audioUrl(string $audioUrl): self 260 | { 261 | $this->audioUrl = $audioUrl; 262 | 263 | return $this; 264 | } 265 | 266 | /** 267 | * @param string $videoUrl 268 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 269 | */ 270 | public function videoUrl(string $videoUrl): self 271 | { 272 | $this->videoUrl = $videoUrl; 273 | 274 | return $this; 275 | } 276 | 277 | /** 278 | * @param array $fields 279 | * @return \NotificationChannels\RocketChat\RocketChatAttachment 280 | */ 281 | public function fields(array $fields): self 282 | { 283 | $this->fields = $fields; 284 | 285 | return $this; 286 | } 287 | 288 | /** 289 | * Get an array representation of the RocketChatAttachment. 290 | * 291 | * @return array 292 | */ 293 | public function toArray(): array 294 | { 295 | return array_filter([ 296 | 'color' => $this->color, 297 | 'text' => $this->text, 298 | 'ts' => $this->timestamp, 299 | 'thumb_url' => $this->thumbnailUrl, 300 | 'message_link' => $this->messageLink, 301 | 'collapsed' => $this->collapsed, 302 | 'author_name' => $this->authorName, 303 | 'author_link' => $this->authorLink, 304 | 'author_icon' => $this->authorIcon, 305 | 'title' => $this->title, 306 | 'title_link' => $this->titleLink, 307 | 'title_link_download' => $this->titleLinkDownload, 308 | 'image_url' => $this->imageUrl, 309 | 'audio_url' => $this->audioUrl, 310 | 'video_url' => $this->videoUrl, 311 | 'fields' => $this->fields, 312 | ]); 313 | } 314 | 315 | /** 316 | * Set attachment data from array. 317 | * 318 | * @param array $data 319 | * @return void 320 | */ 321 | private function setPropertiesFromArray(array $data): void 322 | { 323 | foreach ($data as $key => $value) { 324 | $methodName = Str::camel($key); 325 | 326 | if (! method_exists($this, $methodName)) { 327 | continue; 328 | } 329 | 330 | $this->{$methodName}($value); 331 | } 332 | } 333 | } 334 | -------------------------------------------------------------------------------- /src/RocketChatMessage.php: -------------------------------------------------------------------------------- 1 | content($content); 49 | } 50 | 51 | public function getChannel(): ?string 52 | { 53 | return $this->channel; 54 | } 55 | 56 | public function getFrom(): ?string 57 | { 58 | return $this->from; 59 | } 60 | 61 | /** 62 | * Set the sender's access token. 63 | * 64 | * @param string $accessToken 65 | * @return $this 66 | */ 67 | public function from(string $accessToken): self 68 | { 69 | $this->from = $accessToken; 70 | 71 | return $this; 72 | } 73 | 74 | /** 75 | * Set the RocketChat channel the message should be sent to. 76 | * 77 | * @param string $channel 78 | * @return $this 79 | */ 80 | public function to(string $channel): self 81 | { 82 | $this->channel = $channel; 83 | 84 | return $this; 85 | } 86 | 87 | /** 88 | * Set the sender's alias. 89 | * 90 | * @param string $alias 91 | * @return $this 92 | */ 93 | public function alias(string $alias): self 94 | { 95 | $this->alias = $alias; 96 | 97 | return $this; 98 | } 99 | 100 | /** 101 | * Set the sender's emoji. 102 | * 103 | * @param string $emoji 104 | * @return $this 105 | */ 106 | public function emoji(string $emoji): self 107 | { 108 | $this->emoji = $emoji; 109 | 110 | return $this; 111 | } 112 | 113 | /** 114 | * Set the sender's avatar. 115 | * 116 | * @param string $avatar 117 | * @return $this 118 | */ 119 | public function avatar(string $avatar): self 120 | { 121 | $this->avatar = $avatar; 122 | 123 | return $this; 124 | } 125 | 126 | /** 127 | * Set the content of the RocketChat message. 128 | * Supports GitHub flavoured markdown. 129 | * 130 | * @param string $content 131 | * @return $this 132 | */ 133 | public function content(string $content): self 134 | { 135 | $this->content = $content; 136 | 137 | return $this; 138 | } 139 | 140 | /** 141 | * Add an attachment to the message. 142 | * 143 | * @param array|\NotificationChannels\RocketChat\RocketChatAttachment $attachment 144 | * @return $this 145 | */ 146 | public function attachment($attachment): self 147 | { 148 | if (! ($attachment instanceof RocketChatAttachment)) { 149 | $attachment = new RocketChatAttachment($attachment); 150 | } 151 | 152 | $this->attachments[] = $attachment; 153 | 154 | return $this; 155 | } 156 | 157 | /** 158 | * Add multiple attachments to the message. 159 | * 160 | * @param array|\NotificationChannels\RocketChat\RocketChatAttachment[] $attachments 161 | * @return $this 162 | */ 163 | public function attachments(array $attachments): self 164 | { 165 | foreach ($attachments as $attachment) { 166 | $this->attachment($attachment); 167 | } 168 | 169 | return $this; 170 | } 171 | 172 | /** 173 | * Get an array representation of the RocketChatMessage. 174 | * 175 | * @return array 176 | */ 177 | public function toArray(): array 178 | { 179 | $attachments = []; 180 | 181 | foreach ($this->attachments as $attachment) { 182 | $attachments[] = $attachment->toArray(); 183 | } 184 | 185 | return array_filter([ 186 | 'text' => $this->content, 187 | 'channel' => $this->channel, 188 | 'alias' => $this->alias, 189 | 'emoji' => $this->emoji, 190 | 'avatar' => $this->avatar, 191 | 'attachments' => $attachments, 192 | ]); 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /src/RocketChatServiceProvider.php: -------------------------------------------------------------------------------- 1 | app 21 | ->when(RocketChatWebhookChannel::class) 22 | ->needs(RocketChat::class) 23 | ->give(function () { 24 | return new RocketChat( 25 | new HttpClient(), 26 | Config::get('services.rocketchat.url'), 27 | Config::get('services.rocketchat.token'), 28 | Config::get('services.rocketchat.channel') 29 | ); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/RocketChatWebhookChannel.php: -------------------------------------------------------------------------------- 1 | rocketChat = $rocketChat; 26 | } 27 | 28 | /** 29 | * Send the given notification. 30 | * 31 | * @param mixed $notifiable 32 | * @param \Illuminate\Notifications\Notification $notification 33 | * @return void 34 | * 35 | * @throws \NotificationChannels\RocketChat\Exceptions\CouldNotSendNotification 36 | */ 37 | public function send($notifiable, Notification $notification): void 38 | { 39 | /** @var \NotificationChannels\RocketChat\RocketChatMessage $message */ 40 | $message = $notification->toRocketChat($notifiable); 41 | 42 | $to = $message->getChannel() ?: $notifiable->routeNotificationFor('RocketChat'); 43 | $to = $to ?: $this->rocketChat->getDefaultChannel(); 44 | if ($to === null) { 45 | throw CouldNotSendNotification::missingTo(); 46 | } 47 | 48 | $from = $message->getFrom() ?: $this->rocketChat->getToken(); 49 | if (! $from) { 50 | throw CouldNotSendNotification::missingFrom(); 51 | } 52 | 53 | try { 54 | $this->sendMessage($to, $message); 55 | } catch (ClientException $exception) { 56 | throw CouldNotSendNotification::rocketChatRespondedWithAnError($exception); 57 | } catch (Exception $exception) { 58 | throw CouldNotSendNotification::couldNotCommunicateWithRocketChat($exception); 59 | } 60 | } 61 | 62 | /** 63 | * @param string $to 64 | * @param \NotificationChannels\RocketChat\RocketChatMessage $message 65 | * @return void 66 | */ 67 | private function sendMessage(string $to, RocketChatMessage $message): void 68 | { 69 | $this->rocketChat->sendMessage($to, $message->toArray()); 70 | } 71 | } 72 | --------------------------------------------------------------------------------