mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
refactor(messaging): build the platform's SMS adapter as a DI resource
The worker memoised its own SMS adapter in a nullable field, so construction and caching lived in the action rather than the container. It is now adapterForSMS, registered in app/init/resources.php and cached for the life of the process by the container itself. Provider owns the mapping from a provider, whether a project's document or the platform's DSN, to the adapter that sends it. Push stays with the worker: its Appwrite provider is built from the message and the project database, so a provider document alone does not describe it. The factory reports a malformed platform DSN and reads it as unset rather than throwing, because it is injected into every messaging job and one bad variable must not stop a project's push and email. No behaviour change.
This commit is contained in:
3 files changed
+296
-237
No files matched your search
@@ -22,6 +22,7 @@ use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher;
|
||||
use Appwrite\Event\Publisher\Usage as UsagePublisher;
|
||||
use Appwrite\Execution\Store as ExecutionStore;
|
||||
use Appwrite\Geo\Client as GeoClient;
|
||||
use Appwrite\Messaging\Provider as MessagingProvider;
|
||||
use Appwrite\Platform\Modules\Storage\Config\StorageCacheControl;
|
||||
use Appwrite\Screenshots\Client as ScreenshotsClient;
|
||||
use Appwrite\Usage\Connection as UsageConnection;
|
||||
@@ -45,6 +46,7 @@ use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\DI\Container;
|
||||
use Utopia\DSN\DSN;
|
||||
use Utopia\Lock\Distributed;
|
||||
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
|
||||
use Utopia\Pools\Adapter\Swoole as SwoolePoolAdapter;
|
||||
use Utopia\Pools\Group;
|
||||
use Utopia\Pools\Pool as Connections;
|
||||
@@ -117,6 +119,24 @@ $container->set('autogravity', function (Cache $cache) {
|
||||
|
||||
$container->set('telemetry', fn () => new NoTelemetry(), []);
|
||||
|
||||
/**
|
||||
* The platform's own SMS adapter, built from _APP_SMS_PROVIDER for the one-time passcodes
|
||||
* and invites Appwrite sends on a project's behalf. Null when the variable is unset, which
|
||||
* is how an instance says it cannot deliver over SMS.
|
||||
*
|
||||
* A malformed DSN is reported and read as unset rather than thrown, because this is injected
|
||||
* into every messaging job: letting one bad platform variable escape would stop a project's
|
||||
* push and email messages, which it has nothing to do with.
|
||||
*/
|
||||
$container->set('adapterForSMS', function (Telemetry $telemetry): ?SMSAdapter {
|
||||
try {
|
||||
return (new MessagingProvider($telemetry))->internalSMS();
|
||||
} catch (\Throwable $error) {
|
||||
Console::error('Ignoring _APP_SMS_PROVIDER: ' . $error->getMessage());
|
||||
return null;
|
||||
}
|
||||
}, ['telemetry']);
|
||||
|
||||
$container->set('authorization', fn () => new Authorization(), []);
|
||||
|
||||
$container->set('publisher', fn (Group $pools) => new BrokerPool(publisher: $pools->get('publisher')), ['pools']);
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\Messaging;
|
||||
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\DSN\DSN;
|
||||
use Utopia\Messaging\Adapter\Email as EmailAdapter;
|
||||
use Utopia\Messaging\Adapter\Email\Mailgun;
|
||||
use Utopia\Messaging\Adapter\Email\Resend;
|
||||
use Utopia\Messaging\Adapter\Email\Sendgrid;
|
||||
use Utopia\Messaging\Adapter\Email\SES;
|
||||
use Utopia\Messaging\Adapter\Email\SMTP;
|
||||
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
|
||||
use Utopia\Messaging\Adapter\SMS\Fast2SMS;
|
||||
use Utopia\Messaging\Adapter\SMS\GEOSMS;
|
||||
use Utopia\Messaging\Adapter\SMS\Inforu;
|
||||
use Utopia\Messaging\Adapter\SMS\Mock;
|
||||
use Utopia\Messaging\Adapter\SMS\Msg91;
|
||||
use Utopia\Messaging\Adapter\SMS\Telesign;
|
||||
use Utopia\Messaging\Adapter\SMS\TextMagic;
|
||||
use Utopia\Messaging\Adapter\SMS\Twilio;
|
||||
use Utopia\Messaging\Adapter\SMS\Vonage;
|
||||
use Utopia\Messaging\Messages\Push;
|
||||
use Utopia\Queue\Message;
|
||||
use Utopia\System\System;
|
||||
use Utopia\Telemetry\Adapter as Telemetry;
|
||||
|
||||
/**
|
||||
* Builds the sending adapter a messaging provider needs.
|
||||
*
|
||||
* A provider is either one a project configured and stored as a document, or the
|
||||
* platform's own, described by the _APP_SMS_PROVIDER environment variable and used
|
||||
* for the one-time passcodes and invites Appwrite sends on a project's behalf. Both
|
||||
* end up as the same adapter, so the DSN is turned into a provider document and
|
||||
* takes the same path.
|
||||
*
|
||||
* Push is missing on purpose. Its Appwrite provider is built from the message being
|
||||
* sent and the project database it belongs to, so it cannot be described by a
|
||||
* provider document alone and stays with the worker that has those to hand.
|
||||
*/
|
||||
class Provider
|
||||
{
|
||||
public function __construct(private readonly Telemetry $telemetry)
|
||||
{
|
||||
}
|
||||
|
||||
public function sms(Document $provider): ?SMSAdapter
|
||||
{
|
||||
$credentials = $provider->getAttribute('credentials');
|
||||
|
||||
$adapter = match ($provider->getAttribute('provider')) {
|
||||
'mock' => (new Mock('username', 'password'))->setEndpoint('http://request-catcher-sms:5000/'),
|
||||
'twilio' => new Twilio(
|
||||
$credentials['accountSid'] ?? '',
|
||||
$credentials['authToken'] ?? '',
|
||||
null,
|
||||
$credentials['messagingServiceSid'] ?? null
|
||||
),
|
||||
'textmagic' => new TextMagic(
|
||||
$credentials['username'] ?? '',
|
||||
$credentials['apiKey'] ?? ''
|
||||
),
|
||||
'telesign' => new Telesign(
|
||||
$credentials['customerId'] ?? '',
|
||||
$credentials['apiKey'] ?? ''
|
||||
),
|
||||
'msg91' => new Msg91(
|
||||
$credentials['senderId'] ?? '',
|
||||
$credentials['authKey'] ?? '',
|
||||
$credentials['templateId'] ?? ''
|
||||
),
|
||||
'vonage' => new Vonage(
|
||||
$credentials['apiKey'] ?? '',
|
||||
$credentials['apiSecret'] ?? ''
|
||||
),
|
||||
'fast2sms' => new Fast2SMS(
|
||||
$credentials['apiKey'] ?? '',
|
||||
$credentials['senderId'] ?? '',
|
||||
$credentials['messageId'] ?? '',
|
||||
$credentials['useDLT'] ?? true
|
||||
),
|
||||
'inforu' => new Inforu(
|
||||
$credentials['senderId'] ?? '',
|
||||
$credentials['apiKey'] ?? '',
|
||||
),
|
||||
default => null
|
||||
};
|
||||
|
||||
if ($adapter !== null) {
|
||||
$adapter->setTelemetry($this->telemetry);
|
||||
}
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
public function email(Document $provider): ?EmailAdapter
|
||||
{
|
||||
$credentials = $provider->getAttribute('credentials', []);
|
||||
$options = $provider->getAttribute('options', []);
|
||||
$apiKey = $credentials['apiKey'] ?? '';
|
||||
|
||||
$adapter = match ($provider->getAttribute('provider')) {
|
||||
'mock' => new Mock('username', 'password'),
|
||||
'smtp' => new SMTP(
|
||||
$credentials['host'] ?? '',
|
||||
$credentials['port'] ?? 25,
|
||||
$credentials['username'] ?? '',
|
||||
$credentials['password'] ?? '',
|
||||
$options['encryption'] ?? '',
|
||||
$options['autoTLS'] ?? false,
|
||||
$options['mailer'] ?? '',
|
||||
),
|
||||
'mailgun' => new Mailgun(
|
||||
$apiKey,
|
||||
$credentials['domain'] ?? '',
|
||||
$credentials['isEuRegion'] ?? false
|
||||
),
|
||||
'sendgrid' => new Sendgrid($apiKey),
|
||||
'resend' => new Resend($apiKey),
|
||||
'ses' => new SES(
|
||||
$credentials['accessKey'] ?? '',
|
||||
$credentials['secretKey'] ?? '',
|
||||
$credentials['region'] ?? '',
|
||||
$credentials['sessionToken'] ?? null,
|
||||
),
|
||||
default => null
|
||||
};
|
||||
|
||||
if ($adapter !== null) {
|
||||
$adapter->setTelemetry($this->telemetry);
|
||||
}
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
public function internalSMS(): ?SMSAdapter
|
||||
{
|
||||
if (empty(System::getEnv('_APP_SMS_PROVIDER')) || empty(System::getEnv('_APP_SMS_FROM'))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$providers = System::getEnv('_APP_SMS_PROVIDER', '');
|
||||
|
||||
$dsns = [];
|
||||
if (!empty($providers)) {
|
||||
$providers = explode(',', $providers);
|
||||
foreach ($providers as $provider) {
|
||||
$dsns[] = new DSN($provider);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($dsns) === 1) {
|
||||
$provider = $this->fromDSN($dsns[0]);
|
||||
$adapter = $this->sms($provider);
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
$defaultDSN = null;
|
||||
$localDSNs = [];
|
||||
|
||||
/** @var DSN $dsn */
|
||||
foreach ($dsns as $dsn) {
|
||||
if ($dsn->getParam('local', '') === 'default') {
|
||||
$defaultDSN = $dsn;
|
||||
} else {
|
||||
$localDSNs[] = $dsn;
|
||||
}
|
||||
}
|
||||
|
||||
if ($defaultDSN === null) {
|
||||
throw new \Exception('No default SMS provider found');
|
||||
}
|
||||
|
||||
$defaultProvider = $this->fromDSN($defaultDSN);
|
||||
$adapter = $this->sms($defaultProvider);
|
||||
$geosms = new GEOSMS($adapter);
|
||||
$geosms->setTelemetry($this->telemetry);
|
||||
|
||||
/** @var DSN $localDSN */
|
||||
foreach ($localDSNs as $localDSN) {
|
||||
try {
|
||||
$provider = $this->fromDSN($localDSN);
|
||||
$adapter = $this->sms($provider);
|
||||
} catch (\Exception) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$callingCode = $localDSN->getParam('local', '');
|
||||
if (empty($callingCode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$geosms->setLocal($callingCode, $adapter);
|
||||
}
|
||||
return $geosms;
|
||||
}
|
||||
|
||||
private function fromDSN(DSN $dsn): Document
|
||||
{
|
||||
$host = $dsn->getHost();
|
||||
$password = $dsn->getPassword();
|
||||
$user = $dsn->getUser();
|
||||
$from = System::getEnv('_APP_SMS_FROM');
|
||||
|
||||
$provider = new Document([
|
||||
'$id' => ID::unique(),
|
||||
'provider' => $host,
|
||||
'type' => MESSAGE_TYPE_SMS,
|
||||
'name' => 'Internal SMS',
|
||||
'enabled' => true,
|
||||
'credentials' => match ($host) {
|
||||
'twilio' => [
|
||||
'accountSid' => $user,
|
||||
'authToken' => $password,
|
||||
// Messaging Service SIDs are always 34 characters; alphanumeric sender IDs, at most 11, can also start with MG
|
||||
// https://www.twilio.com/docs/messaging/api/service-resource
|
||||
'messagingServiceSid' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? $from : null
|
||||
],
|
||||
'textmagic' => [
|
||||
'username' => $user,
|
||||
'apiKey' => $password
|
||||
],
|
||||
'telesign' => [
|
||||
'customerId' => $user,
|
||||
'apiKey' => $password
|
||||
],
|
||||
'msg91' => [
|
||||
'senderId' => $user,
|
||||
'authKey' => $password,
|
||||
'templateId' => $dsn->getParam('templateId', $from),
|
||||
],
|
||||
'vonage' => [
|
||||
'apiKey' => $user,
|
||||
'apiSecret' => $password
|
||||
],
|
||||
'fast2sms' => [
|
||||
'senderId' => $user,
|
||||
'apiKey' => $password,
|
||||
'messageId' => $dsn->getParam('messageId'),
|
||||
'useDLT' => $dsn->getParam('useDLT'),
|
||||
],
|
||||
'inforu' => [
|
||||
'senderId' => $user,
|
||||
'apiKey' => $password,
|
||||
],
|
||||
default => null
|
||||
},
|
||||
'options' => match ($host) {
|
||||
'twilio' => [
|
||||
'from' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? null : $from
|
||||
],
|
||||
default => [
|
||||
'from' => $from
|
||||
]
|
||||
}
|
||||
]);
|
||||
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ use Appwrite\Event\Message\Usage;
|
||||
use Appwrite\Event\Publisher\Usage as UsagePublisher;
|
||||
use Appwrite\Messaging\Adapter\Mqtt;
|
||||
use Appwrite\Messaging\Adapter\Push\Appwrite as AppwritePush;
|
||||
use Appwrite\Messaging\Provider;
|
||||
use Appwrite\Messaging\Status as MessageStatus;
|
||||
use Appwrite\OpenSSL\OpenSSL;
|
||||
use Appwrite\PubSub\Adapter\Pool as PubSubPool;
|
||||
@@ -19,29 +20,18 @@ use Utopia\Database\DateTime;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Helpers\ID;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\DSN\DSN;
|
||||
use Utopia\Lock\Semaphore;
|
||||
use Utopia\Messaging\Adapter\Email as EmailAdapter;
|
||||
use Utopia\Messaging\Adapter\Email\Mailgun;
|
||||
use Utopia\Messaging\Adapter\Email\Resend;
|
||||
use Utopia\Messaging\Adapter\Email\Sendgrid;
|
||||
use Utopia\Messaging\Adapter\Email\SES;
|
||||
use Utopia\Messaging\Adapter\Email\SMTP;
|
||||
use Utopia\Messaging\Adapter\Push\APNS;
|
||||
use Utopia\Messaging\Adapter\Push as PushAdapter;
|
||||
use Utopia\Messaging\Adapter\Push\FCM;
|
||||
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
|
||||
use Utopia\Messaging\Adapter\SMS\Fast2SMS;
|
||||
use Utopia\Messaging\Adapter\SMS\GEOSMS;
|
||||
use Utopia\Messaging\Adapter\SMS\GEOSMS\CallingCode;
|
||||
use Utopia\Messaging\Adapter\SMS\Inforu;
|
||||
use Utopia\Messaging\Adapter\SMS\Mock;
|
||||
use Utopia\Messaging\Adapter\SMS\Msg91;
|
||||
use Utopia\Messaging\Adapter\SMS\Msg91\MetadataParameter;
|
||||
use Utopia\Messaging\Adapter\SMS\Telesign;
|
||||
use Utopia\Messaging\Adapter\SMS\TextMagic;
|
||||
use Utopia\Messaging\Adapter\SMS\Twilio;
|
||||
use Utopia\Messaging\Adapter\SMS\Vonage;
|
||||
use Utopia\Messaging\Messages\Email;
|
||||
use Utopia\Messaging\Messages\Email\Attachment;
|
||||
use Utopia\Messaging\Messages\Push;
|
||||
@@ -63,10 +53,10 @@ use function Swoole\Coroutine\batch;
|
||||
|
||||
class Messaging extends Action
|
||||
{
|
||||
private ?SMSAdapter $adapter = null;
|
||||
|
||||
private Telemetry $telemetry;
|
||||
|
||||
private Provider $provider;
|
||||
|
||||
private Group $pools;
|
||||
|
||||
public static function getName(): string
|
||||
@@ -88,6 +78,7 @@ class Messaging extends Action
|
||||
->inject('publisherForUsage')
|
||||
->inject('telemetry')
|
||||
->inject('pools')
|
||||
->inject('adapterForSMS')
|
||||
->callback($this->action(...));
|
||||
}
|
||||
|
||||
@@ -98,6 +89,7 @@ class Messaging extends Action
|
||||
* @param Device $deviceForFiles
|
||||
* @param UsagePublisher $publisherForUsage
|
||||
* @param Telemetry $telemetry
|
||||
* @param SMSAdapter|null $adapterForSMS
|
||||
* @param Group $pools
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
@@ -109,10 +101,12 @@ class Messaging extends Action
|
||||
Device $deviceForFiles,
|
||||
UsagePublisher $publisherForUsage,
|
||||
Telemetry $telemetry,
|
||||
Group $pools
|
||||
Group $pools,
|
||||
?SMSAdapter $adapterForSMS
|
||||
): void {
|
||||
$this->telemetry = $telemetry;
|
||||
$this->pools = $pools;
|
||||
$this->provider = new Provider($telemetry);
|
||||
$payload = $message->getPayload();
|
||||
|
||||
if (empty($payload)) {
|
||||
@@ -128,7 +122,7 @@ class Messaging extends Action
|
||||
$message = new Document($payload['message'] ?? []);
|
||||
$recipients = $payload['recipients'] ?? [];
|
||||
|
||||
$this->sendInternalSMSMessage($message, $project, $recipients);
|
||||
$this->sendInternalSMSMessage($message, $project, $recipients, $adapterForSMS);
|
||||
break;
|
||||
case MESSAGE_SEND_TYPE_EXTERNAL:
|
||||
$messageId = $payload['messageId'];
|
||||
@@ -258,9 +252,9 @@ class Messaging extends Action
|
||||
$resolvedProviderType = $provider->getAttribute('type');
|
||||
|
||||
$adapter = match ($resolvedProviderType) {
|
||||
MESSAGE_TYPE_SMS => $this->getSmsAdapter($provider),
|
||||
MESSAGE_TYPE_SMS => $this->provider->sms($provider),
|
||||
MESSAGE_TYPE_PUSH => $this->getPushAdapter($provider, $dbForProject, $project, $message),
|
||||
MESSAGE_TYPE_EMAIL => $this->getEmailAdapter($provider),
|
||||
MESSAGE_TYPE_EMAIL => $this->provider->email($provider),
|
||||
default => throw new \Exception('Provider with the requested ID is of the incorrect type')
|
||||
};
|
||||
|
||||
@@ -823,13 +817,9 @@ class Messaging extends Action
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function sendInternalSMSMessage(Document $message, Document $project, array $recipients): void
|
||||
private function sendInternalSMSMessage(Document $message, Document $project, array $recipients, ?SMSAdapter $adapterForSMS): void
|
||||
{
|
||||
if ($this->adapter === null) {
|
||||
$this->adapter = $this->createInternalSMSAdapter();
|
||||
}
|
||||
|
||||
if ($this->adapter === null) {
|
||||
if ($adapterForSMS === null) {
|
||||
throw new \Exception('SMS adapter is not set.');
|
||||
}
|
||||
|
||||
@@ -860,59 +850,10 @@ class Messaging extends Action
|
||||
// webhooks can be attributed back to the originating project.
|
||||
$sms->setMetadata([MetadataParameter::UUID->value => $project->getId()]);
|
||||
|
||||
$this->adapter->send($sms);
|
||||
$adapterForSMS->send($sms);
|
||||
}
|
||||
|
||||
|
||||
protected function getSmsAdapter(Document $provider): ?SMSAdapter
|
||||
{
|
||||
$credentials = $provider->getAttribute('credentials');
|
||||
|
||||
$adapter = match ($provider->getAttribute('provider')) {
|
||||
'mock' => (new Mock('username', 'password'))->setEndpoint('http://request-catcher-sms:5000/'),
|
||||
'twilio' => new Twilio(
|
||||
$credentials['accountSid'] ?? '',
|
||||
$credentials['authToken'] ?? '',
|
||||
null,
|
||||
$credentials['messagingServiceSid'] ?? null
|
||||
),
|
||||
'textmagic' => new TextMagic(
|
||||
$credentials['username'] ?? '',
|
||||
$credentials['apiKey'] ?? ''
|
||||
),
|
||||
'telesign' => new Telesign(
|
||||
$credentials['customerId'] ?? '',
|
||||
$credentials['apiKey'] ?? ''
|
||||
),
|
||||
'msg91' => new Msg91(
|
||||
$credentials['senderId'] ?? '',
|
||||
$credentials['authKey'] ?? '',
|
||||
$credentials['templateId'] ?? ''
|
||||
),
|
||||
'vonage' => new Vonage(
|
||||
$credentials['apiKey'] ?? '',
|
||||
$credentials['apiSecret'] ?? ''
|
||||
),
|
||||
'fast2sms' => new Fast2SMS(
|
||||
$credentials['apiKey'] ?? '',
|
||||
$credentials['senderId'] ?? '',
|
||||
$credentials['messageId'] ?? '',
|
||||
$credentials['useDLT'] ?? true
|
||||
),
|
||||
'inforu' => new Inforu(
|
||||
$credentials['senderId'] ?? '',
|
||||
$credentials['apiKey'] ?? '',
|
||||
),
|
||||
default => null
|
||||
};
|
||||
|
||||
if ($adapter !== null) {
|
||||
$adapter->setTelemetry($this->telemetry);
|
||||
}
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
protected function getPushAdapter(Document $provider, Database $dbForProject, Document $project, Document $message): ?PushAdapter
|
||||
{
|
||||
$credentials = $provider->getAttribute('credentials');
|
||||
@@ -946,46 +887,6 @@ class Messaging extends Action
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
protected function getEmailAdapter(Document $provider): ?EmailAdapter
|
||||
{
|
||||
$credentials = $provider->getAttribute('credentials', []);
|
||||
$options = $provider->getAttribute('options', []);
|
||||
$apiKey = $credentials['apiKey'] ?? '';
|
||||
|
||||
$adapter = match ($provider->getAttribute('provider')) {
|
||||
'mock' => new Mock('username', 'password'),
|
||||
'smtp' => new SMTP(
|
||||
$credentials['host'] ?? '',
|
||||
$credentials['port'] ?? 25,
|
||||
$credentials['username'] ?? '',
|
||||
$credentials['password'] ?? '',
|
||||
$options['encryption'] ?? '',
|
||||
$options['autoTLS'] ?? false,
|
||||
$options['mailer'] ?? '',
|
||||
),
|
||||
'mailgun' => new Mailgun(
|
||||
$apiKey,
|
||||
$credentials['domain'] ?? '',
|
||||
$credentials['isEuRegion'] ?? false
|
||||
),
|
||||
'sendgrid' => new Sendgrid($apiKey),
|
||||
'resend' => new Resend($apiKey),
|
||||
'ses' => new SES(
|
||||
$credentials['accessKey'] ?? '',
|
||||
$credentials['secretKey'] ?? '',
|
||||
$credentials['region'] ?? '',
|
||||
$credentials['sessionToken'] ?? null,
|
||||
),
|
||||
default => null
|
||||
};
|
||||
|
||||
if ($adapter !== null) {
|
||||
$adapter->setTelemetry($this->telemetry);
|
||||
}
|
||||
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Materialise a message's attachments as files the adapters can read.
|
||||
*
|
||||
@@ -1218,128 +1119,4 @@ class Messaging extends Action
|
||||
return new Local(APP_STORAGE_UPLOADS . '/app-' . $project->getId());
|
||||
}
|
||||
|
||||
private function createInternalSMSAdapter(): ?SMSAdapter
|
||||
{
|
||||
if (empty(System::getEnv('_APP_SMS_PROVIDER')) || empty(System::getEnv('_APP_SMS_FROM'))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$providers = System::getEnv('_APP_SMS_PROVIDER', '');
|
||||
|
||||
$dsns = [];
|
||||
if (!empty($providers)) {
|
||||
$providers = explode(',', $providers);
|
||||
foreach ($providers as $provider) {
|
||||
$dsns[] = new DSN($provider);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($dsns) === 1) {
|
||||
$provider = $this->createProviderFromDSN($dsns[0]);
|
||||
$adapter = $this->getSmsAdapter($provider);
|
||||
return $adapter;
|
||||
}
|
||||
|
||||
$defaultDSN = null;
|
||||
$localDSNs = [];
|
||||
|
||||
/** @var DSN $dsn */
|
||||
foreach ($dsns as $dsn) {
|
||||
if ($dsn->getParam('local', '') === 'default') {
|
||||
$defaultDSN = $dsn;
|
||||
} else {
|
||||
$localDSNs[] = $dsn;
|
||||
}
|
||||
}
|
||||
|
||||
if ($defaultDSN === null) {
|
||||
throw new \Exception('No default SMS provider found');
|
||||
}
|
||||
|
||||
$defaultProvider = $this->createProviderFromDSN($defaultDSN);
|
||||
$adapter = $this->getSmsAdapter($defaultProvider);
|
||||
$geosms = new GEOSMS($adapter);
|
||||
$geosms->setTelemetry($this->telemetry);
|
||||
|
||||
/** @var DSN $localDSN */
|
||||
foreach ($localDSNs as $localDSN) {
|
||||
try {
|
||||
$provider = $this->createProviderFromDSN($localDSN);
|
||||
$adapter = $this->getSmsAdapter($provider);
|
||||
} catch (\Exception) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$callingCode = $localDSN->getParam('local', '');
|
||||
if (empty($callingCode)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$geosms->setLocal($callingCode, $adapter);
|
||||
}
|
||||
return $geosms;
|
||||
}
|
||||
|
||||
private function createProviderFromDSN(DSN $dsn): Document
|
||||
{
|
||||
$host = $dsn->getHost();
|
||||
$password = $dsn->getPassword();
|
||||
$user = $dsn->getUser();
|
||||
$from = System::getEnv('_APP_SMS_FROM');
|
||||
|
||||
$provider = new Document([
|
||||
'$id' => ID::unique(),
|
||||
'provider' => $host,
|
||||
'type' => MESSAGE_TYPE_SMS,
|
||||
'name' => 'Internal SMS',
|
||||
'enabled' => true,
|
||||
'credentials' => match ($host) {
|
||||
'twilio' => [
|
||||
'accountSid' => $user,
|
||||
'authToken' => $password,
|
||||
// Messaging Service SIDs are always 34 characters; alphanumeric sender IDs, at most 11, can also start with MG
|
||||
// https://www.twilio.com/docs/messaging/api/service-resource
|
||||
'messagingServiceSid' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? $from : null
|
||||
],
|
||||
'textmagic' => [
|
||||
'username' => $user,
|
||||
'apiKey' => $password
|
||||
],
|
||||
'telesign' => [
|
||||
'customerId' => $user,
|
||||
'apiKey' => $password
|
||||
],
|
||||
'msg91' => [
|
||||
'senderId' => $user,
|
||||
'authKey' => $password,
|
||||
'templateId' => $dsn->getParam('templateId', $from),
|
||||
],
|
||||
'vonage' => [
|
||||
'apiKey' => $user,
|
||||
'apiSecret' => $password
|
||||
],
|
||||
'fast2sms' => [
|
||||
'senderId' => $user,
|
||||
'apiKey' => $password,
|
||||
'messageId' => $dsn->getParam('messageId'),
|
||||
'useDLT' => $dsn->getParam('useDLT'),
|
||||
],
|
||||
'inforu' => [
|
||||
'senderId' => $user,
|
||||
'apiKey' => $password,
|
||||
],
|
||||
default => null
|
||||
},
|
||||
'options' => match ($host) {
|
||||
'twilio' => [
|
||||
'from' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? null : $from
|
||||
],
|
||||
default => [
|
||||
'from' => $from
|
||||
]
|
||||
}
|
||||
]);
|
||||
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user