-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Notifier] Add Orange SMS bridge #44884
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
Changes from 1 commit
4bc6ee7
d7f88de
dad3317
e65759e
e2f73f8
bba4192
666d342
174b8a9
e1ab434
ad7cc8d
fff38ec
66991ce
c222672
9b0658f
6deb236
a272b6d
c68be45
cc3adfb
1c3a4c8
090acd7
193a34c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.4.0 | ||
----- | ||
|
||
* Added the bridge | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2022 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?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\OrangeSms; | ||
|
||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Component\Notifier\Exception\LogicException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
final class OrangeSmsTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'https://api.orange.com'; | ||
|
||
private $clientID; | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private $clientSecret; | ||
private $from; | ||
private $senderName; | ||
|
||
public function __construct(string $clientID, string $clientSecret, string $from, ?string $senderName, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->clientID = $clientID; | ||
$this->clientSecret = $clientSecret; | ||
$this->from = $from; | ||
$this->senderName = $senderName; | ||
|
||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
public function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message))); | ||
} | ||
|
||
$url = $this->getEndpoint() . '/smsmessaging/v1/outbound/' . urlencode('tel:' . $this->from) . '/requests'; | ||
$headers = [ | ||
'Authorization' => 'Bearer ' . $this->getAccessToken(), | ||
'Content-Type' => 'application/json' | ||
]; | ||
|
||
$args = [ | ||
'outboundSMSMessageRequest' => [ | ||
'address' => 'tel:' . $message->getPhone(), | ||
'senderAddress' => 'tel:' . $this->from, | ||
'outboundSMSTextMessage' => [ | ||
'message' => $message->getSubject() | ||
] | ||
] | ||
]; | ||
|
||
if (null !== $this->senderName) { | ||
$args['outboundSMSMessageRequest']['senderName'] = urlencode($this->senderName); | ||
} | ||
|
||
$response = $this->client->request('POST', $url, [ | ||
'headers' => $headers, | ||
'json' => $args | ||
]); | ||
|
||
if (201 != $response->getStatusCode()) { | ||
$content = $response->toArray(false); | ||
$errorMessage = $content['requestError']['serviceException']['messageId'] ?? ''; | ||
$errorInfo = $content['requestError']['serviceException']['text'] ?? ''; | ||
|
||
throw new TransportException(sprintf('Unable to send the SMS: '.$errorMessage.' (%s).', $errorInfo), $response); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. concatenating the error message into the format string of |
||
} | ||
|
||
return new SentMessage($message, (string) $this); | ||
} | ||
|
||
public function getAccessToken() | ||
{ | ||
$url = self::HOST . '/oauth/v3/token'; | ||
$credentials = $this->clientID . ':' . $this->clientSecret; | ||
$headers = [ | ||
'Authorization' => 'Basic ' . base64_encode($credentials), | ||
'Content-Type' => 'application/x-www-form-urlencoded', | ||
'Accept' => 'application/json' | ||
]; | ||
$args = array('grant_type' => 'client_credentials'); | ||
|
||
$response = $this->client->request('POST', $url, [ | ||
'headers' => $headers, | ||
'body' => $args | ||
]); | ||
|
||
if (200 !== $response->getStatusCode()) { | ||
throw new TransportException('Get Access Token Failled', $response); | ||
} | ||
|
||
return $response->toArray()['access_token']; | ||
} | ||
|
||
public function __toString(): string | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return sprintf('orangesms://%s?from=%s', $this->getEndpoint(), $this->from); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?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\OrangeSms; | ||
|
||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Component\Notifier\Exception\IncompleteDsnException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
|
||
final class OrangeSmsTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return OrangeSmsTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('orangesms' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'orangesms', $this->getSupportedSchemes()); | ||
} | ||
|
||
$user = $this->getUser($dsn); | ||
$password = $this->getPassword($dsn); | ||
$from = $dsn->getOption('from'); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$senderName = $dsn->getOption('senderName'); | ||
|
||
if (!$from) { | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); | ||
} | ||
|
||
return new OrangeSmsTransport($user, $password, $from, $senderName, $this->client, $this->dispatcher); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['orangesms']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Orange Sms Notifier | ||
================ | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Provides [Orange Sms](https://developer.orange.com/apis/sms) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
ORANGE_SMS_DSN=orangesms://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME | ||
``` | ||
|
||
where: | ||
|
||
- `CLIENT_ID` is your Orange APP client ID | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `CLIENT_SECRET` is the Orange APP client secret | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- `FROM` is the sender phone number | ||
- `SENDER_NAME` is the sender name | ||
|
||
example: | ||
|
||
``` | ||
ORANGE_SMS_DSN=orangesms://RbttXve8o2y3IglAqJXlXTzZywyyjqKo:iNpfgVeHusPEKrrp@default?from=+243000000&sender_name=platformXYZ | ||
``` | ||
|
||
See Orange Sms documentation at https://developer.orange.com/apis/sms-cd/api-reference |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?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\Esendex\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\Esendex\OrangeSmsTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
|
||
final class OrangeSmsTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
/** | ||
* @return OrangeSmsTransportFactory | ||
*/ | ||
public function createFactory(): TransportFactoryInterface | ||
{ | ||
return new OrangeSmsTransportFactory(); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'orangesms://default?from=FROM&sender_name=SENDER_NAME', | ||
'orangesms://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME', | ||
]; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'orangesms://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME']; | ||
yield [false, 'somethingElse://CLIENT_ID:CLIENT_SECRET@default']; | ||
} | ||
|
||
public function incompleteDsnProvider(): iterable | ||
{ | ||
yield 'missing credentials' => ['orangesms://default?from=FROM&sender_name=SENDER_NAME']; | ||
yield 'missing Client ID' => ['orangesms://:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME']; | ||
yield 'missing client Secret' => ['orangesms://CLIENT_ID:@default?from=FROM&sender_name=SENDER_NAME']; | ||
} | ||
|
||
public function missingRequiredOptionProvider(): iterable | ||
{ | ||
yield 'missing option: from' => ['orangesms://CLIENT_ID:CLIENT_SECRET@default?sender_name=SENDER_NAME']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://CLIENT_ID:CLIENT_SECRET@default?from=FROM&sender_name=SENDER_NAME']; | ||
yield ['somethingElse://CLIENT_ID:CLIENT_SECRET@host?sender_name=SENDER_NAME']; // missing "from" option | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?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\Esendex\Tests; | ||
|
||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\Notifier\Bridge\Esendex\OrangeSmsTransport; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Message\ChatMessage; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Test\TransportTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
final class OrangeSmsTransportTest extends TransportTestCase | ||
{ | ||
/** | ||
* @return OrangeSmsTransport | ||
*/ | ||
public function createTransport(?HttpClientInterface $client = null): TransportInterface | ||
{ | ||
return (new OrangeSmsTransport('CLIENT_ID', 'CLIENT_SECRET', 'from', 'senderNname', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('default'); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['orangesms://default?from=FROM&sender_name=SENDER_NAME', $this->createTransport()]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test missing, where Sender name is not set |
||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('+243899999999', 'Hello World!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello World!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.