Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Notifier][Smsbox] Add webhook support #59570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3117,6 +3117,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
$loader->load('notifier_webhook.php');

$webhookRequestParsers = [
NotifierBridge\Smsbox\Webhook\SmsboxRequestParser::class => 'notifier.webhook.request_parser.smsbox',
NotifierBridge\Sweego\Webhook\SweegoRequestParser::class => 'notifier.webhook.request_parser.sweego',
NotifierBridge\Twilio\Webhook\TwilioRequestParser::class => 'notifier.webhook.request_parser.twilio',
NotifierBridge\Vonage\Webhook\VonageRequestParser::class => 'notifier.webhook.request_parser.vonage',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Notifier\Bridge\Smsbox\Webhook\SmsboxRequestParser;
use Symfony\Component\Notifier\Bridge\Sweego\Webhook\SweegoRequestParser;
use Symfony\Component\Notifier\Bridge\Twilio\Webhook\TwilioRequestParser;
use Symfony\Component\Notifier\Bridge\Vonage\Webhook\VonageRequestParser;

return static function (ContainerConfigurator $container) {
$container->services()
->set('notifier.webhook.request_parser.smsbox', SmsboxRequestParser::class)
->alias(SmsboxRequestParser::class, 'notifier.webhook.request_parser.smsbox')

->set('notifier.webhook.request_parser.sweego', SweegoRequestParser::class)
->alias(SweegoRequestParser::class, 'notifier.webhook.request_parser.sweego')

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Notifier/Bridge/Smsbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ $options = (new SmsboxOptions())
$sms->options($options);
$texter->send($sms);
```
## Smsbox notifier also provides Webhooks support. 🚀
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;

parse_str(trim(file_get_contents(str_replace('.php', '.txt', __FILE__))), $payload);
$wh = new SmsEvent(SmsEvent::DELIVERED, '250207960297', $payload);
$wh->setRecipientPhone('33612346578');

return $wh;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
numero=33612346578&reference=250207960297&accuse=0&ts=1737368770
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Smsbox\Tests\Webhook;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Notifier\Bridge\Smsbox\Webhook\SmsboxRequestParser;
use Symfony\Component\Webhook\Client\RequestParserInterface;
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase;

class SmsboxRequestParserTest extends AbstractRequestParserTestCase
{
protected function createRequestParser(): RequestParserInterface
{
return new SmsboxRequestParser();
}

protected function createRequest(string $payload): Request
{
parse_str(trim($payload), $parameters);

return Request::create('/', 'GET', $parameters, [], [], ['REMOTE_ADDR' => '37.59.198.135']);
}

protected static function getFixtureExtension(): string
{
return 'txt';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Smsbox\Webhook;

use Symfony\Component\HttpFoundation\ChainRequestMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher\IpsRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
use Symfony\Component\Webhook\Client\AbstractRequestParser;
use Symfony\Component\Webhook\Exception\RejectWebhookException;

final class SmsboxRequestParser extends AbstractRequestParser
{
protected function getRequestMatcher(): RequestMatcherInterface
{
return new ChainRequestMatcher([
new MethodRequestMatcher(['GET']),
new IpsRequestMatcher([
'37.59.198.135',
'178.33.185.51',
'54.36.93.79',
'54.36.93.80',
'62.4.31.47',
'62.4.31.48',
]),
]);
}

protected function doParse(Request $request, #[\SensitiveParameter] string $secret): ?SmsEvent
{
$payload = $request->query->all();

if (
!isset($payload['numero'])
|| !isset($payload['reference'])
|| !isset($payload['accuse'])
|| !isset($payload['ts'])
) {
throw new RejectWebhookException(406, 'Payload is malformed.');
}

$name = match ($payload['accuse']) {
// Documentation for SMSBOX dlr code https://www.smsbox.net/en/tools-development#doc-sms-accusees
'-3' => SmsEvent::FAILED,
'-1' => null,
'0' => SmsEvent::DELIVERED,
'1' => SmsEvent::FAILED,
'2' => SmsEvent::FAILED,
'3' => SmsEvent::FAILED,
'4' => SmsEvent::FAILED,
'5' => SmsEvent::FAILED,
'6' => SmsEvent::FAILED,
'7' => SmsEvent::FAILED,
'8' => SmsEvent::FAILED,
'9' => null,
'10' => null,
default => throw new RejectWebhookException(406, \sprintf('Unknown status: %s', $payload['accuse'])),
};

$event = new SmsEvent($name, $payload['reference'], $payload);
$event->setRecipientPhone($payload['numero']);

return $event;
}
}
3 changes: 3 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Smsbox/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"symfony/notifier": "^7.2",
"symfony/polyfill-php83": "^1.28"
},
"require-dev": {
"symfony/webhook": "^6.4|^7.0|^7.2"
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Notifier\\Bridge\\Smsbox\\": ""
Expand Down
Loading