Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/message replies #20

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 4 additions & 0 deletions src/GoogleChatChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
26 changes: 26 additions & 0 deletions src/GoogleChatMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions tests/GoogleChatMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}