Skip to content

Commit c4cda75

Browse files
GromNaNfabpot
authored andcommitted
[Notifier] Add Google Chat bridge
1 parent e1cfbd2 commit c4cda75

File tree

15 files changed

+740
-0
lines changed

15 files changed

+740
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
use Symfony\Component\Mime\MimeTypes;
9797
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
9898
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
99+
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
99100
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
100101
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
101102
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -2076,6 +2077,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20762077
SlackTransportFactory::class => 'notifier.transport_factory.slack',
20772078
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
20782079
MattermostTransportFactory::class => 'notifier.transport_factory.mattermost',
2080+
GoogleChatTransportFactory::class => 'notifier.transport_factory.googlechat',
20792081
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
20802082
RocketChatTransportFactory::class => 'notifier.transport_factory.rocketchat',
20812083
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',

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

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

1414
use Symfony\Component\Notifier\Bridge\Firebase\FirebaseTransportFactory;
1515
use Symfony\Component\Notifier\Bridge\FreeMobile\FreeMobileTransportFactory;
16+
use Symfony\Component\Notifier\Bridge\GoogleChat\GoogleChatTransportFactory;
1617
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
1718
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
1819
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -51,6 +52,10 @@
5152
->parent('notifier.transport_factory.abstract')
5253
->tag('chatter.transport_factory')
5354

