-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Notifier] Add LightSms notifier bridge #40607
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 34 commits
b075c0e
f2ba226
be8f994
167f325
6792535
2f65b92
15686c0
a0fae7d
d1ccd46
ce41756
728a3e2
4a11b94
febff46
9e1809e
3cbbc85
5e54dfe
9f89014
c02dbbd
b2e4638
49b4780
b0891be
1b073c2
e0a68bd
3d0d79c
0e41bc9
079406e
7b51e0d
2e0e1d7
66c34ba
8620e82
5d2e692
fc13bb2
b0e64b9
e20ef1e
7f13dbf
f16b4d2
7180c1f
08b0729
80ef5ba
08235e5
23a446a
265f776
b9f9ff8
58ac708
1b59a7d
1c993b7
1ff97e4
83d2598
a197dee
95e82f6
178d9c2
9a832ef
0d7488b
9b2e2d0
bea5256
21e972a
4213564
2a9ac2d
026dcd9
f1f83b9
68a12fa
37c665e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2021 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,171 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Notifier\Exception\TransportException; | ||
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; | ||
use Symfony\Component\Notifier\Message\MessageInterface; | ||
use Symfony\Component\Notifier\Message\SentMessage; | ||
use Symfony\Component\Notifier\Message\SmsMessage; | ||
use Symfony\Component\Notifier\Transport\AbstractTransport; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
/** | ||
* @author Vasilij Duško <vasilij@prado.lt> | ||
*/ | ||
final class LightSmsTransport extends AbstractTransport | ||
{ | ||
protected const HOST = 'www.lightsms.com'; | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private $login; | ||
private $password; | ||
private $phone; | ||
|
||
private const ERROR_CODES = [ | ||
'000' => 'Service unavailable', | ||
'1' => 'Missing Signature', | ||
'2' => 'Login not specified', | ||
'3' => 'Text not specified', | ||
'4' => 'Phone number not specified', | ||
'5' => 'Sender not specified', | ||
'6' => 'Invalid signature', | ||
'7' => 'Invalid login', | ||
'8' => 'Invalid sender name', | ||
'9' => 'Sender name not registered', | ||
'10' => 'Sender name not approved', | ||
'11' => 'There are forbidden words in the text', | ||
'12' => 'Error in SMS sending', | ||
'13' => 'Phone number is in the blackist. SMS sending to this number is forbidden.', | ||
'14' => 'There are more than 50 numbers in the request', | ||
'15' => 'List not specified', | ||
'16' => 'Invalid phone number', | ||
'17' => 'SMS ID not specified', | ||
'18' => 'Status not obtained', | ||
'19' => 'Empty response', | ||
'20' => 'The number already exists', | ||
'21' => 'No name', | ||
'22' => 'Template already exists', | ||
'23' => 'Missing Month (Format: YYYY-MM)', | ||
'24' => 'Timestamp not specified', | ||
'25' => 'Error in access to the list', | ||
'26' => 'There are no numbers in the list', | ||
'27' => 'No valid numbers', | ||
'28' => 'Missing start date (Format: YYYY-MM-DD)', | ||
'29' => 'Missing end date (Format: YYYY-MM-DD)', | ||
'30' => 'No date (format: YYYY-MM-DD)', | ||
'31' => 'Closing direction to the user', | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'32' => 'Not enough money', | ||
'33' => 'Missing phone number', | ||
'34' => 'Phone is in stop list', | ||
'35' => 'Not enough money', | ||
'36' => 'Can not obtain information about phone', | ||
'37' => 'Base Id is not set', | ||
'38' => 'Phone number already exists in this database', | ||
'39' => 'Phone number does not exist in this database', | ||
]; | ||
|
||
public function __construct(string $login, string $password, string $phone, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) | ||
{ | ||
$this->login = $login; | ||
$this->password = $password; | ||
$this->phone = $phone; | ||
|
||
parent::__construct($client, $dispatcher); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return sprintf('lightsms://%s?phone=%s', $this->getEndpoint(), $this->phone); | ||
} | ||
|
||
public function supports(MessageInterface $message): bool | ||
{ | ||
return $message instanceof SmsMessage; | ||
} | ||
|
||
protected function doSend(MessageInterface $message): SentMessage | ||
{ | ||
if (!$message instanceof SmsMessage) { | ||
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); | ||
} | ||
|
||
$timestamp = time(); | ||
|
||
$signature = $this->generateSignature([ | ||
'message' => $message, | ||
'timestamp' => $timestamp, | ||
]); | ||
|
||
$endpoint = sprintf( | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'https://%s/external/get/send.php?login=%s&signature=%s&phone=%s&text=%s&sender=%s×tamp=%s', | ||
$this->getEndpoint(), | ||
$this->login, | ||
$signature, | ||
$this->escapePhoneNumber($message->getPhone()), | ||
$this->escapeSubject($message->getSubject()), | ||
$this->phone, | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$timestamp | ||
); | ||
|
||
$response = $this->client->request('GET', $endpoint); | ||
|
||
if (Response::HTTP_OK !== $response->getStatusCode()) { | ||
throw new TransportException('Unable to send the SMS: ', $response); | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
$content = $response->toArray(false); | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (isset($content['error'])) { | ||
throw new TransportException('Unable to send the SMS: '.self::ERROR_CODES[$content['error']], $response); | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
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. what if the error code is not define in 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. It will throw undefined error 🙄 how than validate it 🤔 yes possible they can send another error code or change documentation changes. Maybe put if not exists like as unknown error (add new one number) 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. Success json
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. Failure json
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. Their uses in different cases different response. How we can manage than 🙄 use (int) before error 🙄 Response for all queries HTTP OK (200) 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. Added (int) before return error. On success their return like string. On error their return integer. |
||
} | ||
|
||
$phone = $this->escapePhoneNumber($message->getPhone()); | ||
if (32 === $content[$phone]['error']) { | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new TransportException('Unable to send the SMS: '.self::ERROR_CODES[$content['error']], $response); | ||
} | ||
|
||
if (0 == $content[$phone]['error']) { | ||
$sentMessage = new SentMessage($message, (string) $this); | ||
$sentMessage->setMessageId($content[$phone]['id_sms']); | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return $sentMessage; | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private function generateSignature(array $params): string | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
$params = [ | ||
'timestamp' => $params['timestamp'], | ||
'login' => $this->login, | ||
'phone' => $this->escapePhoneNumber($params['message']->getPhone()), | ||
'sender' => $this->phone, | ||
'text' => $this->escapeSubject($params['message']->getSubject()), | ||
]; | ||
|
||
ksort($params); | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reset($params); | ||
|
||
return md5(implode('', $params).$this->password); | ||
} | ||
|
||
private function escapeSubject($subject): string | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return strip_tags($subject); | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private function escapePhoneNumber($phoneNumber): string | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return str_replace('+', '00', $phoneNumber); | ||
} | ||
} |
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 <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms; | ||
|
||
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; | ||
use Symfony\Component\Notifier\Transport\AbstractTransportFactory; | ||
use Symfony\Component\Notifier\Transport\Dsn; | ||
use Symfony\Component\Notifier\Transport\TransportInterface; | ||
|
||
/** | ||
* @author Vasilij Duško <vasilij@prado.lt> | ||
*/ | ||
final class LightSmsTransportFactory extends AbstractTransportFactory | ||
{ | ||
/** | ||
* @return LightSmsTransport | ||
*/ | ||
public function create(Dsn $dsn): TransportInterface | ||
{ | ||
$scheme = $dsn->getScheme(); | ||
|
||
if ('lightsms' !== $scheme) { | ||
throw new UnsupportedSchemeException($dsn, 'lightsms', $this->getSupportedSchemes()); | ||
} | ||
|
||
$login = $this->getUser($dsn); | ||
$token = $this->getPassword($dsn); | ||
$phone = $dsn->getRequiredOption('phone'); | ||
|
||
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); | ||
$port = $dsn->getPort(); | ||
|
||
return (new LightSmsTransport($login, $token, $phone, $this->client, $this->dispatcher))->setHost($host)->setPort($port); | ||
} | ||
|
||
protected function getSupportedSchemes(): array | ||
{ | ||
return ['lightsms']; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
LightSms Notifier | ||
================= | ||
|
||
Provides [LightSms](https://www.lightsms.com/) integration for Symfony Notifier. | ||
|
||
DSN example | ||
----------- | ||
|
||
``` | ||
LIGHTSMS_DSN=lightsms://LOGIN:TOKEN@default?phone=PHONE | ||
``` | ||
|
||
where: | ||
- `LOGIN` is your LightSms login | ||
- `TOKEN` is the token displayed in your account | ||
- `PHONE` is your LightSms sender phone number | ||
|
||
See your account info at https://www.lightsms.com/external/client/api/ | ||
|
||
Resources | ||
--------- | ||
|
||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransportFactory; | ||
use Symfony\Component\Notifier\Test\TransportFactoryTestCase; | ||
use Symfony\Component\Notifier\Transport\TransportFactoryInterface; | ||
|
||
final class LightSmsTransportFactoryTest extends TransportFactoryTestCase | ||
{ | ||
/** | ||
* @return LightSmsTransportFactory | ||
*/ | ||
public function createFactory(): TransportFactoryInterface | ||
{ | ||
return new LightSmsTransportFactory(); | ||
} | ||
|
||
public function createProvider(): iterable | ||
{ | ||
yield [ | ||
'lightsms://host.test?phone=0611223344', | ||
'lightsms://login:token@host.test?phone=0611223344', | ||
]; | ||
} | ||
|
||
public function supportsProvider(): iterable | ||
{ | ||
yield [true, 'lightsms://login:token@default?phone=37061234567']; | ||
yield [false, 'somethingElse://login:token@default?phone=37061234567']; | ||
} | ||
|
||
public function unsupportedSchemeProvider(): iterable | ||
{ | ||
yield ['somethingElse://accountSid:authToken@default?phone=37061234567']; | ||
yield ['somethingElse://accountSid:authToken@default']; // missing "phone" option | ||
StaffNowa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Notifier\Bridge\LightSms\Tests; | ||
|
||
use Symfony\Component\Notifier\Bridge\LightSms\LightSmsTransport; | ||
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; | ||
|
||
final class LightSmsTransportTest extends TransportTestCase | ||
{ | ||
/** | ||
* @return LightSmsTransport | ||
*/ | ||
public function createTransport(?HttpClientInterface $client = null): TransportInterface | ||
{ | ||
return new LightSmsTransport('accountSid', 'authToken', 'from', $client ?: $this->createMock(HttpClientInterface::class)); | ||
} | ||
|
||
public function toStringProvider(): iterable | ||
{ | ||
yield ['lightsms://www.lightsms.com/external/get/send.php?phone=from', $this->createTransport()]; | ||
} | ||
|
||
public function supportedMessagesProvider(): iterable | ||
{ | ||
yield [new SmsMessage('0611223344', 'Hello!')]; | ||
} | ||
|
||
public function unsupportedMessagesProvider(): iterable | ||
{ | ||
yield [new ChatMessage('Hello!')]; | ||
yield [$this->createMock(MessageInterface::class)]; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.