2023-05-29 16:32:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Appwrite\Platform\Workers;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Utopia\App;
|
|
|
|
|
use Utopia\CLI\Console;
|
|
|
|
|
use Utopia\DSN\DSN;
|
|
|
|
|
use Utopia\Messaging\Messages\Sms;
|
|
|
|
|
use Utopia\Messaging\Adapters\SMS\Mock;
|
|
|
|
|
use Utopia\Messaging\Adapters\SMS\Msg91;
|
|
|
|
|
use Utopia\Messaging\Adapters\SMS\Telesign;
|
|
|
|
|
use Utopia\Messaging\Adapters\SMS\TextMagic;
|
|
|
|
|
use Utopia\Messaging\Adapters\SMS\Twilio;
|
|
|
|
|
use Utopia\Messaging\Adapters\SMS\Vonage;
|
|
|
|
|
use Utopia\Platform\Action;
|
|
|
|
|
use Utopia\Queue\Message;
|
|
|
|
|
|
|
|
|
|
class Messaging extends Action
|
|
|
|
|
{
|
2023-05-30 08:53:52 +00:00
|
|
|
private ?DSN $dsn = null;
|
|
|
|
|
private string $user = '';
|
|
|
|
|
private string $secret = '';
|
|
|
|
|
private string $provider = '';
|
2023-05-29 16:32:33 +00:00
|
|
|
|
|
|
|
|
public static function getName(): string
|
|
|
|
|
{
|
|
|
|
|
return 'messaging';
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 03:54:34 +00:00
|
|
|
/**
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-05-29 16:32:33 +00:00
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->provider = App::getEnv('_APP_SMS_PROVIDER', '');
|
2023-05-30 08:53:52 +00:00
|
|
|
if (!empty($this->provider)) {
|
|
|
|
|
$this->dsn = new DSN($this->provider);
|
|
|
|
|
$this->user = $this->dsn->getUser();
|
|
|
|
|
$this->secret = $this->dsn->getPassword();
|
|
|
|
|
}
|
2023-05-29 16:32:33 +00:00
|
|
|
|
|
|
|
|
$this
|
|
|
|
|
->desc('Messaging worker')
|
|
|
|
|
->inject('message')
|
|
|
|
|
->callback(fn($message) => $this->action($message));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 03:54:34 +00:00
|
|
|
/**
|
2023-10-01 17:39:26 +00:00
|
|
|
* @param Message $message
|
|
|
|
|
* @return void
|
2023-06-02 03:54:34 +00:00
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2023-05-29 16:32:33 +00:00
|
|
|
public function action(Message $message): void
|
|
|
|
|
{
|
|
|
|
|
$payload = $message->getPayload() ?? [];
|
|
|
|
|
|
|
|
|
|
if (empty($payload)) {
|
2023-10-01 17:39:26 +00:00
|
|
|
Console::error('Payload arg not found');
|
|
|
|
|
return;
|
2023-05-29 16:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($payload['recipient'])) {
|
2023-10-01 17:39:26 +00:00
|
|
|
Console::error('Recipient arg not found');
|
|
|
|
|
return;
|
2023-05-29 16:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($payload['message'])) {
|
2023-10-01 17:39:26 +00:00
|
|
|
Console::error('Message arg not found');
|
|
|
|
|
return;
|
2023-05-29 16:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sms = match ($this->dsn->getHost()) {
|
|
|
|
|
'mock' => new Mock($this->user, $this->secret), // used for tests
|
|
|
|
|
'twilio' => new Twilio($this->user, $this->secret),
|
|
|
|
|
'text-magic' => new TextMagic($this->user, $this->secret),
|
|
|
|
|
'telesign' => new Telesign($this->user, $this->secret),
|
|
|
|
|
'msg91' => new Msg91($this->user, $this->secret),
|
|
|
|
|
'vonage' => new Vonage($this->user, $this->secret),
|
|
|
|
|
default => null
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (empty(App::getEnv('_APP_SMS_PROVIDER'))) {
|
2023-10-01 17:39:26 +00:00
|
|
|
Console::error('Skipped sms processing. No Phone provider has been set.');
|
2023-05-29 16:32:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 17:39:26 +00:00
|
|
|
$from = App::getEnv('_APP_SMS_FROM');
|
|
|
|
|
|
2023-05-29 16:32:33 +00:00
|
|
|
if (empty($from)) {
|
2023-10-01 17:39:26 +00:00
|
|
|
Console::error('Skipped sms processing. No phone number has been set.');
|
2023-05-29 16:32:33 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$message = new SMS(
|
2023-06-04 08:19:49 +00:00
|
|
|
to: [$payload['recipient']],
|
|
|
|
|
content: $payload['message'],
|
2023-05-29 16:32:33 +00:00
|
|
|
from: $from,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$sms->send($message);
|
|
|
|
|
} catch (\Exception $error) {
|
|
|
|
|
throw new Exception('Error sending message: ' . $error->getMessage(), 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|