Skip to content

Commit 33fbf71

Browse files
author
alanzarli
committed
[Notifier][Webhook] Add Smsbox support
1 parent 05d5bb3 commit 33fbf71

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,6 +3045,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
30453045
$loader->load('notifier_webhook.php');
30463046

30473047
$webhookRequestParsers = [
3048+
NotifierBridge\Smsbox\Webhook\SmsboxRequestParser::class => 'notifier.webhook.request_parser.smsbox',
30483049
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
30493050
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
30503051
NotifierBridge\Vonage\Webhook\VonageRequestParser::class => 'notifier.webhook.request_parser.vonage',

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

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

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

1819
return static function (ContainerConfigurator $container) {
1920
$container->services()
21+
->set('notifier.webhook.request_parser.smsbox', SmsboxRequestParser::class)
22+
->alias(SmsboxRequestParser::class, 'notifier.webhook.request_parser.smsbox')
23+
2024
->set('notifier.webhook.request_parser.sweego', SweegoRequestParser::class)
2125
->alias(SweegoRequestParser::class, 'notifier.webhook.request_parser.sweego')
2226

src/Symfony/Component/Notifier/Bridge/Smsbox/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ $options = (new SmsboxOptions())
5454
$sms->options($options);
5555
$texter->send($sms);
5656
```
57+
## Smsbox notifier also provides Webhooks support. 🚀
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
4+
5+
parse_str(trim(file_get_contents(str_replace('.php', '.txt', __FILE__))), $payload);
6+
$wh = new SmsEvent(SmsEvent::DELIVERED, '250207960297', $payload);
7+
$wh->setRecipientPhone('33612346578');
8+
9+
return $wh;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
numero=33612346578&reference=250207960297&accuse=0&ts=1737368770
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Smsbox\Tests\Webhook;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Notifier\Bridge\Smsbox\Webhook\SmsboxRequestParser;
16+
use Symfony\Component\Webhook\Client\RequestParserInterface;
17+
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase;
18+
19+
class SmsboxRequestParserTest extends AbstractRequestParserTestCase
20+
{
21+
protected function createRequestParser(): RequestParserInterface
22+
{
23+
return new SmsboxRequestParser();
24+
}
25+
26+
protected function createRequest(string $payload): Request
27+
{
28+
parse_str(trim($payload), $parameters);
29+
30+
return Request::create('/', 'GET', $parameters, [], [], []);
31+
}
32+
33+
protected static function getFixtureExtension(): string
34+
{
35+
return 'txt';
36+
}
37+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Smsbox\Webhook;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
16+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
17+
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
18+
use Symfony\Component\Webhook\Client\AbstractRequestParser;
19+
use Symfony\Component\Webhook\Exception\RejectWebhookException;
20+
21+
final class SmsboxRequestParser extends AbstractRequestParser
22+
{
23+
protected function getRequestMatcher(): RequestMatcherInterface
24+
{
25+
return new MethodRequestMatcher(['GET']);
26+
}
27+
28+
protected function doParse(Request $request, #[\SensitiveParameter] string $secret): ?SmsEvent
29+
{
30+
$payload = $request->query->all();
31+
32+
if (
33+
!isset($payload['numero'])
34+
|| !isset($payload['reference'])
35+
|| !isset($payload['accuse'])
36+
|| !isset($payload['ts'])
37+
) {
38+
throw new RejectWebhookException(406, 'Payload is malformed.');
39+
}
40+
41+
$name = match ($payload['accuse']) {
42+
// Documentation for SMSBOX dlr code https://www.smsbox.net/en/tools-development#doc-sms-accusees
43+
'-3' => SmsEvent::FAILED,
44+
'-1' => null,
45+
'0' => SmsEvent::DELIVERED,
46+
'1' => SmsEvent::FAILED,
47+
'2' => SmsEvent::FAILED,
48+
'3' => SmsEvent::FAILED,
49+
'4' => SmsEvent::FAILED,
50+
'5' => SmsEvent::FAILED,
51+
'6' => SmsEvent::FAILED,
52+
'7' => SmsEvent::FAILED,
53+
'8' => SmsEvent::FAILED,
54+
'9' => null,
55+
'10' => null,
56+
default => throw new RejectWebhookException(406, \sprintf('Unknown status: %s', $payload['accuse'])),
57+
};
58+
59+
$event = new SmsEvent($name, $payload['reference'], $payload);
60+
$event->setRecipientPhone($payload['numero']);
61+
62+
return $event;
63+
}
64+
}

0 commit comments

Comments
 (0)