Skip to content

[Notifier] Add Chatwork Notifier Bridge #47373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
use Symfony\Component\Notifier\Bridge\Chatwork\ChatworkTransportFactory;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
Expand Down Expand Up @@ -2564,6 +2565,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
$classToServices = [
AllMySmsTransportFactory::class => 'notifier.transport_factory.all-my-sms',
AmazonSnsTransportFactory::class => 'notifier.transport_factory.amazon-sns',
ChatworkTransportFactory::class => 'notifier.transport_factory.chatwork',
ClickatellTransportFactory::class => 'notifier.transport_factory.clickatell',
ContactEveryoneTransportFactory::class => 'notifier.transport_factory.contact-everyone',
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Notifier\Bridge\AllMySms\AllMySmsTransportFactory;
use Symfony\Component\Notifier\Bridge\AmazonSns\AmazonSnsTransportFactory;
use Symfony\Component\Notifier\Bridge\Chatwork\ChatworkTransportFactory;
use Symfony\Component\Notifier\Bridge\Clickatell\ClickatellTransportFactory;
use Symfony\Component\Notifier\Bridge\ContactEveryone\ContactEveryoneTransportFactory;
use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory;
Expand Down Expand Up @@ -281,5 +282,10 @@
->set('notifier.transport_factory.zendesk', ZendeskTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.chatwork', ChatworkTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Chatwork/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Chatwork/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

6.2
---

* Add the bridge
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Chatwork;

/**
* @author Ippei Sumida <ippey.s@gmail.com>
*/
class ChatworkMessageBodyBuilder
{
private array $to = [];
private string $body = '';
private bool $selfUnread = false;

public function to(array|string $userIds): self
{
if (\is_array($userIds)) {
$this->to = $userIds;
} else {
$this->to = [$userIds];
}

return $this;
}

public function body(string $body): self
{
$this->body = $body;

return $this;
}

public function selfUnread(bool $selfUnread): self
{
$this->selfUnread = $selfUnread;

return $this;
}

public function getMessageBody(): array
{
$content = '';
foreach ($this->to as $to) {
$content .= sprintf("[To:%s]\n", $to);
}
$content .= $this->body;

return [
'body' => $content,
'self_unread' => $this->selfUnread,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Chatwork;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author Ippei Sumida <ippey.s@gmail.com>
*/
class ChatworkOptions implements MessageOptionsInterface
{
private array $options;

public function __construct(array $options = [])
{
$this->options = $options;
}

public function toArray(): array
{
return $this->options;
}

public function getRecipientId(): ?string
{
return '';
}

public function to(array|string $userIds): static
{
$this->options['to'] = $userIds;

return $this;
}

public function selfUnread(bool $selfUnread): static
{
$this->options['selfUnread'] = $selfUnread;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Chatwork;

use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\TransportExceptionInterface;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Ippei Sumida <ippey.s@gmail.com>
*/
class ChatworkTransport extends AbstractTransport
{
protected const HOST = 'api.chatwork.com';

private string $apiToken;
private string $roomId;

public function __construct(string $apiToken, string $roomId, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->apiToken = $apiToken;
$this->roomId = $roomId;
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('chatwork://%s?room_id=%s', $this->getEndpoint(), $this->roomId);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof ChatworkOptions);
}

protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
}

$messageOptions = $message->getOptions();
$options = $messageOptions ? $messageOptions->toArray() : [];

$bodyBuilder = new ChatworkMessageBodyBuilder();
if (\array_key_exists('to', $options)) {
$bodyBuilder->to($options['to']);
}
if (\array_key_exists('selfUnread', $options)) {
$bodyBuilder->selfUnread($options['selfUnread']);
}

$messageBody = $bodyBuilder
->body($message->getSubject())
->getMessageBody();

$endpoint = sprintf('https://%s/v2/rooms/%s/messages', $this->getEndpoint(), $this->roomId);
$response = $this->client->request('POST', $endpoint, [
'body' => $messageBody,
'headers' => [
'X-ChatWorkToken' => $this->apiToken,
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);

try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Chatwork server.', $response, 0, $e);
}

if (200 !== $statusCode) {
$originalContent = $message->getSubject();
$result = $response->toArray(false);
$errors = $result['errors'];
throw new TransportException(sprintf('Unable to post the Chatwork message: "%s" (%d: %s).', $originalContent, $statusCode, implode(', ', $errors)), $response);
}

return new SentMessage($message, (string) $this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Chatwork;

use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;

/**
* @author Ippei Sumida <ippey.s@gmail.com
*/
class ChatworkTransportFactory extends AbstractTransportFactory
{
private const SCHEME = 'chatwork';

protected function getSupportedSchemes(): array
{
return [self::SCHEME];
}

public function create(Dsn $dsn): ChatworkTransport
{
$scheme = $dsn->getScheme();
if (self::SCHEME !== $scheme) {
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
}

$token = $this->getUser($dsn);
$roomId = $dsn->getRequiredOption('room_id');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new ChatworkTransport($token, $roomId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}
}
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Chatwork/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Chatwork/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Chatwork Notifier
=================

Provides [Chatwork](https://go.chatwork.com/) integration for Symfony Notifier.

DSN example
-----------

```
CHATWORK_DSN=chatwork://API_TOKEN@default?room_id=ID
```

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Loading