8000 feature #47373 [Notifier] Add Chatwork Notifier Bridge (Ippey) · symfony/symfony@6903807 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6903807

Browse files
committed
feature #47373 [Notifier] Add Chatwork Notifier Bridge (Ippey)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Notifier] Add Chatwork Notifier Bridge | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT Add Chatwork Notifier Bridge. [Chatwork](https://go.chatwork.com/) is one of the popular chat tools in Japan. This bridge allows sending messages to Chatwork via [Chatwork API](https://developer.chatwork.com/docs). Commits ------- 6dcab51 [Notifier] Add Chatwork Notifier Bridge
2 parents b7ce8f2 + 6dcab51 commit 6903807

18 files changed

+601
-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\Mime\MimeTypes;
126126
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
127127
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
128+
use Symfony\Component\Notifier\Bridge\Chatwork\ChatworkTransportFactory;
128129
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
129130
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
130131
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
@@ -2564,6 +2565,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25642565
$classToServices = [
25652566
AllMySmsTransportFactory::class => 'notifier.transport_factory.all-my-sms',
25662567
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazon-sns',
2568+
ChatworkTransportFactory::class => 'notifier.transport_factory.chatwork',
25672569
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
25682570
ContactEveryoneTransportFactory::class => 'notifier.transport_factory.contact-everyone',
25692571
DiscordTransportFactory::class => 'notifier.transport_factory.discord',

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
1515
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
16+
use Symfony\Component\Notifier\Bridge\Chatwork\ChatworkTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
@@ -281,5 +282,10 @@
281282
->set('notifier.transport_factory.zendesk', ZendeskTransportFactory::class)
282283
->parent('notifier.transport_factory.abstract')
283284
->tag('chatter.transport_factory')
285+
286+
->set('notifier.transport_factory.chatwork', ChatworkTransportFactory::class)
287+
->parent('notifier.transport_factory.abstract')
288+
->tag('chatter.transport_factory')
289+
284290
;
285291
};
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+
6.2
5+
---
6+
7+
* Add the bridge
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Chatwork;
13+
14+
/**
15+
* @author Ippei Sumida <ippey.s@gmail.com>
16+
*/
17+
class ChatworkMessageBodyBuilder
18+
{
19+
private array $to = [];
20+
private string $body = '';
21+
private bool $selfUnread = false;
22+
23+
public function to(array|string $userIds): self
24+
{
25+
if (\is_array($userIds)) {
26+
$this->to = $userIds;
27+
} else {
28+
$this->to = [$userIds];
29+
}
30+
31+
return $this;
32+
}
33+
34+
public function body(string $body): self
35+
{
36+
$this->body = $body;
37+
38+
return $this;
39+
}
40+
41+
public function selfUnread(bool $selfUnread): self
42+
{
43+
$this->selfUnread = $selfUnread;
44+
45+
return $this;
46+
}
47+
48+
public function getMessageBody(): array
49+
{
50+
$content = '';
51+
foreach ($this->to as $to) {
52+
$content .= sprintf("[To:%s]\n", $to);
53+
}
54+
$content .= $this->body;
55+
56+
return [
57+
'body' => $content,
58+
'self_unread' => $this->selfUnread,
59+
];
60+
}
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\Chatwork;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Ippei Sumida <ippey.s@gmail.com>
18+
*/
19+
class ChatworkOptions implements MessageOptionsInterface
20+
{
21+
private array $options;
22+
23+
public function __construct(array $options = [])
24+
{
25+
$this->options = $options;
26+
}
27+
28+
public function toArray(): array
29+
{
30+
return $this->options;
31+
}
32+
33+
public function getRecipientId(): ?string
34+
{
35+
return '';
36+
}
37+
38+
public function to(array|string $userIds): static
39+
{
40+
$this->options['to'] = $userIds;
41+
42+
return $this;
43+
}
44+
45+
public function selfUnread(bool $selfUnread): static
46+
{
47+
$this->options['selfUnread'] = $selfUnread;
48+
49+
return $this;
50+
}
51+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\Chatwork;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
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 Ippei Sumida <ippey.s@gmail.com>
26+
*/
27+
class ChatworkTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.chatwork.com';
30+
31+
private string $apiToken;
32+
private string $roomId;
33+
34+
public function __construct(string $apiToken, string $roomId, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->apiToken = $apiToken;
37+
$this->roomId = $roomId;
38+
parent::__construct($client, $dispatcher);
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return sprintf('chatwork://%s?room_id=%s', $this->getEndpoint(), $this->roomId);
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof ChatworkOptions);
49+
}
50+
51+
protected function doSend(MessageInterface $message): SentMessage
52+
{
53+
if (!$message instanceof ChatMessage) {
54+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
55+
}
56+
57+
$messageOptions = $message->getOptions();
58+
$options = $messageOptions ? $messageOptions->toArray() : [];
59+
60+
$bodyBuilder = new ChatworkMessageBodyBuilder();
61+
if (\array_key_exists('to', $options)) {
62+
$bodyBuilder->to($options['to']);
63+
}
64+
if (\array_key_exists('selfUnread', $options)) {
65+
$bodyBuilder->selfUnread($options['selfUnread']);
66+
}
67+
68+
$messageBody = $bodyBuilder
69+
->body($message->getSubject())
70+
->getMessageBody();
71+
72+
$endpoint = sprintf('https://%s/v2/rooms/%s/messages', $this->getEndpoint(), $this->roomId);
73+
$response = $this->client->request('POST', $endpoint, [
74+
'body' => $messageBody,
75+
'headers' => [
76+
'X-ChatWorkToken' => $this->apiToken,
77+
'Content-Type' => 'application/x-www-form-urlencoded',
78+
],
79+
]);
80+
81+
try {
82+
$statusCode = $response->getStatusCode();
83+
} catch (TransportExceptionInterface $e) {
84+
throw new TransportException('Could not reach the remote Chatwork server.', $response, 0, $e);
85+
}
86+
87+
if (200 !== $statusCode) {
88+
$originalContent = $message->getSubject();
89+
$result = $response->toArray(false);
90+
$errors = $result['errors'];
91+
throw new TransportException(sprintf('Unable to post the Chatwork message: "%s" (%d: %s).', $originalContent, $statusCode, implode(', ', $errors)), $response);
92+
}
93+
94+
return new SentMessage($message, (string) $this);
95+
}
96+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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\Chatwork;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
* @author Ippei Sumida <ippey.s@gmail.com
20+
*/
21+
class ChatworkTransportFactory extends AbstractTransportFactory
22+
{
23+
private const SCHEME = 'chatwork';
24+
25+
protected function getSupportedSchemes(): array
26+
{
27+
return [self::SCHEME];
28+
}
29+
30+
public function create(Dsn $dsn): ChatworkTransport
31+
{
32+
$scheme = $dsn->getScheme();
33+
if (self::SCHEME !== $scheme) {
34+
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
35+
}
36+
37+
$token = $this->getUser($dsn);
38+
$roomId = $dsn->getRequiredOption('room_id');
39+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
40+
$port = $dsn->getPort();
41+
42+
return (new ChatworkTransport($token, $roomId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
43+
}
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2022 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Chatwork Notifier
2+
=================
3+
4+
Provides [Chatwork](https://go.chatwork.com/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
CHATWORK_DSN=chatwork://API_TOKEN@default?room_id=ID
11+
```
12+
13+
Resources
14+
---------
15+
16+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
17+
* [Report issues](https://github.com/symfony/symfony/issues) and
18+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
19+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)
0