Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5ff55ef

Browse files
idetoxOskarStark
authored andcommitted
[Notifier] Add notifier for Microsoft Teams
1 parent0cc982c commit5ff55ef

File tree

14 files changed

+435
-0
lines changed

14 files changed

+435
-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
useSymfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
126126
useSymfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
127127
useSymfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
128+
useSymfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
128129
useSymfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
129130
useSymfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
130131
useSymfony\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
useSymfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
2929
useSymfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
3030
useSymfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
31+
useSymfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
3132
useSymfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
3233
useSymfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
3334
useSymfony\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+
/Testsexport-ignore
2+
/phpunit.xml.distexport-ignore
3+
/.gitattributesexport-ignore
4+
/.gitignoreexport-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.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: 92 additions & 0 deletions
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+
namespaceSymfony\Component\Notifier\Bridge\MicrosoftTeams;
13+
14+
useSymfony\Component\Notifier\Exception\LogicException;
15+
useSymfony\Component\Notifier\Exception\TransportException;
16+
useSymfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
useSymfony\Component\Notifier\Message\ChatMessage;
18+
useSymfony\Component\Notifier\Message\MessageInterface;
19+
useSymfony\Component\Notifier\Message\SentMessage;
20+
useSymfony\Component\Notifier\Transport\AbstractTransport;
21+
useSymfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
useSymfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Edouard Lescot <edouard.lescot@gmail.com>
26+
* @author Oskar Stark <oskarstark@googlemail.com>
27+
*/
28+
finalclass MicrosoftTeamsTransportextends AbstractTransport
29+
{
30+
protectedconstENDPOINT ='outlook.office.com';
31+
32+
private$path;
33+
34+
publicfunction__construct(string$path,HttpClientInterface$client =null,EventDispatcherInterface$dispatcher =null)
35+
{
36+
$this->path =$path;
37+
38+
parent::__construct($client,$dispatcher);
39+
}
40+
41+
publicfunction__toString():string
42+
{
43+
returnsprintf('microsoftteams://%s%s',$this->getEndpoint(),$this->path);
44+
}
45+
46+
publicfunctionsupports(MessageInterface$message):bool
47+
{
48+
return$messageinstanceof 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+
protectedfunctiondoSend(MessageInterface$message):SentMessage
55+
{
56+
if (!$messageinstanceof ChatMessage) {
57+
thrownewUnsupportedMessageTypeException(__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+
thrownewTransportException(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+
thrownewTransportException(sprintf('Unable to post the Microsoft Teams message: "%s" (%s : "%s").',$originalContent,$requestId,$errorMessage),$response);
85+
}
86+
87+
$message =newSentMessage($message, (string)$this);
88+
$message->setMessageId($requestId);
89+
90+
return$message;
91+
}
92+
}
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+
namespaceSymfony\Component\Notifier\Bridge\MicrosoftTeams;
13+
14+
useSymfony\Component\Notifier\Exception\IncompleteDsnException;
15+
useSymfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
useSymfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
useSymfony\Component\Notifier\Transport\Dsn;
18+
useSymfony\Component\Notifier\Transport\TransportInterface;
19+
20+
/**
21+
* @author Edouard Lescot <edouard.lescot@gmail.com>
22+
* @author Oskar Stark <oskarstark@googlemail.com>
23+
*/
24+
finalclass MicrosoftTeamsTransportFactoryextends AbstractTransportFactory
25+
{
26+
publicfunctioncreate(Dsn$dsn):TransportInterface
27+
{
28+
$scheme =$dsn->getScheme();
29+
30+
if ('microsoftteams' !==$scheme) {
31+
thrownewUnsupportedSchemeException($dsn,'microsoftteams',$this->getSupportedSchemes());
32+
}
33+
34+
$path =$dsn->getPath();
35+
36+
if (null ===$path) {
37+
thrownewIncompleteDsnException('Path is not set.',$dsn->getOriginalDsn());
38+
}
39+
40+
$host =$dsn->getHost();
41+
$port =$dsn->getPort();
42+
43+
return (newMicrosoftTeamsTransport($path,$this->client,$this->dispatcher))->setHost($host)->setPort($port);
44+
}
45+
46+
protectedfunctiongetSupportedSchemes():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+
namespaceSymfony\Component\Notifier\Bridge\MicrosoftTeams\Tests;
13+
14+
useSymfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransportFactory;
15+
useSymfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
useSymfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
finalclass MicrosoftTeamsTransportFactoryTestextends TransportFactoryTestCase
19+
{
20+
publicfunctioncreateFactory():TransportFactoryInterface
21+
{
22+
returnnewMicrosoftTeamsTransportFactory();
23+
}
24+
25+
publicfunctioncreateProvider():iterable
26+
{
27+
yield [
28+
'microsoftteams://host/webhook',
29+
'microsoftteams://host/webhook',
30+
];
31+
}
32+
33+
publicfunctionsupportsProvider():iterable
34+
{
35+
yield [true,'microsoftteams://host/webhook'];
36+
yield [false,'somethingElse://host/webhook'];
37+
}
38+
39+
publicfunctionunsupportedSchemeProvider():iterable
40+
{
41+
yield ['somethingElse://host/webhook'];
42+
}
43+
}
Lines changed: 118 additions & 0 deletions
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+
namespaceSymfony\Component\Notifier\Bridge\MicrosoftTeams\Tests;
13+
14+
useSymfony\Component\HttpClient\MockHttpClient;
15+
useSymfony\Component\HttpClient\Response\MockResponse;
16+
useSymfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsOptions;
17+
useSymfony\Component\Notifier\Bridge\MicrosoftTeams\MicrosoftTeamsTransport;
18+
useSymfony\Component\Notifier\Exception\TransportException;
19+
useSymfony\Component\Notifier\Message\ChatMessage;
20+
useSymfony\Component\Notifier\Message\MessageInterface;
21+
useSymfony\Component\Notifier\Message\SmsMessage;
22+
useSymfony\Component\Notifier\Notification\Notification;
23+
useSymfony\Component\Notifier\Test\TransportTestCase;
24+
useSymfony\Component\Notifier\Transport\TransportInterface;
25+
useSymfony\Contracts\HttpClient\HttpClientInterface;
26+
useSymfony\Contracts\HttpClient\ResponseInterface;
27+
28+
finalclass MicrosoftTeamsTransportTestextends TransportTestCase
29+
{
30+
/**
31+
* @return MicrosoftTeamsTransport
32+
*/
33+
publicfunctioncreateTransport(?HttpClientInterface$client =null):TransportInterface
34+
{
35+
return (newMicrosoftTeamsTransport('/testPath',$client ?:$this->createMock(HttpClientInterface::class)))->setHost('host.test');
36+
}
37+
38+
publicfunctiontoStringProvider():iterable
39+
{
40+
yield ['microsoftteams://host.test/testPath',$this->createTransport()];
41+
}
42+
43+
44+
publicfunctionsupportedMessagesProvider():iterable
45+
{
46+
yield [newChatMessage('Hello!')];
47+
}
48+
49+
publicfunctionunsupportedMessagesProvider():iterable
50+
{
51+
yield [newSmsMessage('0611223344','Hello!')];
52+
yield [$this->createMock(MessageInterface::class)];
53+
}
54+
55+
publicfunctiontestSendWithErrorResponseThrows()
56+
{
57+
$client =newMockHttpClient(function (string$method,string$url,array$options = []):ResponseInterface {
58+
returnnewMockResponse('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(newChatMessage('testMessage'));
67+
}
68+
69+
publicfunctiontestSendWithErrorRequestIdThrows()
70+
{
71+
$client =newMockHttpClient(newMockResponse());
72+
73+
$transport =$this->createTransport($client);
74+
75+
$this->expectException(TransportException::class);
76+
$this->expectExceptionMessageMatches('/request-id not found/');
77+
78+
$transport->send(newChatMessage('testMessage'));
79+
}
80+
81+
publicfunctiontestSendWithOptions()
82+
{
83+
$message ='testMessage';
84+
85+
$expectedBody =json_encode(['title' =>$message]);
86+
87+
$client =newMockHttpClient(function (string$method,string$url,array$options = [])use ($expectedBody):ResponseInterface {
88+
$this->assertJsonStringEqualsJsonString($expectedBody,$options['body']);
89+
90+
returnnewMockResponse('1', ['response_headers' => ['request-id' => ['testRequestId']],'http_code' =>200]);
91+
});
92+
93+
$transport =$this->createTransport($client);
94+
95+
$transport->send(newChatMessage($message));
96+
}
97+
98+
publicfunctiontestSendWithNotification()
99+
{
100+
$notification =newNotification('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 =newMockHttpClient(function (string$method,string$url,array$options = [])use ($expectedBody):ResponseInterface {
109+
$this->assertJsonStringEqualsJsonString($expectedBody,$options['body']);
110+
111+
returnnewMockResponse('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)

[8]ページ先頭

©2009-2025 Movatter.jp