Skip to content

Commit

Permalink
feat: support threaded replies for SlackMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma-ve committed Jul 9, 2024
1 parent b783be6 commit c28c000
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/Slack/SlackMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 $replyBroadcast = null;

/**
* Set the Slack channel the message should be sent to.
*/
Expand Down Expand Up @@ -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 replyBroadcast(?bool $replyBroadcast): self
{
$this->replyBroadcast = $replyBroadcast;

return $this;
}

/**
* Get the instance as an array.
*/
Expand All @@ -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->replyBroadcast,
'unfurl_links' => $this->unfurlLinks,
'unfurl_media' => $this->unfurlMedia,
'username' => $this->username,
Expand Down
26 changes: 26 additions & 0 deletions tests/Slack/Feature/SlackMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->replyBroadcast(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
{
Expand Down

0 comments on commit c28c000

Please sign in to comment.