Skip to content

Commit ba55f32

Browse files
committed
Add Sweego Notifier bridge
1 parent 2d1fe22 commit ba55f32

22 files changed

+662
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
28812881
NotifierBridge\SmsSluzba\SmsSluzbaTransportFactory::class => 'notifier.transport_factory.sms-sluzba',
28822882
NotifierBridge\Smsense\SmsenseTransportFactory::class => 'notifier.transport_factory.smsense',
28832883
NotifierBridge\SpotHit\SpotHitTransportFactory::class => 'notifier.transport_factory.spot-hit',
2884+
NotifierBridge\Sweego\SweegoTransportFactory::class => 'notifier.transport_factory.sweego',
28842885
NotifierBridge\Telegram\TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
28852886
NotifierBridge\Telnyx\TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
28862887
NotifierBridge\Termii\TermiiTransportFactory::class => 'notifier.transport_factory.termii',
@@ -2948,6 +2949,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
29482949
$loader->load('notifier_webhook.php');
29492950

29502951
$webhookRequestParsers = [
2952+
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
29512953
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
29522954
NotifierBridge\Vonage\Webhook\VonageRequestParser::class => 'notifier.webhook.request_parser.vonage',
29532955
];

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

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
'smsense' => Bridge\Smsense\SmsenseTransportFactory::class,
109109
'smsmode' => Bridge\Smsmode\SmsmodeTransportFactory::class,
110110
'spot-hit' => Bridge\SpotHit\SpotHitTransportFactory::class,
111+
'sweego' => Bridge\Sweego\SweegoTransportFactory::class,
111112
'telnyx' => Bridge\Telnyx\TelnyxTransportFactory::class,
112113
'termii' => Bridge\Termii\TermiiTransportFactory::class,
113114
'turbo-sms' => Bridge\TurboSms\TurboSmsTransportFactory::class,

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

+4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Component\Notifier\Bridge\Sweego\Webhook\SweegoRequestParser;
1415
use Symfony\Component\Notifier\Bridge\Twilio\Webhook\TwilioRequestParser;
1516
use Symfony\Component\Notifier\Bridge\Vonage\Webhook\VonageRequestParser;
1617

