├── .editorconfig ├── .github └── workflows │ ├── php-cs.yml │ └── tests.yml ├── .php_cs.dist.php ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── google-chat.php └── src ├── Card.php ├── Components └── Button │ ├── AbstractButton.php │ ├── ImageButton.php │ └── TextButton.php ├── Concerns └── ValidatesCardComponents.php ├── Enums ├── Icon.php └── ImageStyle.php ├── Exceptions └── CouldNotSendNotification.php ├── GoogleChatChannel.php ├── GoogleChatMessage.php ├── GoogleChatServiceProvider.php ├── Section.php └── Widgets ├── AbstractWidget.php ├── Buttons.php ├── Image.php ├── KeyValue.php └── TextParagraph.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 | -------------------------------------------------------------------------------- /.github/workflows/php-cs.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | php-cs-fixer: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v2 12 | with: 13 | ref: ${{ github.head_ref }} 14 | 15 | - name: Run PHP CS Fixer 16 | uses: docker://oskarstark/php-cs-fixer-ga:2.18.6 17 | with: 18 | args: --config=.php_cs.dist.php --allow-risky=yes 19 | 20 | - name: Commit changes 21 | uses: stefanzweifel/git-auto-commit-action@v4 22 | with: 23 | commit_message: Fix styling 24 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: PHPUnit tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | tests: 9 | runs-on: ubuntu-latest 10 | strategy: 11 | fail-fast: true 12 | matrix: 13 | os: [ubuntu-latest, windows-latest] 14 | php: [8.2, 8.1] 15 | stability: [prefer-lowest, prefer-stable] 16 | 17 | name: PHP ${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }} 18 | 19 | steps: 20 | - name: Checkout code 21 | uses: actions/checkout@v2 22 | 23 | - name: Setup PHP 24 | uses: shivammathur/setup-php@v2 25 | with: 26 | php-version: ${{ matrix.php }} 27 | tools: composer:v2 28 | coverage: none 29 | 30 | - name: Setup problem matchers 31 | run: | 32 | echo "::add-matcher::${{ runner.tool_cache }}/php.json" 33 | echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json" 34 | 35 | - name: Install dependencies 36 | run: | 37 | composer update --${{ matrix.stability }} --prefer-dist --no-interaction 38 | 39 | - name: Execute tests 40 | run: vendor/bin/phpunit 41 | env: 42 | GOOGLE_CHAT_TEST_SPACE: ${{ secrets.GOOGLE_CHAT_TEST_SPACE }} 43 | -------------------------------------------------------------------------------- /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | notPath('bootstrap/*') 5 | ->notPath('storage/*') 6 | ->notPath('resources/view/mail/*') 7 | ->in([ 8 | __DIR__ . '/src', 9 | __DIR__ . '/tests', 10 | ]) 11 | ->name('*.php') 12 | ->notName('*.blade.php') 13 | ->ignoreDotFiles(true) 14 | ->ignoreVCS(true); 15 | 16 | return PhpCsFixer\Config::create() 17 | ->setRules([ 18 | '@PSR2' => true, 19 | 'array_syntax' => ['syntax' => 'short'], 20 | 'ordered_imports' => ['sortAlgorithm' => 'alpha'], 21 | 'no_unused_imports' => true, 22 | 'not_operator_with_successor_space' => true, 23 | 'trailing_comma_in_multiline_array' => true, 24 | 'phpdoc_scalar' => true, 25 | 'unary_operator_spaces' => true, 26 | 'binary_operator_spaces' => true, 27 | 'blank_line_before_statement' => [ 28 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 29 | ], 30 | 'phpdoc_single_line_var_spacing' => true, 31 | 'phpdoc_var_without_name' => true, 32 | 'class_attributes_separation' => [ 33 | 'elements' => [ 34 | 'method', 35 | ], 36 | ], 37 | 'method_argument_space' => [ 38 | 'on_multiline' => 'ensure_fully_multiline', 39 | 'keep_multiple_spaces_after_comma' => true, 40 | ], 41 | 'single_trait_insert_per_statement' => true, 42 | ]) 43 | ->setFinder($finder); 44 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | disabled: 3 | - laravel_braces 4 | enabled: 5 | - psr12_braces -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `google-chat` will be documented in this file 4 | 5 | ## 3.0.0 - 2022-04-30 6 | - Correct passing of notification instance instead of notifiable instance 7 | 8 | ## 2.0.1 - 2022-04-09 9 | - Corrected Laravel Framework version constraints, preventing installations on Laravel versions >=9.1 10 | 11 | ## 2.0.0 - 2022-02-12 12 | - Upgraded to support Laravel 9.x (Minimum of 9.0.2 due to an [unintended breaking change](https://github.com/laravel/framework/pull/40880)) 13 | - Dropped support for PHP 7.3 and 7.4 14 | 15 | ## 1.0.1 - 2021-07-16 16 | - Removed property type hints for PHP 7.3 compatibility 17 | 18 | ## 1.0.0 - 2021-05-18 19 | 20 | - Initial Release - Woo-hoo! 21 | -------------------------------------------------------------------------------- /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) Frank Dixon 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 |

2 | 3 |

4 | 5 |

6 | Latest Version on Packagist 7 | Software License 8 | GitHub Tests Action Status 9 | GitHub Code Style Action 10 | Total Downloads 11 | PHP Version Requirements 12 | Laravel Version Requirements 13 |

14 | 15 |

Google Chat - Laravel Notification Channel

