-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore | ||
OskarStark marked this conversation as resolved.
8000
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.3 | ||
--- | ||
|
||
* Add the bridge |
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,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) |
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 | ||
{ | ||
9E88 td> | 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; | ||
} | ||
} |
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(); | ||||||||||||||||||||||||||||||||||||||||||||
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. Please pull up the scheme check like here, to first check if the scheme is supported and afterwards for missing options. Thanks symfony/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransportFactory.php Lines 27 to 47 in 7d846d3
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. 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']; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.