1
0
Fork 0
mirror of synced 2024-06-18 18:54:55 +12:00
appwrite/app/workers/messaging.php

79 lines
2.1 KiB
PHP
Raw Normal View History

2022-06-09 01:57:34 +12:00
<?php
2022-06-21 00:22:35 +12:00
use Appwrite\DSN\DSN;
2022-06-09 01:57:34 +12:00
use Appwrite\Resque\Worker;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Messaging\Adapter;
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\Messaging\Messages\SMS;
2022-06-09 01:57:34 +12:00
require_once __DIR__ . '/../init.php';
Console::title('Messaging V1 Worker');
Console::success(APP_NAME . ' messaging worker v1 has started' . "\n");
class MessagingV1 extends Worker
{
2022-09-19 20:09:48 +12:00
protected ?Adapter $sms = null;
2022-06-09 01:57:34 +12:00
protected ?string $from = null;
public function getName(): string
{
return "mails";
}
public function init(): void
{
$dsn = new DSN(App::getEnv('_APP_SMS_PROVIDER'));
2022-06-21 00:22:35 +12:00
$user = $dsn->getUser();
$secret = $dsn->getPassword();
2022-06-09 01:57:34 +12:00
$this->sms = match ($dsn->getHost()) {
2022-10-03 13:42:53 +13:00
'mock' => new Mock($user, $secret), // used for tests
2022-06-09 01:57:34 +12:00
'twilio' => new Twilio($user, $secret),
'text-magic' => new TextMagic($user, $secret),
'telesign' => new Telesign($user, $secret),
2022-07-10 02:04:57 +12:00
'msg91' => new Msg91($user, $secret),
2022-07-13 03:45:22 +12:00
'vonage' => new Vonage($user, $secret),
2022-06-09 01:57:34 +12:00
default => null
};
2022-06-29 01:32:59 +12:00
$this->from = App::getEnv('_APP_SMS_FROM');
2022-06-09 01:57:34 +12:00
}
public function run(): void
{
if (empty(App::getEnv('_APP_SMS_PROVIDER'))) {
2022-06-09 01:57:34 +12:00
Console::info('Skipped sms processing. No Phone provider has been set.');
return;
}
2022-06-29 01:40:59 +12:00
if (empty($this->from)) {
2022-06-29 01:32:59 +12:00
Console::info('Skipped sms processing. No phone number has been set.');
return;
}
$message = new SMS(
to: [$this->args['recipient']],
content: $this->args['message'],
from: $this->from,
);
2022-06-09 01:57:34 +12:00
try {
$this->sms->send($message);
2022-06-09 01:57:34 +12:00
} catch (\Exception $error) {
throw new Exception('Error sending message: ' . $error->getMessage(), 500);
}
}
public function shutdown(): void
{
}
}