16 | 17 | This package makes it easy to send notifications using [Google Chat](https://developers.google.com/hangouts/chat) , (formerly known as Hangouts Chat) with Laravel 9.x 18 | 19 | ````php 20 | class InvoicePaidNotification extends Notification 21 | { 22 | // Create a super simple message 23 | public function toGoogleChat($notifiable) 24 | { 25 | return GoogleChatMessage::create('An invoice was paid!'); 26 | } 27 | 28 | // ...Or you can use some simple formatting 29 | public function toGoogleChat($notifiable) 30 | { 31 | return GoogleChatMessage::create() 32 | ->text('Someone just paid an invoice... ') 33 | ->bold('Woo-hoo!') 34 | ->line('Looking for ') 35 | ->link(route('invoices'), 'the details?') 36 | ->to('sales_team'); // ... and route it to specific rooms 37 | } 38 | 39 | // ...You can even make a fancy card! 40 | public function toGoogleChat($notifiable) 41 | { 42 | return GoogleChatMessage::create() 43 | ->text('Invoice Paid! Here\'s the details:') 44 | ->card( 45 | Card::create( 46 | Section::create( 47 | KeyValue::create('Amount', '$520.99', '#10004756') 48 | ->onClick(route('invoices')) 49 | ->button(TextButton::create(route('invoices'), 'View')) 50 | ) 51 | ) 52 | ) 53 | } 54 | } 55 | ```` 56 | 57 | > For Laravel 8.x, please use version 1.x of this package. 58 | 59 | ## Contents 60 | 61 | - [Installation](#installation) 62 | - [Generating a Webhook](#generating-a-webhook) 63 | - [Configuring & Using Webhooks in Your Application](#configuring-&-using-webhooks-in-your-application) 64 | - [Default Room](#default-room) 65 | - [Alternate Rooms (Preferred)](#alternate-rooms-preferred) 66 | - [Explicit Webhook Routing](#explicit-webhook-routing) 67 | - [Usage](#usage) 68 | - [Simple Messages](#simple-messages) 69 | - [Card Messages](#card-messages) 70 | - [API Overview](#api-overview) 71 | - [Google Chat Message](#google-chat-message) 72 | - [Card Layout](#card-layout) 73 | - [Widgets](#widgets) 74 | - [Components](#components) 75 | - [Changelog](#changelog) 76 | - [Testing](#testing) 77 | - [Security](#security) 78 | - [Contributing](#contributing) 79 | - [Credits](#credits) 80 | - [License](#license) 81 | 82 | 83 | ## Installation 84 | 85 | The Google Chat notification channel can be installed easily via Composer: 86 | 87 | ````bash 88 | $ composer require laravel-notification-channels/google-chat 89 | ```` 90 | 91 | ### Generating a Webhook 92 | 93 | This package makes use of Google Chat's convenient 'Webhooks' feature, which allows for quick and easy setup. 94 | 95 | You can learn how to create a room, and generate a new Webhook on [Google's documentation](https://developers.google.com/hangouts/chat/how-tos/webhooks). 96 | 97 | > Throughout Google's own documentation, the term 'space' is used to reference conversations generally across Google Chat, whether that be a one-on-one chat between co-workers, or a 'room' with conversations between multiple people. 98 | > 99 | > Although this package only has the ability to send messages into 'rooms', **we still refer to a room as a 'space' for consistency** between this package's documentation, and Google's. 100 | 101 | ## Configuring & Using Webhooks in Your Application 102 | 103 | Firstly, let's publish the configuration file used by the Google Chat notification channel into your application, so that we can start configuring our webhooks: 104 | 105 | ````bash 106 | $ php artisan vendor:publish --tag google-chat-config 107 | ```` 108 | 109 | ### Default Room 110 | 111 | If we only have a single room / webhook we want to post into, we can simply configure the `space` key which defines the default conversation notifications sent via the Google Chat channel will be posted to. 112 | 113 | ````php 114 | // config/google-chat.php 115 | 116 | return [ 117 | 'space' => 'https://chat.googleapis.com/room-webhook-for-all-notifications?key=xxxxx' 118 | ] 119 | ```` 120 | 121 | Notifications that have not otherwise been directed to another room will now be sent to this default space. 122 | 123 | **CAUTION!** If your application sends sensitive notifications via Google Chat, we recommend you configure the `space` key to `NULL`, so that notifications must be explicitly directed to an endpoint. 124 | 125 | ### Alternate Rooms (Preferred) 126 | 127 | You can also define alternate webhooks under the `spaces` (plural) key, and reference these more easily throughout your application: 128 | 129 | ````php 130 | // config/google-chat.php 131 | 132 | return [ 133 | 'spaces' => [ 134 | 'sales_team' => 'https://chat.googleapis.com/sales-team-room?key=xxxxx', 135 | 'dev_team' => 'https://chat.googleapis.com/dev-team-room?key=xxxxx', 136 | 'executive' => 'https://chat.googleapis.com/executives?key=xxxxx', 137 | ] 138 | ] 139 | ```` 140 | 141 | You can now direct notifications to one of these configured rooms by using the relevant key anywhere you can route notifications, like: 142 | 143 | ````php 144 | Notification::route('googleChat', 'sales_team')->... 145 | 146 | // Or 147 | 148 | GoogleChatMessage::create()->to('dev_team')->... 149 | 150 | // Or, even in your notifiable class: 151 | public function routeNotificationForGoogleChat() 152 | { 153 | return 'executive'; 154 | } 155 | ```` 156 | 157 | ### Explicit Webhook Routing 158 | 159 | If needed, you can route notifications to explicit webhooks in all the same places listed above. This isn't the preferred method, however, as it gives you less flexibility should you ever need to change a webhook. 160 | 161 | ````php 162 | Notification::route('googleChat', 'https://chat.googleapis.com/xxxxx')->... 163 | 164 | // Or 165 | 166 | GoogleChatMessage::create()->to('https://chat.googleapis.com/xxxxx')->... 167 | 168 | // Or, even in your notifiable class: 169 | public function routeNotificationForGoogleChat() 170 | { 171 | return 'https://chat.googleapis.com/xxxxx'; 172 | } 173 | ```` 174 | 175 | ## Usage 176 | 177 | In order to send a notification via the Google Chat channel, you'll need to specify the channel in the `via()` method of your notification: 178 | 179 | ````php 180 | use NotificationChannels\GoogleChat\GoogleChatChannel; 181 | 182 | // ... 183 | 184 | public function via($notifiable) 185 | { 186 | return [ 187 | GoogleChatChannel::class 188 | ] 189 | } 190 | ```` 191 | 192 | > If you haven't already, make sure you understand [how notification classes are constructed](https://laravel.com/docs/8.x/notifications). 193 | 194 | Google Chat messages have [two formats](https://developers.google.com/hangouts/chat/reference/message-formats): Simple Messages, and Card Messages. This package allows you to construct both. 195 | 196 | ### Simple Messages 197 | 198 | Simple messages can be created easily using a `NotificationChannels\GoogleChat\GoogleChatMessage` instance directly, and returned in your notification class like so: 199 | 200 | ````php 201 | use NotificationChannels\GoogleChat\GoogleChatMessage; 202 | 203 | public function toGoogleChat($notifiable) 204 | { 205 | return GoogleChatMessage::create('Hello world!'); 206 | } 207 | ```` 208 | 209 | Simple messages can also contain basic formatting: 210 | 211 | ````php 212 | use NotificationChannels\GoogleChat\GoogleChatMessage; 213 | 214 | public function toGoogleChat($notifiable) 215 | { 216 | return GoogleChatMessage::create() 217 | ->bold('Heads Up!') 218 | ->line('An error was encountered whilst communicating with an external service:') 219 | ->monospaceBlock($this->errorMessage) 220 | ->italic('Want to know more? ') 221 | ->link('https://status.example.com/logs', 'Check Out the Logs.'); 222 | } 223 | ```` 224 | 225 | ### Card Messages 226 | 227 | Google Chat cards are more complex pieces of UI that can display organized information to the recipient. Cards are added to a `GoogleChatMessage` instance, and can be used in combination with a simple message. 228 | 229 | The structure of cards in this package closely resembles the actual data format sent to the webhook endpoint. For this reason, it's worth checking out [how cards are structured](https://developers.google.com/hangouts/chat/reference/message-formats/cards). 230 | 231 | ````php 232 | use NotificationChannels\GoogleChat\GoogleChatMessage; 233 | use NotificationChannels\GoogleChat\Card; 234 | use NotificationChannels\GoogleChat\Section; 235 | use NotificationChannels\GoogleChat\Widgets\KeyValue; 236 | use NotificationChannels\GoogleChat\Enums\Icon; 237 | use NotificationChannels\GoogleChat\Enums\ImageStyle; 238 | 239 | public function toGoogleChat($notifiable) 240 | { 241 | return GoogleChatMessage::create() 242 | ->text('An invoice was just paid... ') 243 | ->bold('Woo-hoo!') 244 | ->card( 245 | Card::create() 246 | ->header( 247 | 'Invoice Paid', 248 | '#1004756', 249 | 'https://cdn.example.com/avatars/xxx.png', 250 | ImageStyle::CIRCULAR 251 | ) 252 | ->section( 253 | Section::create( 254 | KeyValue::create( 255 | 'Payment Received', 256 | '$20.14', 257 | 'Paid by Josephine Smith' 258 | ) 259 | ->icon(Icon::DOLLAR) 260 | ->onClick(route('invoice.show')) 261 | ) 262 | ) 263 | ) 264 | } 265 | ```` 266 | 267 | **Visual learner?** Us too. Here's a visual overview of the card structure: 268 | 269 | ```` 270 | cards 271 | | 272 | |---card 273 | | | 274 | | |---header (complex) 275 | | | ... 276 | | | 277 | | |---sections 278 | | | | 279 | | | |---section 280 | | | | | 281 | | | | |---header (simple) 282 | | | | | ... 283 | | | | | 284 | | | | |---widgets 285 | | | | | | 286 | | | | | |---widget 287 | | | | | | ... 288 | | | | | | 289 | | | | | |---widget 290 | | | | | | ... 291 | | | | 292 | | | |---section 293 | | | | ... 294 | | 295 | |---card 296 | | ... 297 | ```` 298 | 299 | ## API Overview 300 | 301 | ### Google Chat Message 302 | 303 | Namespace: `NotificationChannels\GoogleChat\GoogleChatMessage` 304 | 305 | The `GoogleChatMessage` class encompasses an entire message that will be sent to the Google Chat room. 306 | 307 | - `static create(?string $text)` Instantiates and returns a new `GoogleChatMessage` instance, optionally pre-configuring it with the provided simple text 308 | - `to(string $space)` Specifies the webhook or space key this notification will be sent to. This takes precedence over the default space and any value returned by a notifiable's `routeNotificationForGoogleChat()` method 309 | - `text(string $message)` Appends `$message` to the simple message content 310 | - `line(string $message)` Appends `$message` on a new line 311 | - `bold(string $message)` Appends bold text 312 | - `italic(string $message)` Appends italic text 313 | - `strikethrough(string $message)` Appends strikethorugh text 314 | - `strike(string $message)` Alias for `strikethrough()` 315 | - `monospace(string $message)` Appends monospace text 316 | - `mono(string $message)` Alias for `monospace()` 317 | - `monospaceBlock(string $message)` Appends a block of monospace text 318 | - `link(string $link, ?string $displayText)` Appends a text link, optionally with custom display text 319 | - `mention(string $userId)` Appends a mention text targeting a specific user id 320 | - `mentionAll(?string $prependText, ?string $appendText)` Appends a mention-all text, optionally with text before and after the block 321 | - `card(Card|Card[] $card)` Add one or more complex card UIs to the message 322 | 323 | ### Card Layout 324 | 325 | The layout is split into two concepts: The card, and the section. The card can be thought of as the container, whilst the sections can be thought of as rows within the card itself. The card can have a complex, overarching header, whilst each section can contain a simple text based header. 326 | 327 | You can add multiple sections to a card, in order to group related pieces of information. 328 | 329 | #### Card 330 | 331 | Namespace: `NotificationChannels\GoogleChat\Card` 332 | 333 | The `Card` class represents the top level layout definition for a Card UI to be sent in a message. Cards define one or more sections, and may optionally define header information 334 | 335 | - `static create(Section|Section[]|null $section)` Instantiates and returns a new `Card` instance, optionally pre-configuring it with the provided section or sections 336 | - `header(string $title, ?string $subtitle, ?string $imageUrl, ?string $imageStyle)` Optional - Configures the header UI for the card. Note that `$imageStyle` is one of the constants defined in `NotificationChannels\GoogleChat\Enums\ImageStyle` 337 | - `section(Section|Section[] $section)` Add one or more sections to the card 338 | 339 | #### Section 340 | 341 | Namespace: `NotificationChannels\GoogleChat\Section` 342 | 343 | The `Section` class defines the intermediary layout definition of a card. From a UI perspective, it groups related widgets. 344 | 345 | - `static create(AbstractWidget|AbstractWidget[]|null $widgets)` Instantiates and returns a new `Section` instance, optionally pre-configuring it with the provided widget or widgets 346 | - `header(string $text)` Optionally defines the simple header displayed at the top of the section 347 | - `widget(AbstractWidget|AbstractWidgets[] $widget)` Adds one or more widgets to the section 348 | 349 | ### Widgets 350 | 351 | Widgets are the meaningful pieces of UI displayed throughout a single card. There are different types of widgets, in order to display information more appropriately to the user. 352 | 353 | Widgets are added to a section, and a section can contain multiple widgets of various types. 354 | 355 | #### Text Paragraph 356 | 357 | Namespace: `NotificationChannels\GoogleChat\Widgets\TextParagraph` 358 | 359 | The `TextParagraph` widget defines rich text. This widget can define more complex text formats than permissible in a simple message. 360 | 361 | - `static create(?string $message)` Instantiates and returns a new `TextParagraph` instance, optionally pre-configuring it with the provided text 362 | - `text(string $message)` Appends the `$message` to the widget content 363 | - `bold(string $message)` Appends bold text 364 | - `italic(string $message)` Appends italic text 365 | - `underline(string $message)` Appends underline text 366 | - `strikethrough(string $message)` Appends strikethrough text 367 | - `strike(string $message)` Alias for `strikethrough()` 368 | - `color(string $message, string $hex)` Appends colored text according to the `$hex` color 369 | - `link(string $link, ?string $displayText)` Appends a textual link, optionally with the provided display text 370 | - `break()` Appends a line break 371 | 372 | #### Key Value 373 | 374 | Namespace: `NotificationChannels\GoogleChat\Widgets\KeyValue` 375 | 376 | The `KeyValue` widget defines a table like element that can segment information and provide an external click through 377 | 378 | - `static create(?string $topLabel, ?string $content, ?string $bottomLabel)` Instantiates and returns a new `KeyValue` instance, optionally pre-configuring it with a top label, content and bottom label. 379 | - `topLabel(string $message)` Defines the top label text 380 | - `content(string $content)` Defines the primary text content of the widget 381 | - `bottomLabel(string $message)` Defines the bottom label text 382 | - `setContentMultiline(bool $value)` Determines whether the primary content should flow onto multiple lines. Google defaults this value to `false` 383 | - `onClick(string $url)` Defines a click through URL which can be activated by clicking the widget itself. Note that this is a different definition from the button, which may optionally be added to the widget too. 384 | - `icon(string $icon)` Defines the glyph icon displayed with the text content; One of the constants defined in `NotificationChannels\GoogleChat\Enums\Icon` 385 | - `button(AbstractButton $button)` Optionally defines a button displayed alongside the text content 386 | 387 | #### Image 388 | 389 | Namespace: `NotificationChannels\GoogleChat\Widgets\Image` 390 | 391 | The `Image` widget defines a simple image to be displayed in the card. Optionally, a click through URL can be configured for when a user clicks/taps on the image. 392 | 393 | - `static create(?string $imageUrl, ?string $onClickUrl)` Instantiates and returns a new `Image` instance, optionally pre-configuring it with an image URL and click through URL. 394 | - `imageUrl(string $url)` Defines the image URL where the image can be sourced 395 | - `onClick(string $url)` Defines a URL the user will be taken to if they click/tap on the image 396 | 397 | #### Buttons 398 | 399 | Namespace: `NotificationChannels\GoogleChat\Widgets\Buttons` 400 | 401 | The `Buttons` widget acts as a container for one or more buttons, laid out horizontally. This widget accepts instances of `NotificationChannels\GoogleChat\Components\Button\AbstractButton` and can accept buttons of different types. 402 | 403 | - `static create(AbstractButton|AbstractButton[]|null $buttons)` Instantiates and returns a new `Buttons` instance, optionally pre-configuring it with the provided buttons 404 | - `button(AbstractButton|AbstractButton[] $button)` Adds one or more buttons 405 | 406 | ### Components 407 | 408 | Components are structures that are nestled within widgets. For simplicity, the Google Chat notification channel only supports button components. 409 | 410 | Both the Text Button and Image Button can be nested within the `Buttons` widget, as well as in the button properties of the `KeyValue` and `ImageWidget`. 411 | 412 | #### Text Button 413 | 414 | Namespace: `NotificationChannels\GoogleChat\Components\Button\TextButton` 415 | 416 | The `TextButton` defines a simple text button, and can be accepted anywhere that an `AbstractButton` is accepted. 417 | 418 | - `static create(?string $url, ?string $displayText)` Instantiates and returns a new `TextButton` instance, optionally pre-configuring it with the provided URL and display text 419 | - `url(string $url)` Defines the target endpoint for the button 420 | - `text(string $text)` Defines the display text for the button 421 | 422 | #### Image Button 423 | 424 | Namespace: `NotificationChannels\GoogleChat\Components\Button\ImageButton` 425 | 426 | The `ImageButton` defines a clickable icon or image, and can be accepted anywhere that an `AbstractButton` is accepted. The icon can either be a default icon (one of the constants defined in `NotificationChannels\GoogleChat\Enums\Icon`) or an external image url. 427 | 428 | - `static create(?string $url, ?string $icon)` Instantiates and returns a new `ImageButton` instance, optionally pre-configuring it with the provided URL and icon 429 | - `url(string $url)` Defines the target endpoint for the button 430 | - `icon(string $icon)` Defines the icon or image to display for the button. 431 | 432 | ## Changelog 433 | 434 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 435 | 436 | ## Testing 437 | 438 | ``` bash 439 | $ composer test 440 | ``` 441 | 442 | The test suite also includes one end-to-end test. In order for this test to pass, a `GOOGLE_CHAT_TEST_SPACE` environment variable should be set, containing a webhook to a test room. 443 | 444 | Alternatively, you can exclude this test with PHPUnit during local development: 445 | 446 | ````bash 447 | $ ./vendor/bin/phpunit --exclude-group external 448 | ```` 449 | 450 | ## Security 451 | 452 | If you discover any security related issues, please email frank@thetreehouse.family instead of using the issue tracker. 453 | 454 | ## Contributing 455 | 456 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 457 | 458 | ## Credits 459 | 460 | - [Frank Dixon](https://github.com/frankieeedeee) 461 | - [All Contributors](../../contributors) 462 | 463 | ## License 464 | 465 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 466 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "laravel-notification-channels/google-chat", 3 | "description": "Google Chat Notification Channel for Laravel (fka. Hangouts Chat)", 4 | "homepage": "https://github.com/laravel-notification-channels/google-chat", 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Frank Dixon", 9 | "email": "frank@thetreehouse.family", 10 | "homepage": "https://thetreehouse.family", 11 | "role": "Developer" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=8.0", 16 | "guzzlehttp/guzzle": "^6.3 || ^7.0", 17 | "illuminate/notifications": "^9.0.2 || ^10.0 || ^11.0", 18 | "illuminate/support": "^9.0.2 || ^10.0 || ^11.0" 19 | }, 20 | "require-dev": { 21 | "orchestra/testbench": "^7.0 || ^9.0", 22 | "phpunit/phpunit": "^9.5.10 || ^10.5" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "NotificationChannels\\GoogleChat\\": "src" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "NotificationChannels\\GoogleChat\\Tests\\": "tests" 32 | } 33 | }, 34 | "scripts": { 35 | "test": "phpunit", 36 | "test:coverage": "phpunit --coverage-text --coverage-clover=coverage.clover" 37 | }, 38 | "config": { 39 | "sort-packages": true 40 | }, 41 | "extra": { 42 | "laravel": { 43 | "providers": [ 44 | "NotificationChannels\\GoogleChat\\GoogleChatServiceProvider" 45 | ] 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /config/google-chat.php: -------------------------------------------------------------------------------- 1 | env('GOOGLE_CHAT_DEFAULT_SPACE', null), 14 | 15 | /** 16 | * Additional Spaces. 17 | * 18 | * This key defines additional spaces which can be used as the argument in the 19 | * `GoogleChatMessage::to({key})` method. For example, using the 'sales_team' 20 | * example key below, we can direct an individual notification to that 21 | * endpoint like: 22 | * 23 | * ```` 24 | * GoogleChatMessage::create('My Message')->to('sales_team'); 25 | * ```` 26 | */ 27 | 'spaces' => [ 28 | // 'sales_team' => 'https://chat.googleapis.com/...' 29 | ], 30 | ]; 31 | -------------------------------------------------------------------------------- /src/Card.php: -------------------------------------------------------------------------------- 1 | [], 20 | ]; 21 | 22 | /** 23 | * Configure the header content of the card. 24 | * 25 | * @param string $title The title of the card, usually the bot or service name 26 | * @param string|null $subtitle Secondary text displayed below the title 27 | * @param string|null $imageUrl Display a particular avatar image for the message 28 | * @param string|null $imageStyle Configure the avatar image style, one of IMAGE or AVATAR 29 | * @return self 30 | */ 31 | public function header(string $title, string $subtitle = null, string $imageUrl = null, string $imageStyle = null): Card 32 | { 33 | $header = [ 34 | 'title' => $title, 35 | ]; 36 | 37 | if ($subtitle) { 38 | $header['subtitle'] = $subtitle; 39 | } 40 | 41 | if ($imageUrl) { 42 | $header['imageUrl'] = $imageUrl; 43 | } 44 | 45 | if ($imageStyle) { 46 | $header['imageStyle'] = $imageStyle; 47 | } 48 | 49 | $this->payload['header'] = $header; 50 | 51 | return $this; 52 | } 53 | 54 | /** 55 | * Add one or more sections to the card. 56 | * 57 | * @param \NotificationChannels\GoogleChat\Section|\NotificationChannels\GoogleChat\Section[] 58 | * @return self 59 | */ 60 | public function section($section): Card 61 | { 62 | $sections = Arr::wrap($section); 63 | 64 | $this->guardOnlyInstancesOf(Section::class, $sections); 65 | 66 | $this->payload['sections'] = array_merge($this->payload['sections'] ?? [], $sections); 67 | 68 | return $this; 69 | } 70 | 71 | /** 72 | * Serialize the card to an array representation. 73 | * 74 | * @return array 75 | */ 76 | public function toArray() 77 | { 78 | return $this->payload; 79 | } 80 | 81 | /** 82 | * Return a new Google Chat Card instance. 83 | * 84 | * @param \NotificationChannels\GoogleChat\Section|\NotificationChannels\GoogleChat\Section[]|null $section 85 | * @return self 86 | */ 87 | public static function create($section = null): Card 88 | { 89 | $card = new static; 90 | 91 | if ($section) { 92 | $card->section($section); 93 | } 94 | 95 | return $card; 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/Components/Button/AbstractButton.php: -------------------------------------------------------------------------------- 1 | payload['onClick'] = [ 26 | 'openLink' => [ 27 | 'url' => $url, 28 | ], 29 | ]; 30 | 31 | return $this; 32 | } 33 | 34 | /** 35 | * Return the array representation of this button. 36 | * 37 | * @return array 38 | */ 39 | public function toArray() 40 | { 41 | $class = Str::of( 42 | Str::of(get_called_class()) 43 | ->explode('\\') 44 | ->last() 45 | ) 46 | ->camel(); 47 | 48 | return [ 49 | (string) $class => $this->payload, 50 | ]; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Components/Button/ImageButton.php: -------------------------------------------------------------------------------- 1 | setIconByName($icon) 17 | : $this->setIconByUrl($icon); 18 | 19 | return $this; 20 | } 21 | 22 | /** 23 | * Set an icon by its name. 24 | * 25 | * @param string $icon 26 | * @return self 27 | */ 28 | public function setIconByName(string $icon): ImageButton 29 | { 30 | $this->payload['icon'] = $icon; 31 | unset($this->payload['iconUrl']); 32 | 33 | return $this; 34 | } 35 | 36 | /** 37 | * Set an icon by url. 38 | * 39 | * @param string $url 40 | * @return self 41 | */ 42 | public function setIconByUrl(string $url): ImageButton 43 | { 44 | $this->payload['iconUrl'] = $url; 45 | unset($this->payload['icon']); 46 | 47 | return $this; 48 | } 49 | 50 | /** 51 | * Create a new image button instance. 52 | * 53 | * @param string|null $url 54 | * @param string|null $icon Either an icon name or URL to the icon image 55 | * @return self 56 | */ 57 | public static function create(string $url = null, string $icon = null): ImageButton 58 | { 59 | $button = new static; 60 | 61 | if ($url) { 62 | $button->url($url); 63 | } 64 | 65 | if ($icon) { 66 | $button->icon($icon); 67 | } 68 | 69 | return $button; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Components/Button/TextButton.php: -------------------------------------------------------------------------------- 1 | payload['text'] = $text; 16 | 17 | return $this; 18 | } 19 | 20 | /** 21 | * Create a new text button instance. 22 | * 23 | * @param string|null $url 24 | * @param string|null $displayText 25 | * @return self 26 | */ 27 | public static function create(string $url = null, string $displayText = null): TextButton 28 | { 29 | $button = new static; 30 | 31 | if ($url) { 32 | $button->url($url); 33 | } 34 | 35 | if ($displayText) { 36 | $button->text($displayText); 37 | } 38 | 39 | return $button; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Concerns/ValidatesCardComponents.php: -------------------------------------------------------------------------------- 1 | hasResponse()) { 95 | return new static('Google Chat responded with an error but no response body was available'); 96 | } 97 | 98 | $statusCode = $exception->getResponse()->getStatusCode(); 99 | $description = $exception->getMessage(); 100 | 101 | return new static( 102 | "Failed to send Google Chat message, encountered client error: `{$statusCode} - {$description}`" 103 | ); 104 | } 105 | 106 | /** 107 | * Thrown if an unexpected exception was encountered whilst attempting to deliver the 108 | * notification. 109 | * 110 | * @param \Exception $exception 111 | * @return static 112 | */ 113 | public static function unexpectedException(Exception $exception) 114 | { 115 | return new static( 116 | 'Failed to send Google Chat message, unexpected exception encountered: `'.$exception->getMessage().'`', 117 | 0, 118 | $exception 119 | ); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/GoogleChatChannel.php: -------------------------------------------------------------------------------- 1 | client = $client; 28 | } 29 | 30 | /** 31 | * Send the given notification. 32 | * 33 | * @param mixed $notifiable 34 | * @param \Illuminate\Notifications\Notification $notification 35 | * 36 | * @throws \NotificationChannels\GoogleChat\Exceptions\CouldNotSendNotification 37 | */ 38 | public function send($notifiable, Notification $notification) 39 | { 40 | if (! method_exists($notification, 'toGoogleChat')) { 41 | throw CouldNotSendNotification::undefinedMethod($notification); 42 | } 43 | 44 | /** @var \NotificationChannels\GoogleChat\GoogleChatMessage $message */ 45 | if (! ($message = $notification->toGoogleChat($notifiable)) instanceof GoogleChatMessage) { 46 | throw CouldNotSendNotification::invalidMessage($message); 47 | } 48 | 49 | $space = $message->getSpace() 50 | ?? $notifiable->routeNotificationFor('googleChat') 51 | ?? config('google-chat.space'); 52 | 53 | if (! $endpoint = config("google-chat.spaces.$space", $space)) { 54 | throw CouldNotSendNotification::webhookUnavailable(); 55 | } 56 | 57 | try { 58 | $this->client->request( 59 | 'post', 60 | $endpoint, 61 | [ 62 | 'json' => $message->toArray(), 63 | ] 64 | ); 65 | } catch (ClientException $exception) { 66 | throw CouldNotSendNotification::clientError($exception); 67 | } catch (Exception $exception) { 68 | throw CouldNotSendNotification::unexpectedException($exception); 69 | } 70 | 71 | return $this; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/GoogleChatMessage.php: -------------------------------------------------------------------------------- 1 | endpoint = $space; 36 | 37 | return $this; 38 | } 39 | 40 | /** 41 | * Append text content as a simple text message. 42 | * 43 | * @param string $message 44 | * @return self 45 | */ 46 | public function text(string $message): GoogleChatMessage 47 | { 48 | $this->payload['text'] = ($this->payload['text'] ?? '').$message; 49 | 50 | return $this; 51 | } 52 | 53 | /** 54 | * Append simple text content on a new line. 55 | * 56 | * @param string $message 57 | * @return self 58 | */ 59 | public function line(string $message): GoogleChatMessage 60 | { 61 | $this->text("\n".$message); 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * Append bold text. 68 | * 69 | * @param string $message 70 | * @return self 71 | */ 72 | public function bold(string $message): GoogleChatMessage 73 | { 74 | $this->text("*{$message}*"); 75 | 76 | return $this; 77 | } 78 | 79 | /** 80 | * Append italic text. 81 | * 82 | * @param string $message 83 | * @return self 84 | */ 85 | public function italic(string $message): GoogleChatMessage 86 | { 87 | $this->text("_{$message}_"); 88 | 89 | return $this; 90 | } 91 | 92 | /** 93 | * Append strikethrough text. 94 | * 95 | * @param string $message 96 | * @return self 97 | */ 98 | public function strikethrough(string $message): GoogleChatMessage 99 | { 100 | $this->text("~{$message}~"); 101 | 102 | return $this; 103 | } 104 | 105 | /** 106 | * Append strikethrough text. 107 | * 108 | * @param string $message 109 | * @return self 110 | */ 111 | public function strike(string $message): GoogleChatMessage 112 | { 113 | return $this->strikethrough($message); 114 | } 115 | 116 | /** 117 | * Append monospace text. 118 | * 119 | * @param string $message 120 | * @return self 121 | */ 122 | public function monospace(string $message): GoogleChatMessage 123 | { 124 | $this->text("`{$message}`"); 125 | 126 | return $this; 127 | } 128 | 129 | /** 130 | * Append monospace text. 131 | * 132 | * @param string $message 133 | * @return self 134 | */ 135 | public function mono(string $message): GoogleChatMessage 136 | { 137 | return $this->monospace($message); 138 | } 139 | 140 | /** 141 | * Append monospace block text. 142 | * 143 | * @param string $message 144 | * @return self 145 | */ 146 | public function monospaceBlock(string $message): GoogleChatMessage 147 | { 148 | $this->text("```{$message}```"); 149 | 150 | return $this; 151 | } 152 | 153 | /** 154 | * Append a text link. 155 | * 156 | * @param string $link 157 | * @param string|null $displayText 158 | * @return self 159 | */ 160 | public function link(string $link, string $displayText = null): GoogleChatMessage 161 | { 162 | if ($displayText) { 163 | $link = "<{$link}|{$displayText}>"; 164 | } 165 | 166 | $this->text($link); 167 | 168 | return $this; 169 | } 170 | 171 | /** 172 | * Append mention text. 173 | * 174 | * @param string $userId 175 | * @return self 176 | */ 177 | public function mention(string $userId): GoogleChatMessage 178 | { 179 | $this->text(""); 180 | 181 | return $this; 182 | } 183 | 184 | /** 185 | * Append mention-all text. 186 | * 187 | * @param string|null $prependText 188 | * @param string|null $appendText 189 | * @return self 190 | */ 191 | public function mentionAll(string $prependText = null, string $appendText = null): GoogleChatMessage 192 | { 193 | $this->text("{$prependText}{$appendText}"); 194 | 195 | return $this; 196 | } 197 | 198 | /** 199 | * Add a one or more cards to the message. 200 | * 201 | * @param \NotificationChannels\GoogleChat\Card|\NotificationChannels\GoogleChat\Card[] $card 202 | * @return self 203 | */ 204 | public function card($card): GoogleChatMessage 205 | { 206 | $cards = Arr::wrap($card); 207 | 208 | $this->guardOnlyInstancesOf(Card::class, $cards); 209 | 210 | $this->payload['cards'] = array_merge($this->payload['cards'] ?? [], $cards); 211 | 212 | return $this; 213 | } 214 | 215 | /** 216 | * Return the configured webhook URL of the recipient space, or null if this has 217 | * not been configured. 218 | * 219 | * @return string|null 220 | */ 221 | public function getSpace(): ?string 222 | { 223 | return $this->endpoint; 224 | } 225 | 226 | /** 227 | * Serialize the message to an array representation. 228 | * 229 | * @return array 230 | */ 231 | public function toArray() 232 | { 233 | return $this->castNestedArrayables($this->payload); 234 | } 235 | 236 | /** 237 | * Recursively attempt to cast arrayable values within an array to their 238 | * primitive representation. 239 | * 240 | * @param mixed $value 241 | * @return mixed 242 | */ 243 | private function castNestedArrayables($value) 244 | { 245 | if ($value instanceof Arrayable) { 246 | $value = $value->toArray(); 247 | } 248 | 249 | if (is_array($value)) { 250 | foreach ($value as $key => $val) { 251 | $value[$key] = $this->castNestedArrayables($val); 252 | } 253 | } 254 | 255 | return $value; 256 | } 257 | 258 | /** 259 | * Return a new Google Chat Message instance. Optionally, configure it as a simple 260 | * text message using the provided message string. 261 | * 262 | * @param string|null $text 263 | * @return self 264 | */ 265 | public static function create(string $text = null): GoogleChatMessage 266 | { 267 | $message = new static; 268 | 269 | if ($text) { 270 | $message->text($text); 271 | } 272 | 273 | return $message; 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /src/GoogleChatServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->when(GoogleChatChannel::class) 16 | ->needs(GuzzleClient::class) 17 | ->give(function () { 18 | return new GuzzleClient(); 19 | }); 20 | 21 | $this->publishes([ 22 | realpath(__DIR__.'/../config/google-chat.php') => config_path('google-chat.php'), 23 | ], 'google-chat-config'); 24 | } 25 | 26 | /** 27 | * Register the application services. 28 | */ 29 | public function register() 30 | { 31 | $this->mergeConfigFrom(realpath(__DIR__.'/../config/google-chat.php'), 'google-chat'); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Section.php: -------------------------------------------------------------------------------- 1 | [], 21 | ]; 22 | 23 | /** 24 | * Set the section header text. 25 | * 26 | * @param string $text 27 | * @return self 28 | */ 29 | public function header(string $text): Section 30 | { 31 | $this->payload['header'] = $text; 32 | 33 | return $this; 34 | } 35 | 36 | /** 37 | * Add one or more widgets to this section. 38 | * 39 | * @param \NotificationChannels\GoogleChat\Widgets\AbstractWidget|\NotificationChannels\GoogleChat\Widgets\AbstractWidget[] $widget 40 | * @return self 41 | */ 42 | public function widget($widget): Section 43 | { 44 | $widgets = Arr::wrap($widget); 45 | 46 | $this->guardOnlyInstancesOf(AbstractWidget::class, $widgets); 47 | 48 | $this->payload['widgets'] = array_merge($this->payload['widgets'], $widgets); 49 | 50 | return $this; 51 | } 52 | 53 | /** 54 | * Serialize the section to an array representation. 55 | * 56 | * @return array 57 | */ 58 | public function toArray() 59 | { 60 | return $this->payload; 61 | } 62 | 63 | /** 64 | * Return a new Google Chat Section instance. 65 | * 66 | * @param \NotificationChannels\GoogleChat\Widgets\AbstractWidget|\NotificationChannels\GoogleChat\Widgets\AbstractWidget[] $widgets 67 | * @return self 68 | */ 69 | public static function create($widgets = null): Section 70 | { 71 | $section = new static; 72 | 73 | if ($widgets) { 74 | $section->widget($widgets); 75 | } 76 | 77 | return $section; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Widgets/AbstractWidget.php: -------------------------------------------------------------------------------- 1 | explode('\\') 27 | ->last() 28 | ) 29 | ->camel(); 30 | 31 | return [ 32 | (string) $widgetName => $this->payload, 33 | ]; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Widgets/Buttons.php: -------------------------------------------------------------------------------- 1 | guardOnlyInstancesOf(AbstractButton::class, $buttons); 24 | 25 | $this->payload = array_merge($this->payload, $buttons); 26 | 27 | return $this; 28 | } 29 | 30 | /** 31 | * Return a new Buttons widget instance. 32 | * 33 | * @param \NotificationChannels\GoogleChat\Components\Button\AbstractButton|\NotificationChannels\GoogleChat\Components\Button\AbstractButton[]|null $buttons 34 | * @return self 35 | */ 36 | public static function create($buttons = null): Buttons 37 | { 38 | $widget = new static; 39 | 40 | if ($buttons) { 41 | $widget->button($buttons); 42 | } 43 | 44 | return $widget; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/Widgets/Image.php: -------------------------------------------------------------------------------- 1 | payload['imageUrl'] = $url; 16 | 17 | return $this; 18 | } 19 | 20 | /** 21 | * Make the widget clickable through to the provided link. 22 | * 23 | * @param string $url 24 | * @return self 25 | */ 26 | public function onClick(string $url): Image 27 | { 28 | $this->payload['onClick'] = [ 29 | 'openLink' => [ 30 | 'url' => $url, 31 | ], 32 | ]; 33 | 34 | return $this; 35 | } 36 | 37 | /** 38 | * Return a new Image widget instance. 39 | * 40 | * @param string|null $imageUrl 41 | * @param string|null $onClickUrl 42 | * @return self 43 | */ 44 | public static function create(string $imageUrl = null, string $onClickUrl = null): Image 45 | { 46 | $widget = new static; 47 | 48 | if ($imageUrl) { 49 | $widget->imageUrl($imageUrl); 50 | } 51 | 52 | if ($onClickUrl) { 53 | $widget->onClick($onClickUrl); 54 | } 55 | 56 | return $widget; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Widgets/KeyValue.php: -------------------------------------------------------------------------------- 1 | payload['topLabel'] = $message; 18 | 19 | return $this; 20 | } 21 | 22 | /** 23 | * Set the content text. 24 | * 25 | * @param string $message 26 | * @return self 27 | */ 28 | public function content(string $message): KeyValue 29 | { 30 | $this->payload['content'] = $message; 31 | 32 | return $this; 33 | } 34 | 35 | /** 36 | * Set the bottom label text. 37 | * 38 | * @param string $message 39 | * @return self 40 | */ 41 | public function bottomLabel(string $message): KeyValue 42 | { 43 | $this->payload['bottomLabel'] = $message; 44 | 45 | return $this; 46 | } 47 | 48 | /** 49 | * Set the content multiline property. 50 | * 51 | * @param bool $value 52 | * @return self 53 | */ 54 | public function setContentMultiline(bool $value): KeyValue 55 | { 56 | $this->payload['contentMultiline'] = $value; 57 | 58 | return $this; 59 | } 60 | 61 | /** 62 | * Make the widget clickable through to the provided link. 63 | * 64 | * @param string $url 65 | * @return self 66 | */ 67 | public function onClick(string $url): KeyValue 68 | { 69 | $this->payload['onClick'] = [ 70 | 'openLink' => [ 71 | 'url' => $url, 72 | ], 73 | ]; 74 | 75 | return $this; 76 | } 77 | 78 | /** 79 | * Set the icon of this widget. 80 | * 81 | * @param string $icon 82 | * @return self 83 | */ 84 | public function icon(string $icon): KeyValue 85 | { 86 | $this->payload['icon'] = $icon; 87 | 88 | return $this; 89 | } 90 | 91 | /** 92 | * Set the button of the widget. 93 | * 94 | * @param \NotificationChannels\GoogleChat\Components\Button\AbstractButton $button 95 | * @return self 96 | */ 97 | public function button(AbstractButton $button): KeyValue 98 | { 99 | $this->payload['button'] = $button; 100 | 101 | return $this; 102 | } 103 | 104 | /** 105 | * Return a new Key Value widget instance. 106 | * 107 | * @param string|null $topLabel 108 | * @param string|null $content 109 | * @param string|null $bottomLabel 110 | * @return self 111 | */ 112 | public static function create(string $topLabel = null, string $content = null, string $bottomLabel = null): KeyValue 113 | { 114 | $widget = new static; 115 | 116 | if ($topLabel) { 117 | $widget->topLabel($topLabel); 118 | } 119 | 120 | if ($content) { 121 | $widget->content($content); 122 | } 123 | 124 | if ($bottomLabel) { 125 | $widget->bottomLabel($bottomLabel); 126 | } 127 | 128 | return $widget; 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /src/Widgets/TextParagraph.php: -------------------------------------------------------------------------------- 1 | payload['text'] = ($this->payload['text'] ?? '').$message; 16 | 17 | return $this; 18 | } 19 | 20 | /** 21 | * Append bold text context. 22 | * 23 | * @param string $message 24 | * @return self 25 | */ 26 | public function bold(string $message): TextParagraph 27 | { 28 | return $this->text("{$message}"); 29 | } 30 | 31 | /** 32 | * Append italic text context. 33 | * 34 | * @param string $message 35 | * @return self 36 | */ 37 | public function italic(string $message): TextParagraph 38 | { 39 | return $this->text("{$message}"); 40 | } 41 | 42 | /** 43 | * Append underline text context. 44 | * 45 | * @param string $message 46 | * @return self 47 | */ 48 | public function underline(string $message): TextParagraph 49 | { 50 | return $this->text("{$message}"); 51 | } 52 | 53 | /** 54 | * Append strikethrough text context. 55 | * 56 | * @param string $message 57 | * @return self 58 | */ 59 | public function strikethrough(string $message): TextParagraph 60 | { 61 | return $this->text("{$message}"); 62 | } 63 | 64 | /** 65 | * Append strikethrough text context. 66 | * 67 | * @param string $message 68 | * @return self 69 | */ 70 | public function strike(string $message): TextParagraph 71 | { 72 | return $this->strikethrough($message); 73 | } 74 | 75 | /** 76 | * Append colored text context. 77 | * 78 | * @param string $message 79 | * @param string $hex 80 | * @return self 81 | */ 82 | public function color(string $message, string $hex): TextParagraph 83 | { 84 | return $this->text("{$message}"); 85 | } 86 | 87 | /** 88 | * Append a text link. 89 | * 90 | * @param string $link 91 | * @param string|null $displayText 92 | * @return self 93 | */ 94 | public function link(string $link, string $displayText = null): TextParagraph 95 | { 96 | return $this->text("".($displayText ?? $link).''); 97 | } 98 | 99 | /** 100 | * Append a line break. 101 | * 102 | * @return self 103 | */ 104 | public function break(): TextParagraph 105 | { 106 | return $this->text('
'); 107 | } 108 | 109 | /** 110 | * Return a new Text Paragraph widget instance. 111 | * 112 | * @param string|null $message 113 | * @return self 114 | */ 115 | public static function create(string $message = null): TextParagraph 116 | { 117 | $widget = new static; 118 | 119 | if ($message) { 120 | $widget->text($message); 121 | } 122 | 123 | return $widget; 124 | } 125 | } 126 | --------------------------------------------------------------------------------