8000 [Notifier][Webhook] Add Smsbox support · symfony/symfony@95bb117 · GitHub
[go: up one dir, main page]

Skip to content

Commit 95bb117

Browse files
author
alanzarli
committed
[Notifier][Webhook] Add Smsbox support
1 parent a7a32f7 commit 95bb117

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

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

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

30503050
$webhookRequestParsers = [
3051+
NotifierBridge\Smsbox\Webhook\SmsboxRequestParser::class => 'notifier.webhook.request_parser.smsbox',
30513052
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
30523053
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
30533054
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, [], [], ['REMOTE_ADDR' => '37.59.198.135']);
31+
}
32+
33+
protected static function getFixtureExtension(): string
34+
{
35+
return 'txt';
36+
}
37+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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\ChainRequestMatcher;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher;
17+
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
18+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
19+
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
20+
use Symfony\Component\Webhook\Client\AbstractRequestParser;
21+
use Symfony\Component\Webhook\Exception\RejectWebhookException;
22+
23+
final class SmsboxRequestParser extends AbstractRequestParser
24+
{
25+
protected function getRequestMatcher(): RequestMatcherInterface
26+
{
27+
return new ChainRequestMatcher([
28+
new MethodRequestMatcher(['GET']),
29+
new IpsRequestMatcher([
30+
'37.59.198.135',
31+
'178.33.185.51',
32+
'54.36.93.79',
33+
'54.36.93.80',
34+
'62.4.31.47',
35+
'62.4.31.48',
36+
]),
37+
]);
38+
}
39+
40+
protected function doParse(Request $request, #[\SensitiveParameter] string $secret): ?SmsEvent
41+
{
42+
$payload = $request->query->all();
43+
44+
if (
45+
!isset($payload['numero'])
46+
|| !isset($payload['reference'])
47+
|| !isset($payload['accuse'])
48+
|| !isset($payload['ts'])
49+
) {
50+
throw new RejectWebhookException(406, 'Payload is malformed.');
51+
}
52+
53+
$name = match ($payload['accuse']) {
54+
// Documentation for SMSBOX dlr code https://www.smsbox.net/en/tools-development#doc-sms-accusees
55+
'-3' => SmsEvent::FAILED,
56+
'-1' => null,
57+
'0' => SmsEvent::DELIVERED,
58+
'1' => SmsEvent::FAILED,
59+
'2' => SmsEvent::FAILED,
60+
'3' => SmsEvent::FAILED,
61+
'4' => SmsEvent::FAILED,
62+
'5' => SmsEvent::FAILED,
63+
'6' => SmsEvent::FAILED,
64+
'7' => SmsEvent::FAILED,
65+
'8' => SmsEvent::FAILED,
66+
'9' => null,
67+
'10' => null,
68+
default => throw new RejectWebhookException(406, \sprintf('Unknown status: %s', $payload['accuse'])),
69+
};
70+
71+
$event = new SmsEvent($name, $payload['reference'], $payload);
72+
$event->setRecipientPhone($payload['numero']);
73+
74+
return $event;
75+
}
76+
}

0 commit comments

Comments
 (0)
0