Skip to content

[Notifier] Allow to update Slack messages #47349

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

Merged
merged 1 commit into from
Dec 16, 2022
Merged
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Slack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Allow to update Slack messages

6.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
final class SlackOptions implements MessageOptionsInterface
class SlackOptions implements MessageOptionsInterface
{
private const MAX_BLOCKS = 50;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Slack;

use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;

/**
* @author Maxim Dovydenok <dovydenok.maxim@gmail.com>
*/
final class SlackSentMessage extends SentMessage
{
private string $channelId;

public function __construct(MessageInterface $original, string $transport, string $channelId, string $messageId)
{
parent::__construct($original, $transport);
$this->channelId = $channelId;
$this->setMessageId($messageId);
}

public function getChannelId(): string
{
return $this->channelId;
}

public function getUpdateMessage(string $subject, array $options = []): ChatMessage
{
return new ChatMessage($subject, new UpdateMessageSlackOptions($this->channelId, $this->getMessageId(), $options));
}
}
12 changes: 5 additions & 7 deletions src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
Expand Down Expand Up @@ -63,7 +62,7 @@ public function supports(MessageInterface $message): bool
/**
* @see https://api.slack.com/methods/chat.postMessage
*/
protected function doSend(MessageInterface $message): SentMessage
protected function doSend(MessageInterface $message): SlackSentMessage
{
if (!$message instanceof ChatMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
Expand All @@ -82,7 +81,9 @@ protected function doSend(MessageInterface $message): SentMessage
$options['channel'] = $message->getRecipientId() ?: $this->chatChannel;
}
$options['text'] = $message->getSubject();
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/chat.postMessage', [

$apiMethod = $opts instanceof UpdateMessageSlackOptions ? 'chat.update' : 'chat.postMessage';
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/'.$apiMethod, [
'json' => array_filter($options),
'auth_bearer' => $this->accessToken,
'headers' => [
Expand All @@ -107,9 +108,6 @@ protected function doSend(MessageInterface $message): SentMessage
throw new TransportException(sprintf('Unable to post the Slack message: "%s"%s.', $result['error'], $errors), $response);
}

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($result['ts']);

return $sentMessage;
return new SlackSentMessage($message, (string) $this, $result['channel'], $result['ts']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\Notifier\Bridge\Slack\SlackOptions;
use Symfony\Component\Notifier\Bridge\Slack\SlackSentMessage;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException;
Expand Down Expand Up @@ -115,7 +116,7 @@ public function testSendWithOptions()

$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247']));
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247', 'channel' => 'C123456']));

$expectedBody = json_encode(['channel' => $channel, 'text' => $message]);

Expand All @@ -130,6 +131,8 @@ public function testSendWithOptions()
$sentMessage = $transport->send(new ChatMessage('testMessage'));

$this->assertSame('1503435956.000247', $sentMessage->getMessageId());
$this->assertInstanceOf(SlackSentMessage::class, $sentMessage);
$this->assertSame('C123456', $sentMessage->getChannelId());
}

public function testSendWithNotification()
Expand All @@ -145,7 +148,7 @@ public function testSendWithNotification()

$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247']));
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247', 'channel' => 'C123456']));

$notification = new Notification($message);
$chatMessage = ChatMessage::fromNotification($notification);
Expand Down Expand Up @@ -223,7 +226,7 @@ public function testSendIncludesContentTypeWithCharset()

$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247']));
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247', 'channel' => 'C123456']));

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response): ResponseInterface {
$this->assertContains('Content-Type: application/json; charset=utf-8', $options['headers']);
Expand Down Expand Up @@ -263,4 +266,39 @@ public function testSendWithErrorsIncluded()

$transport->send(new ChatMessage('testMessage'));
}

public function testUpdateMessage()
{
$response = $this->createMock(ResponseInterface::class);

$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(200);

$response->expects($this->once())
->method('getContent')
->willReturn(json_encode(['ok' => true, 'ts' => '1503435956.000247', 'channel' => 'C123456']));

$sentMessage = new SlackSentMessage(new ChatMessage('Hello'), 'slack', 'C123456', '1503435956.000247');
$chatMessage = $sentMessage->getUpdateMessage('Hello World');

$expectedBody = json_encode([
'channel' => 'C123456',
'ts' => '1503435956.000247',
'text' => 'Hello World',
]);

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
$this->assertStringEndsWith('chat.update', $url);

return $response;
});

$transport = $this->createTransport($client, 'another-channel');

$sentMessage = $transport->send($chatMessage);

$this->assertSame('1503435956.000247', $sentMessage->getMessageId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Slack;

/**
* @author Maxim Dovydenok <dovydenok.maxim@gmail.com>
*/
final class UpdateMessageSlackOptions extends SlackOptions
{
public function __construct(string $channelId, string $messageId, array $options = [])
{
$options['channel'] = $channelId;
$options['ts'] = $messageId;

parent::__construct($options);
}
}