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 */
2930final 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}
0 commit comments