55+
->set('notifier.transport_factory.googlechat', GoogleChatTransportFactory::class)
56+
->parent('notifier.transport_factory.abstract')
57+
->tag('chatter.transport_factory')
58+
5459
->set('notifier.transport_factory.twilio', TwilioTransportFactory::class)
5560
->parent('notifier.transport_factory.abstract')
5661
->tag('texter.transport_factory')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-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.2.0
5+
-----
6+
7+
* Added the bridge
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\GoogleChat;
13+
14+
use Symfony\Component\Notifier\Bridge\GoogleChat\Component\Card;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
17+
use Symfony\Component\Notifier\Notification\Notification;
18+
19+
/**
20+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
final class GoogleChatOptions implements MessageOptionsInterface
25+
{
26+
private $threadKey;
27+
private $options = [];
28+
29+
public function __construct(array $options = [])
30+
{
31+
$this->options = $options;
32+
}
33+
34+
public static function fromNotification(Notification $notification): self
35+
{
36+
$options = new self();
37+
38+
$text = $notification->getEmoji().' *'.$notification->getSubject().'* ';
39+
40+
if ($notification->getContent()) {
41+
$text .= "\r\n".$notification->getContent();
42+
}
43+
if ($exception = $notification->getExceptionAsString()) {
44+
$text .= "\r\n".'```'.$notification->getExceptionAsString().'```';
45+
}
46+
47+
$options->text($text);
48+
49+
return $options;
50+
}
51+
52+
public static function fromMessage(ChatMessage $message): self
53+
{
54+
$options = new self();
55+
56+
$options->text($message->getSubject());
57+
58+
return $options;
59+
}
60+
61+
public function toArray(): array
62+
{
63+
return $this->options;
64+
}
65+
66+
public function card(array $card): self
67+
{
68+
$this->options['cards'][] = $card;
69+
70+
return $this;
71+
}
72+
73+
public function text(string $text): self
74+
{
75+
$this->options['text'] = $text;
76+
77+
return $this;
78+
}
79+
80+
public function setThreadKey(?string $threadKey): self
81+
{
82+
$this->threadKey = $threadKey;
83+
84+
return $this;
85+
}
86+
87+
public function getThreadKey(): ?string
88+
{
89+
return $this->threadKey;
90+
}
91+
92+
public function getRecipientId(): ?string
93+
{
94+
return null;
95+
}
96+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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\GoogleChat;
13+
14+
use Symfony\Component\HttpClient\Exception\JsonException;
15+
use Symfony\Component\Notifier\Exception\LogicException;
16+
use Symfony\Component\Notifier\Exception\TransportException;
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 Jérôme Tamarelle <jerome@tamarelle.net>
26+
*
27+
* @internal
28+
*
29+
* @experimental in 5.2
30+
*/
31+
final class GoogleChatTransport extends AbstractTransport
32+
{
33+
protected const HOST = 'chat.googleapis.com';
34+
35+
private $space;
36+
private $accessKey;
37+
private $accessToken;
38+
39+
/**
40+
* @var ?string
41+
*/
42+
private $threadKey;
43+
44+
/**
45+
* @param string $space The space name the the webhook url "/v1/spaces/<space>/messages"
46+
* @param string $accessKey The "key" parameter of the webhook url
47+
* @param string $accessToken The "token" parameter of the webhook url
48+
*/
49+
public function __construct(string $space, string $accessKey, string $accessToken, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
50+
{
51+
$this->space = $space;
52+
$this->accessKey = $accessKey;
53+
$this->accessToken = $accessToken;
54+
55+
parent::__construct($client, $dispatcher);
56+
}
57+
58+
/**
59+
* Opaque thread identifier string that can be specified to group messages into a single thread.
60+
* If this is the first message with a given thread identifier, a new thread is created.
61+
* Subsequent messages with the same thread identifier will be posted into the same thread.
62+
*
63+
* @see https://developers.google.com/hangouts/chat/reference/rest/v1/spaces.messages/create#query-parameters
64+
*/
65+
public function setThreadKey(?string $threadKey): self
66+
{
67+
$this->threadKey = $threadKey;
68+
69+
return $this;
70+
}
71+
72+
public function __toString(): string
73+
{
74+
return sprintf('googlechat://%s/%s%s',
75+
$this->getEndpoint(),
76+
$this->space,
77+
$this->threadKey ? '?threadKey='.urlencode($this->threadKey) : ''
78+
);
79+
}
80+
81+
public function supports(MessageInterface $message): bool
82+
{
83+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof GoogleChatOptions);
84+
}
85+
86+
/**
87+
* @see https://developers.google.com/hangouts/chat/how-tos/webhooks
88+
*/
89+
protected function doSend(MessageInterface $message): SentMessage
90+
{
91+
if (!$message instanceof ChatMessage) {
92+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
93+
}
94+
if ($message->getOptions() && !$message->getOptions() instanceof GoogleChatOptions) {
95+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, GoogleChatOptions::class));
96+
}
97+
98+
$opts = $message->getOptions();
99+
if (!$opts) {
100+
if ($notification = $message->getNotification()) {
101+
$opts = GoogleChatOptions::fromNotification($notification);
102+
} else {
103+
$opts = GoogleChatOptions::fromMessage($message);
104+
}
105+
}
106+
107+
if (null !== $this->threadKey && null === $opts->getThreadKey()) {
108+
$opts->setThreadKey($this->threadKey);
109+
}
110+
111+
$threadKey = $opts->getThreadKey() ?: $this->threadKey;
112+
113+
$options = $opts->toArray();
114+
$url = sprintf('https://%s/v1/spaces/%s/messages?key=%s&token=%s%s',
115+
$this->getEndpoint(),
116+
$this->space,
117+
urlencode($this->accessKey),
118+
urlencode($this->accessToken),
119+
$threadKey ? '&threadKey='.urlencode($threadKey) : ''
120+
);
121+
$response = $this->client->request('POST', $url, [
122+
'json' => array_filter($options),
123+
]);
124+
125+
try {
126+
$result = $response->toArray(false);
127+
} catch (JsonException $jsonException) {
128+
throw new TransportException(sprintf('Unable to post the Google Chat message: Invalid response.'), $response, $response->getStatusCode(), $jsonException);
129+
}
130+
131+
if (200 !== $response->getStatusCode()) {
132+
throw new TransportException(sprintf('Unable to post the Google Chat message: "%s".', $result['error']['message'] ?? $response->getContent(false)), $response, $result['error']['code'] ?? $response->getStatusCode());
133+
}
134+
135+
if (!\array_key_exists('name', $result)) {
136+
throw new TransportException(sprintf('Unable to post the Google Chat message: "%s".', $response->getContent(false)), $response);
137+
}
138+
139+
$sentMessage = new SentMessage($message, (string) $this);
140+
$sentMessage->setMessageId($result['name']);
141+
142+
return $sentMessage;
143+
}
144+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\GoogleChat;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Jérôme Tamarelle <jerome@tamarelle.net>
21+
*
22+
* @experimental in 5.2
23+
*/
24+
final class GoogleChatTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @param Dsn $dsn Format: googlechat://<key>:<token>@default/<space>?threadKey=<thread>
28+
*
29+
* @return GoogleChatTransport
30+
*/
31+
public function create(Dsn $dsn): TransportInterface
32+
{
33+
$scheme = $dsn->getScheme();
34+
35+
if ('googlechat' === $scheme) {
36+
$space = explode('/', $dsn->getPath())[1];
37+
$accessKey = $this->getUser($dsn);
38+
$accessToken = $this->getPassword($dsn);
39+
$threadKey = $dsn->getOption('threadKey');
40+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
41+
$port = $dsn->getPort();
42+
43+
return (new GoogleChatTransport($space, $accessKey, $accessToken, $this->client, $this->dispatcher))
44+
->setThreadKey($threadKey)
45+
->setHost($host)
46+
->setPort($port);
47+
}
48+
49+
throw new UnsupportedSchemeException($dsn, 'googlechat', $this->getSupportedSchemes());
50+
}
51+
52+
protected function getSupportedSchemes(): array
53+
{
54+
return ['googlechat'];
55+
}
56+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2019 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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Google Chat Notifier
2+
====================
3+
4+
Provides Google Chat integration for Symfony Notifier.
5+
6+
googlechat://ACCESS_KEY:ACCESS_TOKEN@default/SPACE?threadKey=THREAD_KEY
7+
8+
Resources
9+
---------
10+
11+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
12+
* [Report issues](https://github.com/symfony/symfony/issues) and
13+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
14+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy