Skip to content

Commit 145a2aa

Browse files
committed
[Notifier] Add Termii bridge
1 parent 69f46f2 commit 145a2aa

File tree

17 files changed

+655
-0
lines changed

17 files changed

+655
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
172172
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
173173
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
174+
use Symfony\Component\Notifier\Bridge\Termii\TermiiTransportFactory;
174175
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransport;
175176
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
176177
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
@@ -2592,6 +2593,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
25922593
SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
25932594
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
25942595
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
2596+
TermiiTransportFactory::class => 'notifier.transport_factory.termii',
25952597
TurboSmsTransport::class => 'notifier.transport_factory.turbo-sms',
25962598
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
25972599
VonageTransportFactory::class => 'notifier.transport_factory.vonage',

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
5858
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
5959
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
60+
use Symfony\Component\Notifier\Bridge\Termii\TermiiTransportFactory;
6061
use Symfony\Component\Notifier\Bridge\TurboSms\TurboSmsTransportFactory;
6162
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
6263
use Symfony\Component\Notifier\Bridge\Vonage\VonageTransportFactory;
@@ -287,5 +288,9 @@
287288
->parent('notifier.transport_factory.abstract')
288289
->tag('chatter.transport_factory')
289290

291+
->set('notifier.transport_factory.termii', TermiiTransportFactory::class)
292+
->parent('notifier.transport_factory.abstract')
293+
->tag('texter.transport_factory')
294+
290295
;
291296
};
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.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) 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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Termii Notifier
2+
=============
3+
4+
Provides [Termii](https://www.termii.com) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
TERMII_DSN=termii://API_KEY@default?from=FROM&channel=CHANNEL
11+
```
12+
13+
where:
14+
15+
- `API_KEY` is your Termii API key
16+
- `FROM` is your sender
17+
- `CHANNEL` is your channel (generic, dnd, whatsapp)
18+
19+
Resources
20+
---------
21+
22+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
23+
* [Report issues](https://github.com/symfony/symfony/issues) and
24+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
25+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\Termii;
12+
13+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
14+
15+
/**
16+
* @author gnito-org <https://github.com/gnito-org>
17+
*/
18+
final class TermiiOptions implements MessageOptionsInterface
19+
{
20+
private array $options;
21+
22+
public function __construct(array $options = [])
23+
{
24+
$this->options = $options;
25+
}
26+
27+
public function getChannel(): ?string
28+
{
29+
return $this->options['channel'] ?? null;
30+
}
31+
32+
public function getFrom(): ?string
33+
{
34+
return $this->options['from'] ?? null;
35+
}
36+
37+
public function getMediaCaption(): ?string
38+
{
39+
return $this->options['media_caption'] ?? null;
40+
}
41+
42+
public function getMediaUrl(): ?string
43+
{
44+
return $this->options['media_url'] ?? null;
45+
}
46+
47+
public function getRecipientId(): ?string
48+
{
49+
return $this->options['recipient_id'] ?? null;
50+
}
51+
52+
public function getType(): ?string
53+
{
54+
return $this->options['type'] ?? null;
55+
}
56+
57+
public function setChannel(string $channel): self
58+
{
59+
$this->options['channel'] = $channel;
60+
return $this;
61+
}
62+
63+
public function setFrom(string $from): self
64+
{
65+
$this->options['from'] = $from;
66+
return $this;
67+
}
68+
69+
public function setMediaCaption(string $mediaCaption): self
70+
{
71+
$this->options['media_caption'] = $mediaCaption;
72+
return $this;
73+
}
74+
75+
public function setMediaUrl(string $mediaUrl): self
76+
{
77+
$this->options['media_url'] = $mediaUrl;
78+
return $this;
79+
}
80+
81+
public function setRecipientId(string $id): self
82+
{
83+
$this->options['recipient_id'] = $id;
84+
return $this;
85+
}
86+
87+
public function setType(string $type): self
88+
{
89+
$this->options['type'] = $type;
90+
return $this;
91+
}
92+
93+
public function toArray(): array
94+
{
95+
return $this->options;
96+
}
97+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\Termii;
12+
13+
use Symfony\Component\HttpClient\Exception\JsonException;
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
23+
use Symfony\Contracts\HttpClient\HttpClientInterface;
24+
25+
/**
26+
* @author gnito-org <https://github.com/gnito-org>
27+
*/
28+
final class TermiiTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.ng.termii.com';
31+
32+
private string $apiKey;
33+
34+
private string $from;
35+
36+
private string $channel;
37+
38+
public function __construct(
39+
#[\SensitiveParameter] string $apiKey,
40+
string $from,
41+
string $channel,
42+
HttpClientInterface $client = null,
43+
EventDispatcherInterface $dispatcher = null
44+
) {
45+
$this->apiKey = $apiKey;
46+
$this->from = $from;
47+
$this->channel = $channel;
48+
parent::__construct(
49+
$client,
50+
$dispatcher
51+
);
52+
}
53+
54+
public function __toString(): string
55+
{
56+
return sprintf(
57+
'termii://%s?from=%s&channel=%s',
58+
$this->getEndpoint(),
59+
$this->from,
60+
$this->channel
61+
);
62+
}
63+
64+
public function supports(MessageInterface $message): bool
65+
{
66+
return $message instanceof SmsMessage;
67+
}
68+
69+
protected function doSend(MessageInterface $message): SentMessage
70+
{
71+
if (!$message instanceof SmsMessage) {
72+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
73+
}
74+
$opts = $message->getOptions();
75+
$options = $opts ? $opts->toArray() : [];
76+
$options['api_key'] = $this->apiKey;
77+
$options['sms'] = $message->getSubject();
78+
$options['from'] = $options['from'] ?? $this->from;
79+
$options['to'] = $message->getPhone();
80+
$options['channel'] = $options['channel'] ?? $this->channel;
81+
$options['type'] = $options['type'] ?? 'plain';
82+
if (isset($options['media_url'])) {
83+
$options['media']['url'] = $options['media_url'] ?? null;
84+
$options['media']['caption'] = $options['media_caption'] ?? null;
85+
unset($options['media_url'], $options['media_caption']);
86+
}
87+
if (!preg_match(
88+
'/^[a-zA-Z0-9\s]{3,11}$/',
89+
$options['from']
90+
) && !preg_match(
91+
'/^\+?[1-9]\d{1,14}$/',
92+
$options['from']
93+
)) {
94+
throw new InvalidArgumentException(sprintf('The "From" number "%s" is not a valid phone number, shortcode, or alphanumeric sender ID.', $this->from));
95+
}
96+
$endpoint = sprintf(
97+
'https://%s/api/sms/send',
98+
$this->getEndpoint()
99+
);
100+
$response = $this->client->request(
101+
'POST',
102+
$endpoint,
103+
[
104+
'json' => array_filter($options),
105+
]
106+
);
107+
try {
108+
$statusCode = $response->getStatusCode();
109+
} catch (TransportExceptionInterface $e) {
110+
throw new TransportException('Could not reach the remote Termii server.', $response, 0, $e);
111+
}
112+
if (200 !== $statusCode) {
113+
try {
114+
$error = $response->toArray(false);
115+
} catch (JsonException) {
116+
$error['message'] = $response->getContent(false);
117+
}
118+
throw new TransportException(sprintf('Unable to send the SMS - status code: %s: %s', $statusCode, $error['message'] ?? 'unknown error'), $response);
119+
}
120+
$success = $response->toArray(false);
121+
$sentMessage = new SentMessage(
122+
$message,
123+
(string) $this
124+
);
125+
$sentMessage->setMessageId($success['message_id']);
126+
127+
return $sentMessage;
128+
}
129+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Notifier\Bridge\Termii;
12+
13+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
14+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
15+
use Symfony\Component\Notifier\Transport\Dsn;
16+
17+
/**
18+
* @author gnito-org <https://github.com/gnito-org>
19+
*/
20+
final class TermiiTransportFactory extends AbstractTransportFactory
21+
{
22+
private const TRANSPORT_SCHEME = 'termii';
23+
24+
public function create(Dsn $dsn): TermiiTransport
25+
{
26+
$scheme = $dsn->getScheme();
27+
if (self::TRANSPORT_SCHEME !== $scheme) {
28+
throw new UnsupportedSchemeException($dsn, self::TRANSPORT_SCHEME, $this->getSupportedSchemes());
29+
}
30+
$apiKey = $this->getUser($dsn);
31+
$from = $dsn->getRequiredOption('from');
32+
$channel = $dsn->getRequiredOption('channel');
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$port = $dsn->getPort();
35+
36+
return (new TermiiTransport(
37+
$apiKey,
38+
$from,
39+
$channel,
40+
$this->client,
41+
$this->dispatcher
42+
))->setHost($host)
43+
->setPort($port)
44+
;
45+
}
46+
47+
protected function getSupportedSchemes(): array
48+
{
49+
return [self::TRANSPORT_SCHEME];
50+
}
51+
}

0 commit comments

Comments
 (0)