Skip to content

Commit 5ff55ef

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

14 files changed

+435
-0
lines changed

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

+2
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

+5
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')
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
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
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Edouard Lescot <edouard.lescot@gmail.com>
26+
* @author Oskar Stark <oskarstark@googlemail.com>
27+
*/
28+
final class MicrosoftTeamsTransport extends AbstractTransport
29+
{
30+
protected const ENDPOINT = 'outlook.office.com';
31+
32+
private $path;
33+
34+
public function __construct(string $path, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->path = $path;
37+
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('microsoftteams://%s%s', $this->getEndpoint(), $this->path);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof ChatMessage;
49+
}
50+
51+
/**
52+
* @see https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using#post-a-message-to-the-webhook-using-curl
53+
*/
54+
protected function doSend(MessageInterface $message): SentMessage
55+
{
56+
if (!$message instanceof ChatMessage) {
57+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
58+
}
59+
60+
$options = [
61+
'title' => $message->getSubject(),
62+
];
63+
64+
$path = $message->getRecipientId() ?? $this->path;
65+
66+
$endpoint = sprintf('https://%s%s', $this->getEndpoint(), $path);
67+
68+
$response = $this->client->request('POST', $endpoint, [
69+
'json' => array_filter($options),
70+
]);
71+
72+
$requestId = $response->getHeaders(false)['request-id'][0] ?? null;
73+
74+
if (null === $requestId) {
75+
$originalContent = $message->getSubject();
76+
77+
throw new TransportException(sprintf('Unable to post the Microsoft Teams message: "%s" (request-id not found).', $originalContent), $response);
78+
}
79+
80+
if (200 !== $response->getStatusCode()) {
81+
$errorMessage = $response->getContent(false);
82+
$originalContent = $message->getSubject();
83+
84+
throw new TransportException(sprintf('Unable to post the Microsoft Teams message: "%s" (%s : "%s").', $originalContent, $requestId, $errorMessage), $response);
85+
}
86+
87+
$message = new SentMessage($message, (string) $this);
88+
$message->setMessageId($requestId);
89+
90+
return $message;
91+
}
92+
}
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+
}
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)
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+
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\HttpClient\Response\MockResponse;
16+
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsOptions;
17+
use Symfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransport;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Message\ChatMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Component\Notifier\Message\SmsMessage;
22+
use Symfony\Component\Notifier\Notification\Notification;
23+
use Symfony\Component\Notifier\Test\TransportTestCase;
24+
use Symfony\Component\Notifier\Transport\TransportInterface;
25+
use Symfony\Contracts\HttpClient\HttpClientInterface;
26+
use Symfony\Contracts\HttpClient\ResponseInterface;
27+
28+
final class MicrosoftTeamsTransportTest extends TransportTestCase
29+
{
30+
/**
31+
* @return MicrosoftTeamsTransport
32+
*/
33+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
34+
{
35+
return (new MicrosoftTeamsTransport('/testPath', $client ?: $this->createMock(HttpClientInterface::class)))->setHost('host.test');
36+
}
37+
38+
public function toStringProvider(): iterable
39+
{
40+
yield ['microsoftteams://host.test/testPath', $this->createTransport()];
41+
}
42+
43+
44+
public function supportedMessagesProvider(): iterable
45+
{
46+
yield [new ChatMessage('Hello!')];
47+
}
48+
49+
public function unsupportedMessagesProvider(): iterable
50+
{
51+
yield [new SmsMessage('0611223344', 'Hello!')];
52+
yield [$this->createMock(MessageInterface::class)];
53+
}
54+
55+
public function testSendWithErrorResponseThrows()
56+
{
57+
$client = new MockHttpClient(function (string $method, string $url, array $options = []): ResponseInterface {
58+
return new MockResponse('testErrorMessage', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 400]);
59+
});
60+
61+
$transport = $this->createTransport($client);
62+
63+
$this->expectException(TransportException::class);
64+
$this->expectExceptionMessageMatches('/testErrorMessage/');
65+
66+
$transport->send(new ChatMessage('testMessage'));
67+
}
68+
69+
public function testSendWithErrorRequestIdThrows()
70+
{
71+
$client = new MockHttpClient(new MockResponse());
72+
73+
$transport = $this->createTransport($client);
74+
75+
$this->expectException(TransportException::class);
76+
$this->expectExceptionMessageMatches('/request-id not found/');
77+
78+
$transport->send(new ChatMessage('testMessage'));
79+
}
80+
81+
public function testSendWithOptions()
82+
{
83+
$message = 'testMessage';
84+
85+
$expectedBody = json_encode(['title' => $message]);
86+
87+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($expectedBody): ResponseInterface {
88+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
89+
90+
return new MockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 200]);
91+
});
92+
93+
$transport = $this->createTransport($client);
94+
95+
$transport->send(new ChatMessage($message));
96+
}
97+
98+
public function testSendWithNotification()
99+
{
100+
$notification = new Notification('testMessage');
101+
$chatMessage = ChatMessage::fromNotification($notification);
102+
$options = MicrosoftTeamsOptions::fromNotification($notification);
103+
104+
$expectedBody = json_encode([
105+
'title' => $options->toArray()['title'],
106+
]);
107+
108+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($expectedBody): ResponseInterface {
109+
$this->assertJsonStringEqualsJsonString($expectedBody, $options['body']);
110+
111+
return new MockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']], 'http_code' => 200]);
112+
});
113+
114+
$transport = $this->createTransport($client);
115+
116+
$transport->send($chatMessage);
117+
}
118+
}

0 commit comments

Comments
 (0)