Skip to content

Commit 152e869

Browse files
committed
Remove SlackWebhook transport and replace default one
1 parent 40cea0a commit 152e869

File tree

3 files changed

+39
-136
lines changed

3 files changed

+39
-136
lines changed

src/Symfony/Component/Notifier/Bridge/Slack/SlackTransport.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,41 @@
2121

2222
/**
2323
* @author Fabien Potencier <fabien@symfony.com>
24+
* @author Daniel Stancu <birkof@birkof.ro>
2425
*
2526
* @internal
2627
*
27-
* @experimental in 5.0
28+
* @experimental in 5.1
2829
*/
2930
final class SlackTransport extends AbstractTransport
3031
{
31-
protected const HOST = 'slack.com';
32+
protected const HOST = 'hooks.slack.com';
3233

33-
private $accessToken;
34-
private $chatChannel;
34+
protected $client;
35+
private $path;
36+
private $channel;
37+
private $username;
3538

36-
public function __construct(string $accessToken, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
39+
public function __construct(string $path, string $channel, string $username, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
3740
{
38-
$this->accessToken = $accessToken;
39-
$this->chatChannel = $channel;
41+
$this->path = $path;
42+
$this->channel = $channel;
43+
$this->username = $username;
4044
$this->client = $client;
4145

4246
parent::__construct($client, $dispatcher);
4347
}
4448

4549
public function __toString(): string
4650
{
47-
return sprintf('%s://%s?channel=%s', SlackTransportFactory::SCHEME, $this->getEndpoint(), $this->chatChannel);
51+
return sprintf(
52+
'%s://%s/%s?channel=%s&username=%s&',
53+
SlackTransportFactory::SCHEME,
54+
$this->getEndpoint(),
55+
$this->path,
56+
$this->channel,
57+
$this->username
58+
);
4859
}
4960

5061
public function supports(MessageInterface $message): bool
@@ -53,7 +64,9 @@ public function supports(MessageInterface $message): bool
5364
}
5465

5566
/**
56-
* @see https://api.slack.com/methods/chat.postMessage
67+
* Sending messages using Incoming Webhooks.
68+
*
69+
* @see https://api.slack.com/messaging/webhooks
5770
*/
5871
protected function doSend(MessageInterface $message): void
5972
{
@@ -69,22 +82,25 @@ protected function doSend(MessageInterface $message): void
6982
}
7083

7184
$options = $opts ? $opts->toArray() : [];
72-
$options['token'] = $this->accessToken;
7385
if (!isset($options['channel'])) {
74-
$options['channel'] = $message->getRecipientId() ?: $this->chatChannel;
86+
$options['channel'] = $message->getRecipientId() ?: $this->channel;
7587
}
88+
$options['username'] = $options['username'] ?? $this->username;
7689
$options['text'] = $message->getSubject();
77-
$response = $this->client->request('POST', 'https://'.$this->getEndpoint().'/api/chat.postMessage', [
78-
'body' => array_filter($options),
79-
]);
90+
91+
$response = $this->client->request(
92+
'POST',
93+
sprintf(
94+
'%s://%s/%s',
95+
'https',
96+
$this->getEndpoint(),
97+
$this->path
98+
),
99+
['json' => array_filter($options)]
100+
);
80101

81102
if (200 !== $response->getStatusCode()) {
82103
throw new TransportException(sprintf('Unable to post the Slack message: %s.', $response->getContent(false)), $response);
83104
}
84-
85-
$result = $response->toArray(false);
86-
if (!$result['ok']) {
87-
throw new TransportException(sprintf('Unable to post the Slack message: %s.', $result['error']), $response);
88-
}
89105
}
90106
}

src/Symfony/Component/Notifier/Bridge/Slack/SlackTransportFactory.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
final class SlackTransportFactory extends AbstractTransportFactory
2525
{
2626
public const SCHEME = 'slack';
27-
public const SCHEME_WEBHOOK = 'slack+webhook';
2827

2928
/**
3029
* @return SlackTransport
@@ -33,27 +32,21 @@ public function create(Dsn $dsn): TransportInterface
3332
{
3433
$scheme = $dsn->getScheme();
3534
$channel = $dsn->getOption('channel');
35+
$username = $dsn->getOption('username');
3636
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3737
$port = $dsn->getPort();
3838

3939
if (self::SCHEME === $scheme) {
40-
$accessToken = $this->getUser($dsn);
40+
$path = ltrim($dsn->getPath(), '/');
4141

42-
return (new SlackTransport($accessToken, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
43-
}
44-
45-
if (self::SCHEME_WEBHOOK === $scheme) {
46-
$webhookPath = ltrim($dsn->getPath(), '/');
47-
$username = $dsn->getOption('username');
48-
49-
return (new SlackWebhookTransport($webhookPath, $channel, $username, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
42+
return (new SlackTransport($path, $channel, $username, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
5043
}
5144

5245
throw new UnsupportedSchemeException($dsn, 'slack', $this->getSupportedSchemes());
5346
}
5447

5548
protected function getSupportedSchemes(): array
5649
{
57-
return ['slack', 'slack+webhook'];
50+
return ['slack'];
5851
}
5952
}

src/Symfony/Component/Notifier/Bridge/Slack/SlackWebhookTransport.php

Lines changed: 0 additions & 106 deletions
This file was deleted.

0 commit comments

Comments
 (0)