10000 [Notifier] Add notifier for Microsoft Teams · symfony/symfony@4e8bf02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e8bf02

Browse files
idetoxOskarStark
authored andcommitted
[Notifier] Add notifier for Microsoft Teams
1 parent 0cc982c commit 4e8bf02

File tree

15 files changed

+407
-0
lines changed

15 files changed

+407
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
126126
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
127127
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
128+
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
128129
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
129130
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
130131
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
@@ -2390,6 +2391,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
23902391
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
23912392
GitterTransportFactory::class => 'notifier.transport_factory.gitter',
23922393
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
2394+
MicrosoftTeamsTransportFactory::class => 'notifier.transport_factory.microsoftteams',
23932395
];
23942396

23952397
$parentPackages = ['symfony/framework-bundle', 'symfony/notifier'];

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
2929
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
3030
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
31+
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
3132
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
3233
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
3334
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
@@ -150,6 +151,10 @@
150151
->parent('notifier.transport_factory.abstract')
151152
->tag('chatter.transport_factory')
152153

154+
->set('notifier.transport_factory.microsoftteams', MicrosoftTeamsTransportFactory::class)
155+
->parent('notifier.transport_factory.abstract')
156+
->tag('chatter.transport_factory')
157+
153158
->set('notifier.transport_factory.gatewayapi', GatewayApiTransportFactory::class)
154159
->parent('notifier.transport_factory.abstract')
155160
->tag('texter.transport_factory')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 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: 86 additions & 0 deletions
< 10000 tr class="diff-line-row">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\MicrosoftTeams;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
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 Edouard Lescot <edouard.lescot@gmail.com>
25+
* @author Oskar Stark <oskarstark@googlemail.com>
26+
*/
27+
final class MicrosoftTeamsTransport extends AbstractTransport
28+
{
29+
protected const ENDPOINT = 'outlook.office.com';
30+
31+
private $path;
32+
33+
public function __construct(string $path, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
34+
{
35+
$this->path = $path;
36+
37+
parent::__construct($client, $dispatcher);
38+
}
39+
40+
public function __toString(): string
41+
{
42+
return sprintf('microsoftteams://%s%s', $this->getEndpoint(), $this->path);
43+
}
44+
45+
public function supports(MessageInterface $message): bool
46+
{
47+
return $message instanceof ChatMessage;
48+
}
49+
50+
/**
51+
* @see https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using#post-a-message-to-the-webhook-using-curl
52+
*/
53+
protected function doSend(MessageInterface $message): SentMessage
54+
{
55+
if (!$message instanceof ChatMessage) {
56+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
57+
}
58+
59+
$path = $message->getRecipientId() ?? $this->path;
60+
$endpoint = sprintf('https://%s%s', $this->getEndpoint(), $path);
61+
$response = $this->client->request('POST', $endpoint, [
62+
'json' => [
63+
'title' => $message->getSubject(),
64+
],
65+
]);
66+
67+
$requestId = $response->getHeaders(false)['request-id'][0] ?? null;
68+
if (null === $requestId) {
69+
$originalContent = $message->getSubject();
70+
71+
throw new TransportException(sprintf('Unable to post the Microsoft Teams message: "%s" (request-id not found).', $originalContent), $response);
72+
}
73+
74+
if (200 !== $response->getStatusCode()) {
75+
$errorMessage = $response->getContent(false);
76+
$originalContent = $message->getSubject();
77+
78+
throw new TransportException(sprintf('Unable to post the Microsoft Teams message: "%s" (%s : "%s").', $originalContent, $requestId, $errorMessage), $response);
79+
}
80+
81+
$message = new SentMessage($message, (string) $this);
82+
$message->setMessageId($requestId);
83+
84+
return $message;
85+
}
86+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\MicrosoftTeams;
13+
14+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportInterface;
19+
20+
/**
21+
* @author Edouard Lescot <edouard.lescot@gmail.com>
22+
* @author Oskar Stark <oskarstark@googlemail.com>
23+
*/
24+
final class MicrosoftTeamsTransportFactory extends AbstractTransportFactory
25+
{
26+
public function create(Dsn $dsn): TransportInterface
27+
{
28+
$scheme = $dsn->getScheme();
29+
30+
if ('microsoftteams' !== $scheme) {
31+
throw new UnsupportedSchemeException($dsn, 'microsoftteams', $this->getSupportedSchemes());
32+
}
33+
34+
$path = $dsn->getPath();
35+
36+
if (null === $path) {
37+
throw new IncompleteDsnException('Path is not set.', $dsn->getOriginalDsn());
38+
}
39+
40+
$host = $dsn->getHost();
41+
$port = $dsn->getPort();
42+
43+
return (new MicrosoftTeamsTransport($path, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
44+
}
45+
46+
protected function getSupportedSchemes(): array
47+
{
48+
return ['microsoftteams'];
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Microsoft Teams Notifier
2+
========================
3+
4+
Provides [Microsoft Teams](https://www.microsoft.com/en/microsoft-365/microsoft-teams/free) integration
5+
through Incoming Webhook for Symfony Notifier.
6+
7+
DSN example
8+
-----------
9+
10+
```
11+
MICROSOFT_TEAMS_DSN=microsoftteams://default/PATH
12+
```
13+
14+
where:
15+
- `PATH` has the following format: `webhook/{uuid}@{uuid}/IncomingWebhook/{id}/{uuid}`
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 E3F5 +
namespace Symfony\Component\Notifier\Bridge\MicrosoftTeams\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class MicrosoftTeamsTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
public function createFactory(): TransportFactoryInterface
21+
{
22+
return new MicrosoftTeamsTransportFactory();
23+
}
24+
25+
public function createProvider(): iterable
26+
{
27+
yield [
28+
'microsoftteams://host/webhook',
29+
'microsoftteams://host/webhook',
30+
];
31+
}
32+
33+
public function supportsProvider(): iterable
34+
{
35+
yield [true, 'microsoftteams://host/webhook'];
36+
yield [false, 'somethingElse://host/webhook'];
37+
}
38+
39+
public function unsupportedSchemeProvider(): iterable
40+
{
41+
yield ['somethingElse://host/webhook'];
42+
}
43+
}

0 commit comments

Comments
 (0)
0