Skip to content

Commit 48269b5

Browse files
committed
Added telnyx notifier bridge
1 parent 268b5b7 commit 48269b5

File tree

13 files changed

+379
-0
lines changed

13 files changed

+379
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
119119
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
120120
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
121+
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
121122
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
122123
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
123124
use Symfony\Component\Notifier\Notifier;
@@ -2227,6 +2228,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22272228
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
22282229
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
22292230
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
2231+
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
22302232
];
22312233

22322234
foreach ($classToServices as $class => $service) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
2929
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
3030
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
31+
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
3132
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
3233
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
3334
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
@@ -115,6 +116,10 @@
115116
->parent('notifier.transport_factory.abstract')
116117
->tag('chatter.transport_factory')
117118

119+
->set('notifier.transport_factory.telnyx', TelnyxTransportFactory::class)
120+
->parent('notifier.transport_factory.abstract')
121+
->tag('texter.transport_factory')
122+
118123
->set('notifier.transport_factory.null', NullTransportFactory::class)
119124
->parent('notifier.transport_factory.abstract')
120125
->tag('chatter.transport_factory')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Telnyx Notifier
2+
===============
3+
4+
Provides Telnyx integration for Symfony Notifier.
5+
6+
Resources
7+
---------
8+
9+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
10+
* [Report issues](https://github.com/symfony/symfony/issues) and
11+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
12+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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\Telnyx;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Mihail Krasilnikov <mihail.krasilnikov.j@gmail.com>
18+
*
19+
* @experimental in 5.3
20+
*
21+
* @see https://developers.telnyx.com/docs/api/v2/messaging/Messages#createMessage
22+
*/
23+
final class TelnyxOptions implements MessageOptionsInterface
24+
{
25+
/**
26+
* @var array
27+
*/
28+
private $options;
29+
30+
public function __construct(array $options = [])
31+
{
32+
$this->options = $options;
33+
}
34+
35+
public function toArray(): array
36+
{
37+
return $this->options;
38+
}
39+
40+
public function getRecipientId(): ?string
41+
{
42+
return $this->options['to'] ?? null;
43+
}
44+
45+
public function to(string $number): self
46+
{
47+
$this->options['to'] = $number;
48+
49+
return $this;
50+
}
51+
52+
public function from(string $from): self
53+
{
54+
$this->options['from'] = $from;
55+
56+
return $this;
57+
}
58+
59+
public function mediaUrls(array $mediaUrls): self
60+
{
61+
$this->options['media_urls'] = $mediaUrls;
62+
63+
return $this;
64+
}
65+
66+
public function messagingProfileId(string $messagingProfileId): self
67+
{
68+
$this->options['messaging_profile_id'] = $messagingProfileId;
69+
70+
return $this;
71+
}
72+
73+
public function subject(string $subject): self
74+
{
75+
$this->options['subject'] = $subject;
76+
77+
return $this;
78+
}
79+
80+
public function text(string $text): self
81+
{
82+
$this->options['text'] = $text;
83+
84+
return $this;
85+
}
86+
87+
public function type(string $type): self
88+
{
89+
$this->options['type'] = $type;
90+
91+
return $this;
92+
}
93+
94+
public function useProfileWebhooks(bool $useProfileWebhooks): self
95+
{
96+
$this->options['use_profile_webhooks'] = $useProfileWebhooks;
97+
98+
return $this;
99+
}
100+
101+
public function webhookFailoverUrl(string $webhookFailoverUrl): self
102+
{
103+
$this->options['webhook_failover_url'] = $webhookFailoverUrl;
104+
105+
return $this;
106+
}
107+
108+
public function webhookUrll(string $webhookUrl): self
109+
{
110+
$this->options['webhook_url'] = $webhookUrl;
111+
112+
return $this;
113+
}
114+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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\Telnyx;
13+
14+
use Symfony\Component\Notifier\Exception\LogicException;
15+
use Symfony\Component\Notifier\Exception\TransportException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\SentMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Mihail Krasilnikov <mihail.krasilnikov.j@gmail.com>
25+
*
26+
* @experimental in 5.3
27+
*/
28+
final class TelnyxTransport extends AbstractTransport
29+
{
30+
protected const HOST = 'api.telnyx.com';
31+
32+
private $apiKey;
33+
private $from;
34+
35+
public function __construct(string $apiKey, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
36+
{
37+
$this->apiKey = $apiKey;
38+
$this->from = $from;
39+
40+
parent::__construct($client, $dispatcher);
41+
}
42+
43+
public function __toString(): string
44+
{
45+
return sprintf('telnyx://%s?from=%s', $this->apiKey, $this->from);
46+
}
47+
48+
public function supports(MessageInterface $message): bool
49+
{
50+
return $message instanceof SmsMessage;
51+
}
52+
53+
/**
54+
* @see https://developers.telnyx.com/docs/api/v2/messaging/Messages
55+
*/
56+
protected function doSend(MessageInterface $message): SentMessage
57+
{
58+
if (!$message instanceof SmsMessage) {
59+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, SmsMessage::class, get_debug_type($message)));
60+
}
61+
62+
if ($message->getOptions() && !$message->getOptions() instanceof TelnyxOptions) {
63+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, TelnyxOptions::class));
64+
}
65+
66+
$endpoint = sprintf('https://%s/v2/messages', $this->getEndpoint());
67+
68+
$response = $this->client->request('POST', $endpoint, [
69+
'auth_bearer' => $this->apiKey,
70+
'json' => $this->preparePayload($message),
71+
]);
72+
73+
if (200 !== $response->getStatusCode()) {
74+
$error = $response->toArray(false);
75+
76+
throw new TransportException("Unable to send the SMS: {$error['title']}.", $response);
77+
}
78+
79+
return new SentMessage($message, (string) $this);
80+
}
81+
82+
private function preparePayload(SmsMessage $message): array
83+
{
84+
$options = $message->getOptions() ? $message->getOptions()->toArray() : [];
85+
86+
if (!isset($options['from'])) {
87+
$options['from'] = $this->from;
88+
}
89+
90+
if (!isset($options['to'])) {
91+
$options['to'] = $message->getPhone();
92+
}
93+
94+
if (!isset($options['text'])) {
95+
$options['text'] = $message->getSubject();
96+
}
97+
98+
return $options;
99+
}
100+
}
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 <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\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 Mihail Krasilnikov <mihail.krasilnikov.j@gmail.com>
21+
*
22+
* @experimental in 5.3
23+
*/
24+
final class TelnyxTransportFactory extends AbstractTransportFactory
25+
{
26+
/**
27+
* @return TelnyxTransport
28+
*/
29+
public function create(Dsn $dsn): TransportInterface
30+
{
31+
$scheme = $dsn->getScheme();
32+
$apiKey = $dsn->getUser();
33+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
34+
$from = $dsn->getOption('from');
35+
$port = $dsn->getPort();
36+
37+
if ('telnyx' === $scheme) {
38+
return (new TelnyxTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
39+
}
40+
41+
throw new UnsupportedSchemeException($dsn, 'telnyx', $this->getSupportedSchemes());
42+
}
43+
44+
protected function getSupportedSchemes(): array
45+
{
46+
return ['telnyx'];
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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": "Fabien Potencier",
11+
"email": "fabien@symfony.com"
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.3|^5.0",
21+
"symfony/notifier": "^5.2"
22+
},
23+
"autoload": {
24+
"psr-4": {
25+
"Symfony\\Component\\Notifier\\Bridge\\Telnyx\\": ""
26+
},
27+
"exclude-from-classmap": [
28+
"/Tests/"
29+
]
30+
},
31+
"minimum-stability": "dev"
32+
}

0 commit comments

Comments
 (0)