From 8cd988fad1a08ed88dfd852f140477376c60217f Mon Sep 17 00:00:00 2001 From: Wesley Date: Wed, 10 Jul 2024 21:39:44 +0200 Subject: [PATCH] feat: support threaded replies for SlackMessage (#94) * feat: support threaded replies for SlackMessage * formatting --------- Co-authored-by: Taylor Otwell --- src/Slack/SlackMessage.php | 32 ++++++++++++++++++++++++ tests/Slack/Feature/SlackMessageTest.php | 26 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/Slack/SlackMessage.php b/src/Slack/SlackMessage.php index 8d6bee6..e1fda76 100644 --- a/src/Slack/SlackMessage.php +++ b/src/Slack/SlackMessage.php @@ -71,6 +71,16 @@ class SlackMessage implements Arrayable */ protected ?string $username = null; + /** + * Unique, per-channel, timestamp for each message. If provided, send message as a thread reply to this message. + */ + protected ?string $threadTs = null; + + /** + * If sending message as reply to thread, whether to 'broadcast' a reference to the thread reply to the parent conversation. + */ + protected ?bool $broadcastReply = null; + /** * Set the Slack channel the message should be sent to. */ @@ -238,6 +248,26 @@ public function username(string $username): self return $this; } + /** + * Set the thread timestamp (message ID) to send as reply to thread. + */ + public function threadTimestamp(?string $threadTimestamp): self + { + $this->threadTs = $threadTimestamp; + + return $this; + } + + /** + * Only applicable if threadTimestamp is set. Broadcasts a reference to the threaded reply to the parent conversation. + */ + public function broadcastReply(?bool $broadcastReply = true): self + { + $this->broadcastReply = $broadcastReply; + + return $this; + } + /** * Get the instance as an array. */ @@ -258,6 +288,8 @@ public function toArray(): array 'icon_url' => $this->image, 'metadata' => $this->metaData?->toArray(), 'mrkdwn' => $this->mrkdwn, + 'thread_ts' => $this->threadTs, + 'reply_broadcast' => $this->broadcastReply, 'unfurl_links' => $this->unfurlLinks, 'unfurl_media' => $this->unfurlMedia, 'username' => $this->username, diff --git a/tests/Slack/Feature/SlackMessageTest.php b/tests/Slack/Feature/SlackMessageTest.php index 5d4e5a7..6af56e2 100644 --- a/tests/Slack/Feature/SlackMessageTest.php +++ b/tests/Slack/Feature/SlackMessageTest.php @@ -187,6 +187,32 @@ public function it_can_unfurl_media(): void ]); } + /** @test */ + public function it_can_reply_as_thread(): void + { + $this->sendNotification(function (SlackMessage $message) { + $message->text('See https://api.slack.com/methods/chat.postMessage for more information.'); + $message->threadTimestamp('123456.7890'); + })->assertNotificationSent([ + 'channel' => '#ghost-talk', + 'text' => 'See https://api.slack.com/methods/chat.postMessage for more information.', + 'thread_ts' => '123456.7890', + ]); + } + + /** @test */ + public function it_can_send_threaded_reply_as_broadcast_reference(): void + { + $this->sendNotification(function (SlackMessage $message) { + $message->text('See https://api.slack.com/methods/chat.postMessage for more information.'); + $message->broadcastReply(true); + })->assertNotificationSent([ + 'channel' => '#ghost-talk', + 'text' => 'See https://api.slack.com/methods/chat.postMessage for more information.', + 'reply_broadcast' => true, + ]); + } + /** @test */ public function it_can_set_the_bot_user_name(): void {