Skip to content

Commit 2fb272e

Browse files
committed
feature #41172 [Notifier] Add Telnyx notifier bridge (StaffNowa)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Notifier] Add Telnyx notifier bridge | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#15377 | Recipe PR | symfony/recipes#951 Telnyx notifier https://developers.telnyx.com/docs/v2/messaging/quickstarts/sending-sms-and-mms Commits ------- f8a4d42 [Notifier] Add Telnyx notifier bridge
2 parents 3aaa491 + f8a4d42 commit 2fb272e

File tree

15 files changed

+389
-0
lines changed

15 files changed

+389
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use Symfony\Component\Notifier\Bridge\SmsBiuras\SmsBiurasTransportFactory;
4343
use Symfony\Component\Notifier\Bridge\SpotHit\SpotHitTransportFactory;
4444
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
45+
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
4546
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
4647
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
4748
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
@@ -189,5 +190,9 @@
189190
->set('notifier.transport_factory.messagebird', MessageBirdTransportFactory::class)
190191
->parent('notifier.transport_factory.abstract')
191192
->tag('texter.transport_factory')
193+
194+
->set('notifier.transport_factory.telnyx', TelnyxTransportFactory::class)
195+
->parent('notifier.transport_factory.abstract')
196+
->tag('texter.transport_factory')
192197
;
193198
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.4
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Telnyx Notifier
2+
===============
3+
4+
Provides [Telnyx](https://telnyx.com/) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
TELNYX_DSN=telnyx://API_KEY@default?from=FROM&messaging_profile_id=MESSAGING_PROFILE_ID
11+
```
12+
13+
where:
14+
- `API_KEY` is your telnyx API key.
15+
- `FROM` is your sender.
16+
- `MESSAGING_PROFILE_ID` identifier of your messaging profile at Telnyx. You need this in order to show a name to the recipient (e.g. "Symfony") instead of just the phone number.
17+
18+
Resources
19+
---------
20+
21+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
22+
* [Report issues](https://github.com/symfony/symfony/issues) and
23+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
24+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Telnyx;
13+
14+
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Message\SmsMessage;
20+
use Symfony\Component\Notifier\Transport\AbstractTransport;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author Vasilij Duško <[email protected]>
26+
*/
27+
final class TelnyxTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.telnyx.com';
30+
31+
private $apiKey;
32+
private $from;
33+
private $messagingProfileId;
34+
35+
public function __construct(string $apiKey, string $from, ?string $messagingProfileId, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->apiKey = $apiKey;
38+
$this->from = $from;
39+
$this->messagingProfileId = $messagingProfileId;
40+
41+
parent::__construct($client, $dispatcher);
42+
}
43+
44+
public function __toString(): string
45+
{
46+
if (null !== $this->messagingProfileId) {
47+
return sprintf('telnyx://%s?from=%s&messaging_profile_id=%s', $this->getEndpoint(), $this->from, $this->messagingProfileId);
48+
}
49+
50+
return sprintf('telnyx://%s?from=%s', $this->getEndpoint(), $this->from);
51+
}
52+
53+
public function supports(MessageInterface $message): bool
54+
{
55+
return $message instanceof SmsMessage;
56+
}
57+
58+
protected function doSend(MessageInterface $message): SentMessage
59+
{
60+
if (!$message instanceof SmsMessage) {
61+
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
62+
}
63+
64+
if (!preg_match('/^[+]+[1-9][0-9]{9,14}$/', $this->from)) {
65+
if ('' === $this->from) {
66+
throw new IncompleteDsnException('This phone number is invalid.');
67+
} elseif ('' !== $this->from && null === $this->messagingProfileId) {
68+
throw new IncompleteDsnException('The sending messaging profile must be specified.');
69+
}
70+
71+
if (!preg_match('/^[a-zA-Z0-9 ]+$/', $this->from)) {
72+
throw new IncompleteDsnException('The Sender ID is invalid.');
73+
}
74+
}
75+
76+
$endpoint = sprintf('https://%s/v2/messages', $this->getEndpoint());
77+
$response = $this->client->request('POST', $endpoint, [
78+
'auth_bearer' => $this->apiKey,
79+
'json' => [
80+
'from' => $this->from,
81+
'to' => $message->getPhone(),
82+
'text' => $message->getSubject(),
83+
'messaging_profile_id' => $this->messagingProfileId ?? '',
84+
],
85+
]);
86+
87+
if (200 !== $response->getStatusCode()) {
88+
$error = $response->toArray(false);
89+
if (!isset($error['errors'])) {
90+
throw new TransportException('Unable to send the SMS.', $response);
91+
}
92+
93+
throw new TransportException('Unable to send the SMS: '.$error['errors'][0]['detail'] ?? 'Unknown reason', $response);
94+
}
95+
96+
$success = $response->toArray(false);
97+
98+
$sentMessage = new SentMessage($message, (string) $this);
99+
if (isset($success['data']['id'])) {
100+
$sentMessage->setMessageId($success['data']['id']);
101+
}
102+
103+
return $sentMessage;
104+
}
105+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Telnyx;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author Vasilij Duško <[email protected]>
21+
*/
22+
final class TelnyxTransportFactory extends AbstractTransportFactory
23+
{
24+
/**
25+
* @return TelnyxTransport
26+
*/
27+
public function create(Dsn $dsn): TransportInterface
28+
{
29+
$scheme = $dsn->getScheme();
30+
31+
if ('telnyx' !== $scheme) {
32+
throw new UnsupportedSchemeException($dsn, 'telnyx', $this->getSupportedSchemes());
33+
}
34+
35+
$apiKey = $this->getUser($dsn);
36+
$from = $dsn->getRequiredOption('from');
37+
$messagingProfileId = $dsn->getOption('messaging_profile_id');
38+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
39+
$port = $dsn->getPort();
40+
41+
return (new TelnyxTransport($apiKey, $from, $messagingProfileId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return ['telnyx'];
47+
}
48+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Telnyx\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
15+
use Symfony\Component\Notifier\Test\TransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Transport\TransportFactoryInterface;
17+
18+
final class TelnyxTransportFactoryTest extends TransportFactoryTestCase
19+
{
20+
/**
21+
* @return TelnyxTransportFactory
22+
*/
23+
public function createFactory(): TransportFactoryInterface
24+
{
25+
return new TelnyxTransportFactory();
26+
}
27+
28+
public function createProvider(): iterable
29+
{
30+
yield [
31+
'telnyx://host.test?from=+0611223344',
32+
'telnyx://[email protected]?from=%2B0611223344',
33+
];
34+
35+
yield [
36+
'telnyx://host.test?from=+0611223344&messaging_profile_id=messaging_profile_id',
37+
'telnyx://[email protected]?from=%2B0611223344&messaging_profile_id=messaging_profile_id',
38+
];
39+
}
40+
41+
public function supportsProvider(): iterable
42+
{
43+
yield [true, 'telnyx://api_key@default?from=%2B0611223344'];
44+
yield [false, 'somethingElse://api_key@default?from=0611223344'];
45+
}
46+
47+
public function missingRequiredOptionProvider(): iterable
48+
{
49+
yield 'missing option: from' => ['telnyx://api_key@default'];
50+
}
51+
52+
public function unsupportedSchemeProvider(): iterable
53+
{
54+
yield ['somethingElse://api_key@default?from=+0611223344'];
55+
yield ['somethingElse://api_key@default']; // missing "from" option
56+
}
57+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Telnyx\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransport;
15+
use Symfony\Component\Notifier\Message\ChatMessage;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SmsMessage;
18+
use Symfony\Component\Notifier\Test\TransportTestCase;
19+
use Symfony\Component\Notifier\Transport\TransportInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
final class TelnyxTransportTest extends TransportTestCase
23+
{
24+
/**
25+
* @return TelnyxTransport
26+
*/
27+
public function createTransport(?HttpClientInterface $client = null): TransportInterface
28+
{
29+
return new TelnyxTransport('api_key', 'from', 'messaging_profile_id', $client ?? $this->createMock(HttpClientInterface::class));
30+
}
31+
32+
public function toStringProvider(): iterable
33+
{
34+
yield ['telnyx://api.telnyx.com?from=from&messaging_profile_id=messaging_profile_id', $this->createTransport()];
35+
}
36+
37+
public function supportedMessagesProvider(): iterable
38+
{
39+
yield [new SmsMessage('+0611223344', 'Hello!')];
40+
}
41+
42+
public function unsupportedMessagesProvider(): iterable
43+
{
44+
yield [new ChatMessage('Hello!')];
45+
yield [$this->createMock(MessageInterface::class)];
46+
}
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "symfony/telnyx-notifier",
3+
"type": "symfony-bridge",
4+
"description": "Symfony Telnyx Notifier Bridge",
5+
"keywords": ["sms", "telnyx", "notifier"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Vasilij Duško",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.2.5",
20+
"symfony/http-client": "^4.4|^5.2",
21+
"symfony/notifier": "^5.3"
22+
},
23+
"autoload": {
24+
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Telnyx\\": "" },
25+
"exclude-from-classmap": [
26+
"/Tests/"
27+
]
28+
},
29+
"minimum-stability": "dev"
30+
}

0 commit comments

Comments
 (0)