1718
return static function (ContainerConfigurator $container) {
1819
$container->services()
20+
->set('notifier.webhook.request_parser.sweego', SweegoRequestParser::class)
21+
->alias(SweegoRequestParser::class, 'notifier.webhook.request_parser.sweego')
22+
1923
->set('notifier.webhook.request_parser.twilio', TwilioRequestParser::class)
2024
->alias(TwilioRequestParser::class, 'notifier.webhook.request_parser.twilio')
2125

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.git* export-ignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
7.2
5+
---
6+
7+
* Add the bridge
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2024-present 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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Sweego Notifier
2+
===============
3+
4+
Provides [Sweego](https://www.sweego.io/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
SWEEGO_DSN=sweego://API_KEY@default?region=REGION&campaign_type=CAMPAIGN_TYPE&bat=BAT&campaign_id=CAMPAIGN_ID&shorten_urls=SHORTEN_URLS&shorten_with_protocol=SHORTEN_WITH_PROTOCOL
11+
```
12+
13+
where:
14+
- `API_KEY` (required) is your Sweego API key
15+
- `REGION` (required) is the region of the phone number (e.g. `FR`, ISO 3166-1 alpha-2 country code)
16+
- `CAMPAIGN_TYPE` (required) is the type of the campaign (e.g. `transac`)
17+
- `BAT` (optional) is the test mode (e.g. `true`)
18+
- `CAMPAIGN_ID` (optional) is the campaign id (e.g. `string`)
19+
- `SHORTEN_URLS` (optional) is the shorten urls option (e.g. `true`)
20+
- `SHORTEN_WITH_PROTOCOL` (optional) is the shorten with protocol option (e.g. `true`)
21+
22+
Advanced Message options
23+
------------------------
24+
25+
```php
26+
use Symfony\Component\Notifier\Message\SmsMessage;
27+
use Symfony\Component\Notifier\Bridge\Sweego\SweegoOptions;
28+
29+
$sms = new SmsMessage('+1411111111', 'My message');
30+
31+
$options = (new SweegoOptions())
32+
// False by default, set 'bat' to true enable test mode (no sms sent, only for testing purpose)
33+
->bat(true)
34+
// Optional, used for tracking / filtering purpose on our platform; identity an SMS campaign and allow to see logs / stats only for this campaign
35+
->campaignId('string')
36+
// True by default, we replace all url in the SMS content by a shortened url version (reduce the characters of the sms)
37+
->shortenUrls(true)
38+
// True by default, add scheme to shortened url version
39+
->shortenWithProtocol(true);
40+
41+
// Add the custom options to the sms message and send the message
42+
$sms->options($options);
43+
44+
$texter->send($sms);
45+
```
46+
47+
Resources
48+
---------
49+
50+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
51+
* [Report issues](https://github.com/symfony/symfony/issues) and
52+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
53+
in the [main Symfony repository](https://github.com/symfony/symfony)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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\Sweego;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
class SweegoOptions implements MessageOptionsInterface
17+
{
18+
public const REGION = 'region';
19+
public const BAT = 'bat';
20+
public const CAMPAIGN_TYPE = 'campaign_type';
21+
public const CAMPAIGN_ID = 'campaign_id';
22+
public const SHORTEN_URLS = 'shorten_urls';
23+
public const SHORTEN_WITH_PROTOCOL = 'shorten_with_protocol';
24+
25+
public function __construct(
26+
private array $options = [],
27+
) {
28+
}
29+
30+
public function toArray(): array
31+
{
32+
return $this->options;
33+
}
34+
35+
public function getRecipientId(): ?string
36+
{
37+
return null;
38+
}
39+
40+
public function bat(bool $bat): self
41+
{
42+
$this->options[self::BAT] = $bat;
43+
44+
return $this;
45+
}
46+
47+
public function campaignId(string $campaignId): self
48+
{
49+
$this->options[self::CAMPAIGN_ID] = $campaignId;
50+
51+
return $this;
52+
}
53+
54+
public function shortenUrls(bool $shortenUrls): self
55+
{
56+
$this->options[self::SHORTEN_URLS] = $shortenUrls;
57+
58+
return $this;
59+
}
60+
61+
public function shortenWithProtocol(bool $shortenWithProtocol): self
62+
{
63+
$this->options[self::SHORTEN_WITH_PROTOCOL] = $shortenWithProtocol;
64+
65+
return $this;
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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\Sweego;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Mathieu Santostefano <msantostefano@protonmail.com>
26+
*/
27+
final class SweegoTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.sweego.io';
30+
31+
public function __construct(
32+
#[\SensitiveParameter] private readonly string $apiKey,
33+
private readonly string $region,
34+
private readonly string $campaignType,
35+
private readonly ?bool $bat,
36+
private readonly ?string $campaignId,
37+
private readonly ?bool $shortenUrls,
38+
private readonly ?bool $shortenWithProtocol,
39+
?HttpClientInterface $client = null,
40+
?EventDispatcherInterface $dispatcher = null,
41+
) {
42+
parent::__construct($client, $dispatcher);
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return \sprintf('sweego://%s%s', $this->getEndpoint(), '?'.http_build_query([
48+
SweegoOptions::REGION => $this->region,
49+
SweegoOptions::CAMPAIGN_TYPE => $this->campaignType,
50+
SweegoOptions::BAT => $this->bat,
51+
SweegoOptions::CAMPAIGN_ID => $this->campaignId,
52+
SweegoOptions::SHORTEN_URLS => $this->shortenUrls,
53+
SweegoOptions::SHORTEN_WITH_PROTOCOL => $this->shortenWithProtocol,
54+
]));
55+
}
56+
57+
public function supports(MessageInterface $message): bool
58+
{
59+
return $message instanceof SmsMessage
60+
&& (null === $message->getOptions() || $message->getOptions() instanceof SweegoOptions);
61+
}
62+
63+
protected function doSend(MessageInterface $message): SentMessage
64+
{
65+
if (!$message instanceof SmsMessage) {
66+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
67+
}
68+
69+
$options = $message->getOptions()?->toArray() ?? [];
70+
71+
$body = [
72+
'recipients' => [
73+
[
74+
'num' => $message->getPhone(),
75+
'region' => $options[SweegoOptions::REGION] ?? $this->region,
76+
],
77+
],
78+
'message-txt' => $message->getSubject(),
79+
'channel' => 'sms',
80+
'provider' => 'sweego',
81+
];
82+
83+
$body = $this->setBat($body, $options);
84+
$body = $this->setCampaignType($body, $options);
85+
$body = $this->setCampaignId($body, $options);
86+
$body = $this->setShortenUrls($body, $options);
87+
$body = $this->setShortenWithProtocol($body, $options);
88+
89+
$endpoint = \sprintf('https://%s/send', $this->getEndpoint());
90+
$response = $this->client->request('POST', $endpoint, [
91+
'headers' => [
92+
'Api-Key' => $this->apiKey,
93+
],
94+
'json' => array_filter($body),
95+
]);
96+
97+
try {
98+
$statusCode = $response->getStatusCode();
99+
} catch (TransportExceptionInterface $e) {
100+
throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e);
101+
}
102+
103+
if (200 !== $statusCode) {
104+
throw new TransportException('Unable to send the SMS.', $response);
105+
}
106+
107+
$success = $response->toArray(false);
108+
109+
$sentMessage = new SentMessage($message, (string) $this);
110+
$sentMessage->setMessageId(array_values($success['swg_uids'])[0]);
111+
112+
return $sentMessage;
113+
}
114+
115+
private function setBat(array $body, array $options): array
116+
{
117+
$body['bat'] = (bool) ($options[SweegoOptions::BAT] ?? $this->bat);
118+
119+
return $body;
120+
}
121+
122+
private function setCampaignType(array $body, array $options): array
123+
{
124+
$body['campaign-type'] = $this->campaignType;
125+
126+
if (\array_key_exists(SweegoOptions::CAMPAIGN_TYPE, $options) && \is_string($options[SweegoOptions::CAMPAIGN_TYPE])) {
127+
$body['campaign-type'] = $options[SweegoOptions::CAMPAIGN_TYPE];
128+
}
129+
130+
return $body;
131+
}
132+
133+
private function setCampaignId(array $body, array $options): array
134+
{
135+
$body['campaign-id'] = $this->campaignId;
136+
137+
if (\array_key_exists(SweegoOptions::CAMPAIGN_ID, $options) && \is_string($options[SweegoOptions::CAMPAIGN_ID])) {
138+
$body['campaign-id'] = $options[SweegoOptions::CAMPAIGN_ID];
139+
}
140+
141+
return $body;
142+
}
143+
144+
private function setShortenUrls(array $body, array $options): array
145+
{
146+
$body['shorten_urls'] = (bool) ($options[SweegoOptions::SHORTEN_URLS] ?? $this->shortenUrls);
147+
148+
return $body;
149+
}
150+
151+
private function setShortenWithProtocol(array $body, array $options): array
152+
{
153+
$body['shorten_with_protocol'] = (bool) ($options[SweegoOptions::SHORTEN_WITH_PROTOCOL] ?? $this->shortenWithProtocol);
154+
155+
return $body;
156+
}
157+
}

0 commit comments

Comments
 (0)