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

Skip to content

Commit 96dfc56

Browse files
author
alanzarli
committed
[Notifier][Webhook] Add Smsbox support
1 parent 8c941de commit 96dfc56

File tree

8 files changed

+122
-3
lines changed

8 files changed

+122
-3
lines changed

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

+1
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

+4
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/Bundle/SecurityBundle/Tests/Functional/AccessTokenTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public function testCasSuccess()
425425
$this->assertSame(['message' => 'Welcome @dunglas!'], json_decode($response->getContent(), true));
426426
}
427427

428-
public function validAccessTokens(): array
428+
public static function validAccessTokens(): array
429429
{
430430
if (!\extension_loaded('openssl')) {
431431
return [];
@@ -440,8 +440,8 @@ public function validAccessTokens(): array
440440
'sub' => 'e21bf182-1538-406e-8ccb-e25a17aba39f',
441441
'username' => 'dunglas',
442442
];
443-
$jws = $this->createJws($claims);
444-
$jwe = $this->createJwe($jws);
443+
$jws = self::createJws($claims);
444+
$jwe = self::createJwe($jws);
445445

446446
return [
447447
[$jws],

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

+1
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. 🚀
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__))), < B41A span class=pl-s1>$payload);
6+
$wh = new SmsEvent(SmsEvent::DELIVERED, '250207960297', $payload);
7+
$wh->setRecipientPhone('33612346578');
8+
9+
return $wh;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
numero=33612346578&reference=250207960297&accuse=0&ts=1737368770
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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\HttpFoundation\ChainRequestMatcher;
18+
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
19+
use Symfony\Component\Webhook\Client\AbstractRequestParser;
20+
use Symfony\Component\Webhook\Exception\RejectWebhookException;
21+
22+
23+
final class SmsboxRequestParser extends AbstractRequestParser
24+
{
25+
protected function getRequestMatcher(): RequestMatcherInterface
26+
{
27+
return new MethodRequestMatcher(['GET']);
28+
}
29+
30+
protected function doParse(Request $request, #[\SensitiveParameter] string $secret): ?SmsEvent
31+
{
32+
$payload = $request->query->all();
33+
34+
if (
35+
!isset($payload['numero'])
36+
|| !isset($payload['reference'])
37+
|| !isset($payload['accuse'])
38+
|| !isset($payload['ts'])
39+
) {
40+
throw new RejectWebhookException(406, 'Payload is malformed.');
41+
}
42+
43+
$name = match ($payload['accuse']) {
44+
// Documentation for SMSBOX dlr code https://www.smsbox.net/en/tools-development#doc-sms-accusees
45+
'-3' => SmsEvent::FAILED,
46+
'-1' => null,
47+
'0' => SmsEvent::DELIVERED,
48+
'1' => SmsEvent::FAILED,
49+
'2' => SmsEvent::FAILED,
50+
'3' => SmsEvent::FAILED,
51+
'4' => SmsEvent::FAILED,
52+
'5' => SmsEvent::FAILED,
53+
'6' => SmsEvent::FAILED,
54+
'7' => SmsEvent::FAILED,
55+
'8' => SmsEvent::FAILED,
56+
'9' => null,
57+
'10' => null,
58+
default => throw new RejectWebhookException(406, \sprintf('Unknown status: %s', $payload['accuse'])),
59+
};
60+
61+
$event = new SmsEvent($name, $payload['reference'], $payload);
62+
$event->setRecipientPhone($payload['numero']);
63+
64+
return $event;
65+
}
66+
}

0 commit comments

Comments
 (0)
0