Skip to content

Commit 14790d8

Browse files
mpiotfabpot
authored andcommitted
Add Discord bridge notifier
1 parent 88b158d commit 14790d8

File tree

14 files changed

+391
-0
lines changed

14 files changed

+391
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
use Symfony\Component\Mime\Header\Headers;
100100
use Symfony\Component\Mime\MimeTypeGuesserInterface;
101101
use Symfony\Component\Mime\MimeTypes;
102+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
102103
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
103104
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
104105
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
@@ -2222,6 +2223,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22222223
SmsapiTransportFactory::class => 'notifier.transport_factory.smsapi',
22232224
EsendexTransportFactory::class => 'notifier.transport_factory.esendex',
22242225
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
2226+
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
22252227
];
22262228

22272229
foreach ($classToServices as $class => $service) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
1415
use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory;
1516
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
@@ -110,6 +111,10 @@
110111
->parent('notifier.transport_factory.abstract')
111112
->tag('texter.transport_factory')
112113

114+
->set('notifier.transport_factory.discord', DiscordTransportFactory::class)
115+
->parent('notifier.transport_factory.abstract')
116+
->tag('chatter.transport_factory')
117+
113118
->set('notifier.transport_factory.null', NullTransportFactory::class)
114119
->parent('notifier.transport_factory.abstract')
115120
->tag('chatter.transport_factory')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.2.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Mathieu Piot <[email protected]>
25+
*
26+
* @internal
27+
*
28+
* @experimental in 5.2
29+
*/
30+
final class DiscordTransport extends AbstractTransport
31+
{
32+
protected const HOST = 'discord.com';
33+
34+
private $token;
35+
private $chatChannel;
36+
37+
public function __construct(string $token, string $channel = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
38+
{
39+
$this->token = $token;
40+
$this->chatChannel = $channel;
41+
$this->client = $client;
42+
43+
parent::__construct($client, $dispatcher);
44+
}
45+
46+
public function __toString(): string
47+
{
48+
return sprintf('discord://%s?channel=%s', $this->getEndpoint(), $this->chatChannel);
49+
}
50+
51+
public function supports(MessageInterface $message): bool
52+
{
53+
return $message instanceof ChatMessage;
54+
}
55+
56+
/**
57+
* @see https://discord.com/developers/docs/resources/webhook
58+
*/
59+
protected function doSend(MessageInterface $message): SentMessage
60+
{
61+
if (!$message instanceof ChatMessage) {
62+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
63+
}
64+
65+
$endpoint = sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->token, $this->chatChannel);
66+
$options['content'] = $message->getSubject();
67+
$response = $this->client->request('POST', $endpoint, [
68+
'json' => array_filter($options),
69+
]);
70+
71+
if (204 !== $response->getStatusCode()) {
72+
$result = $response->toArray(false);
73+
74+
throw new TransportException(sprintf('Unable to post the Discord message: "%s" (%s).', $result['message'], $result['code']), $response);
75+
}
76+
}
77+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Mathieu Piot <[email protected]>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
final class DiscordTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @return DiscordTransport
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$token = $this->getUser($dsn);
33+
$channel = $dsn->getOption('channel');
34+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
35+
$port = $dsn->getPort();
36+
37+
if ('discord' === $scheme) {
38+
return (new DiscordTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
throw new UnsupportedSchemeException($dsn, 'discord', $this->getSupportedSchemes());
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return ['discord'];
47+
}
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019-2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Discord Notifier
2+
================
3+
4+
Provides Discord integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
// .env file
11+
DISCORD_DSN=discord://TOKEN@default?channel=ID
12+
```
13+
14+
where:
15+
- `TOKEN` the secure token of the webhook (returned for Incoming Webhooks)
16+
- `ID` the id of the webhook
17+
18+
19+
Resources
20+
---------
21+
22+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
23+
* [Report issues](https://github.com/symfony/symfony/issues) and
24+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
25+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
16+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
18+
use Symfony\Component\Notifier\Transport\Dsn;
19+
20+
final class DiscordTransportFactoryTest extends TestCase
21+
{
22+
public function testCreateWithDsn(): void
23+
{
24+
$factory = new DiscordTransportFactory();
25+
26+
$host = 'testHost';
27+
$channel = 'testChannel';
28+
29+
$transport = $factory->create(Dsn::fromString(sprintf('discord://%s@%s/?channel=%s', 'token', $host, $channel)));
30+
31+
$this->assertSame(sprintf('discord://%s?channel=%s', $host, $channel), (string) $transport);
32+
}
33+
34+
public function testCreateWithNoTokenThrowsMalformed(): void
35+
{
36+
$factory = new DiscordTransportFactory();
37+
38+
$this->expectException(IncompleteDsnException::class);
39+
$factory->create(Dsn::fromString(sprintf('discord://%s/?channel=%s', 'testHost', 'testChannel')));
40+
}
41+
42+
public function testSupportsDiscordScheme(): void
43+
{
44+
$factory = new DiscordTransportFactory();
45+
46+
$this->assertTrue($factory->supports(Dsn::fromString('discord://host/?channel=testChannel')));
47+
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host/?channel=testChannel')));
48+
}
49+
50+
public function testNonDiscordSchemeThrows(): void
51+
{
52+
$factory = new DiscordTransportFactory();
53+
54+
$this->expectException(UnsupportedSchemeException::class);
55+
$factory->create(Dsn::fromString('somethingElse://token@host/?channel=testChannel'));
56+
}
57+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\Discord\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\MockHttpClient;
16+
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport;
17+
use Symfony\Component\Notifier\Exception\LogicException;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Message\ChatMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
23+
24+
final class DiscordTransportTest extends TestCase
25+
{
26+
public function testToStringContainsProperties(): void
27+
{
28+
$channel = 'testChannel';
29+
30+
$transport = new DiscordTransport('testToken', $channel, $this->createMock(HttpClientInterface::class));
31+
$transport->setHost('testHost');
32+
33+
$this->assertSame(sprintf('discord://%s?channel=%s', 'testHost', $channel), (string) $transport);
34+
}
35+
36+
public function testSupportsChatMessage(): void
37+
{
38+
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
39+
40+
$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
41+
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
42+
}
43+
44+
public function testSendNonChatMessageThrows(): void
45+
{
46+
$this->expectException(LogicException::class);
47+
$transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class));
48+
49+
$transport->send($this->createMock(MessageInterface::class));
50+
}
51+
52+
public function testSendWithErrorResponseThrows(): void
53+
{
54+
$this->expectException(TransportException::class);
55+
$this->expectExceptionMessageMatches('/testDescription.+testErrorCode/');
56+
57+
$response = $this->createMock(ResponseInterface::class);
58+
$response->expects($this->exactly(2))
59+
->method('getStatusCode')
60+
->willReturn(400);
61+
$response->expects($this->once())
62+
->method('getContent')
63+
->willReturn(json_encode(['message' => 'testDescription', 'code' => 'testErrorCode']));
64+
65+
$client = new MockHttpClient(static function () use ($response): ResponseInterface {
66+
return $response;
67+
});
68+
69+
$transport = new DiscordTransport('testToken', 'testChannel', $client);
70+
71+
$transport->send(new ChatMessage('testMessage'));
72+
}
73+
}

0 commit comments

Comments
 (0)