From b27c94416f5a6910b734854fe1c3d05cf4e4b2f0 Mon Sep 17 00:00:00 2001 From: Ed Jeavons Date: Sat, 25 Feb 2023 10:41:10 +0000 Subject: [PATCH 1/5] Add support for message threads --- src/GoogleChatChannel.php | 4 ++++ src/GoogleChatMessage.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/GoogleChatChannel.php b/src/GoogleChatChannel.php index 74bab17..eb5a6d3 100644 --- a/src/GoogleChatChannel.php +++ b/src/GoogleChatChannel.php @@ -54,6 +54,10 @@ public function send($notifiable, Notification $notification) throw CouldNotSendNotification::webhookUnavailable(); } + if ($message->isThreaded()) { + $endpoint .= '&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD'; + } + try { $this->client->request( 'post', diff --git a/src/GoogleChatMessage.php b/src/GoogleChatMessage.php index edeec17..874e72f 100644 --- a/src/GoogleChatMessage.php +++ b/src/GoogleChatMessage.php @@ -212,6 +212,32 @@ public function card($card): GoogleChatMessage return $this; } + /** + * Start or reply to a message thread. + * + * @param string $thread A thread reference that can be reused later to add replies + * @param bool $isName Specify the thread using a name rather than a threadKey (not typically wanted) + * @return self + */ + public function thread(string $thread, bool $isName = false): GoogleChatMessage + { + $this->payload['thread'] = [ + $isName ? 'name' : 'threadKey' => $thread, + ]; + + return $this; + } + + /** + * The message is creating or replying to a message thread. + * + * @return bool + */ + public function isThreaded(): bool + { + return isset($this->payload['thread']); + } + /** * Return the configured webhook URL of the recipient space, or null if this has * not been configured. From 9ce55432f4de80aa391b0ac92cc252f68306ea4a Mon Sep 17 00:00:00 2001 From: Ed Jeavons Date: Sun, 5 Mar 2023 14:29:50 +0000 Subject: [PATCH 2/5] Document message threading in README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 96d4040..1ef1b64 100644 --- a/README.md +++ b/README.md @@ -306,6 +306,7 @@ The `GoogleChatMessage` class encompasses an entire message that will be sent to - `static create(?string $text)` Instantiates and returns a new `GoogleChatMessage` instance, optionally pre-configuring it with the provided simple text - `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 +- `thread(string $thread)` Start or reply to a message thread (in supported spaces) - `text(string $message)` Appends `$message` to the simple message content - `line(string $message)` Appends `$message` on a new line - `bold(string $message)` Appends bold text @@ -429,6 +430,12 @@ The `ImageButton` defines a clickable icon or image, and can be accepted anywher - `url(string $url)` Defines the target endpoint for the button - `icon(string $icon)` Defines the icon or image to display for the button. +### Message Threads + +If you are posting into a space that supports [conversation by topic](https://support.google.com/mail/answer/12176488) you may set a known thread key in order to reply to a specific message thread. Using a new/unrecognized key will automatically begin a new message thread within the space which may be replied to later by using that same key again. + +By default the reference will be passed as a threadKey which is the most common requirement. Adding `true` as a second parameter in `thread('my_thread_ref', true)` will pass it as a thread name instead. Refer to the [Chat API](https://developers.google.com/chat/api/guides/crudl/messages#start_or_reply_to_a_message_thread) for more details. + ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. From fc31414e2622ffd7fd3bd214a129f95000710e85 Mon Sep 17 00:00:00 2001 From: Ed Jeavons Date: Sun, 5 Mar 2023 15:41:20 +0000 Subject: [PATCH 3/5] Add tests for threaded messages --- tests/GoogleChatMessageTest.php | 40 +++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/GoogleChatMessageTest.php b/tests/GoogleChatMessageTest.php index 630e4e9..423f06a 100644 --- a/tests/GoogleChatMessageTest.php +++ b/tests/GoogleChatMessageTest.php @@ -205,4 +205,44 @@ public function test_it_can_add_card() $message->toArray() ); } + + public function test_it_creates_threaded_messages_by_key() + { + $message = GoogleChatMessage::create()->thread('test-thread-key'); + + $this->assertEquals( + [ + 'thread' => [ + 'threadKey' => 'test-thread-key', + ], + ], + $message->toArray() + ); + } + + public function test_it_creates_threaded_messages_by_name() + { + $message = GoogleChatMessage::create()->thread('test-thread-name', true); + + $this->assertEquals( + [ + 'thread' => [ + 'name' => 'test-thread-name', + ], + ], + $message->toArray() + ); + } + + public function test_it_recognises_non_threaded_messages() + { + $message = GoogleChatMessage::create('Example Non-Threaded Message'); + $this->assertFalse($message->isThreaded()); + } + + public function test_it_recognises_threaded_messages() + { + $message = GoogleChatMessage::create('Example Threaded Message')->thread('test-thread-key'); + $this->assertTrue($message->isThreaded()); + } } From decc978c8f1ce1c6c619a50942581150d35cf960 Mon Sep 17 00:00:00 2001 From: Laravel Shift Date: Tue, 19 Mar 2024 02:42:00 -0400 Subject: [PATCH 4/5] Laravel 11.x Compatibility (#25) --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 29283b9..47f63a1 100644 --- a/composer.json +++ b/composer.json @@ -14,12 +14,12 @@ "require": { "php": ">=8.0", "guzzlehttp/guzzle": "^6.3 || ^7.0", - "illuminate/notifications": "^9.0.2 || ^10.0", - "illuminate/support": "^9.0.2 || ^10.0" + "illuminate/notifications": "^9.0.2 || ^10.0 || ^11.0", + "illuminate/support": "^9.0.2 || ^10.0 || ^11.0" }, "require-dev": { - "orchestra/testbench": "^7.0", - "phpunit/phpunit": "^9.5.10" + "orchestra/testbench": "^7.0 || ^9.0", + "phpunit/phpunit": "^9.5.10 || ^10.5" }, "autoload": { "psr-4": { From 87c3a27c80071c4935728291d89ee75f8aa05dd3 Mon Sep 17 00:00:00 2001 From: Ed Jeavons Date: Sun, 9 Mar 2025 09:24:02 +0000 Subject: [PATCH 5/5] Laravel 12.x Compatibility --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 47f63a1..259aecf 100644 --- a/composer.json +++ b/composer.json @@ -14,12 +14,12 @@ "require": { "php": ">=8.0", "guzzlehttp/guzzle": "^6.3 || ^7.0", - "illuminate/notifications": "^9.0.2 || ^10.0 || ^11.0", - "illuminate/support": "^9.0.2 || ^10.0 || ^11.0" + "illuminate/notifications": "^9.0.2 || ^10.0 || ^11.0 || ^12.0", + "illuminate/support": "^9.0.2 || ^10.0 || ^11.0 || ^12.0" }, "require-dev": { - "orchestra/testbench": "^7.0 || ^9.0", - "phpunit/phpunit": "^9.5.10 || ^10.5" + "orchestra/testbench": "^7.0 || ^9.0 || ^10.0", + "phpunit/phpunit": "^9.5.10 || ^10.5 || ^11.5.3" }, "autoload": { "psr-4": {