8000 [Notifier] Added telnyx notifier bridge by krasilnikovs · Pull Request #38909 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Notifier] Added telnyx notifier bridge #38909

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension 8000


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 @@ -127,6 +127,7 @@
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
use Symfony\Component\Notifier\Notifier;
Expand Down Expand Up @@ -2249,6 +2250,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
GitterTransportFactory::class => 'notifier.transport_factory.gitter',
TelnyxTransportFactory::class => 'notifier.transport_factory.telnyx',
];

foreach ($classToServices as $class => $service) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
use Symfony\Component\Notifier\Bridge\Telnyx\TelnyxTransportFactory;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
Expand Down Expand Up @@ -145,6 +146,10 @@
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.telnyx', TelnyxTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.null', NullTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telnyx/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
7 changes: 7 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telnyx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telnyx/LICENSE
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.
22 changes: 22 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telnyx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Telnyx Notifier
===============

Provides [Telnyx](https://telnyx.com) integration for Symfony Notifier.

DSN example
-----------

```
TELNYX_DSN=telnyx://API_KEY@default?from=FROM
```

where:
- `API_KEY` is your telnyx api key
- `FROM` is your telnyx phone number

---------

* [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)
109 changes: 109 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telnyx/TelnyxOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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\Telnyx;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author Mihail Krasilnikov <mihail.krasilnikov.j@gmail.com>
*
* @see https://developers.telnyx.com/docs/api/v2/messaging/Messages#createMessage
*/
final class TelnyxOptions implements MessageOptionsInterface
{
private $options;

public function __construct(array $options = [])
{
$this->options = $options;
}

public function toArray(): array
{
return $this->options;
}

public function getRecipientId(): ?string
{
return $this->options['to'] ?? null;
}

public function to(string $number): self
{
$this->options['to'] = $number;

return $this;
}

public function from(string $from): self
{
$this->options['from'] = $from;

return $this;
}

public function mediaUrls(array $mediaUrls): self
{
$this->options['media_urls'] = $mediaUrls;

return $this;
}

public function messagingProfileId(string $messagingProfileId): self
{
$this->options['messaging_profile_id'] = $messagingProfileId;

return $this;
}

public function subject(string $subject): self
{
$this->options['subject'] = $subject;

return $this;
}

public function text(string $text): self
{
$this->options['text'] = $text;

return $this;
}

public function type(string $type): self
{
$this->options['type'] = $type;

return $this;
}

public function useProfileWebhooks(bool $useProfileWebhooks): self
{
$this->options['use_profile_webhooks'] = $useProfileWebhooks;

return $this;
}

public function webhookFailoverUrl(string $webhookFailoverUrl): self
{
$this->options['webhook_failover_url'] = $webhookFailoverUrl;

return $this;
}

public function webhookUrll(string $webhookUrl): self
{
$this->options['webhook_url'] = $webhookUrl;

return $this;
}
}
104 changes: 104 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telnyx/TelnyxTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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\Telnyx;

use Symfony\Component\Notifier\Exception\LogicException;
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 Mihail Krasilnikov <mihail.krasilnikov.j@gmail.com>
*/
final class TelnyxTransport extends AbstractTransport
{
protected const HOST = 'api.telnyx.com';

private $apiKey;
private $from;

public function __construct(string $apiKey, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
{
$this->apiKey = $apiKey;
$this->from = $from;

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('telnyx://%s?from=%s', $this->getEndpoint(), $this->from);
}

public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage;
}

/**
* @see https://developers.telnyx.com/docs/api/v2/messaging/Messages
*/
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}

if ($message->getOptions() && !$message->getOptions() instanceof TelnyxOptions) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, TelnyxOptions::class));
}

$endpoint = sprintf('https://%s/v2/messages', $this->getEndpoint());

$response = $this->client->request('POST', $endpoint, [
'auth_bearer' => $this->apiKey,
'json' => $this->preparePayload($message),
]);

if (200 !== $response->getStatusCode()) {
$error = $response->toArray(false);

throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['errors'][0]['title']), $response);
}

$success = $response->toArray(false);

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($success['data']['id']);

return $sentMessage;
}

private function preparePayload(SmsMessage $message): array
{
$options = $message->getOptions() ? $message->getOptions()->toArray() : [];

if (!isset($options['from'])) {
$options['from'] = $this->from;
}

if (!isset($options['to'])) {
$options['to'] = $message->getPhone();
}

if (!isset($options['text'])) {
$options['text'] = $message->getSubject();
}

return $options;
}
}
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\Telnyx;

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 Mihail Krasilnikov <mihail.krasilnikov.j@gmail.com>
*/
final class TelnyxTransportFactory extends AbstractTransportFactory
{
/**
* @return TelnyxTransport
*/
public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pull up the scheme check like here, to first check if the scheme is supported and afterwards for missing options. Thanks

public function create(Dsn $dsn): TransportInterface
{
$scheme = $dsn->getScheme();
if ('sinch' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'sinch', $this->getSupportedSchemes());
}
$accountSid = $this->getUser($dsn);
$authToken = $this->getPassword($dsn);
$from = $dsn->getOption('from');
if (!$from) {
throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn());
}
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
return (new SinchTransport($accountSid, $authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


if ('telnyx' !== $scheme) {
throw new UnsupportedSchemeException($dsn, 'telnyx', $this->getSupportedSchemes());
}

$apiKey = $this->getUser($dsn);
$from = $dsn->getRequiredOption('from');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();

return (new TelnyxTransport($apiKey, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
{
return ['telnyx'];
}
}
Loading
0