2022-06-09 01:57:34 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Appwrite\Resque\Worker;
|
|
|
|
use Utopia\CLI\Console;
|
2023-08-22 04:45:15 +12:00
|
|
|
use Utopia\Messaging\Adapters\SMS as SMSAdapter;
|
2022-11-18 21:51:28 +13:00
|
|
|
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;
|
2023-08-24 08:23:52 +12:00
|
|
|
use Utopia\Messaging\Adapters\Push as PushAdapter;
|
|
|
|
use Utopia\Messaging\Adapters\Push\APNS;
|
|
|
|
use Utopia\Messaging\Adapters\Push\FCM;
|
|
|
|
use Utopia\Messaging\Adapters\Email as EmailAdapter;
|
|
|
|
use Utopia\Messaging\Adapters\Email\Mailgun;
|
|
|
|
use Utopia\Messaging\Adapters\Email\SendGrid;
|
2023-08-22 04:45:15 +12:00
|
|
|
|
2023-08-24 08:23:52 +12:00
|
|
|
require_once __DIR__ . '/../init.php';
|
2022-06-09 01:57:34 +12:00
|
|
|
|
|
|
|
Console::title('Messaging V1 Worker');
|
2023-08-24 08:23:52 +12:00
|
|
|
Console::success(APP_NAME . ' messaging worker v1 has started' . "\n");
|
2022-06-09 01:57:34 +12:00
|
|
|
|
|
|
|
class MessagingV1 extends Worker
|
|
|
|
{
|
2023-08-22 04:45:15 +12:00
|
|
|
protected ?SMSAdapter $sms = null;
|
|
|
|
protected ?PushAdapter $push = null;
|
2023-08-24 05:12:43 +12:00
|
|
|
protected ?EmailAdapter $email = null;
|
2023-08-23 00:51:30 +12:00
|
|
|
|
2023-08-24 08:23:52 +12:00
|
|
|
|
2022-06-09 01:57:34 +12:00
|
|
|
protected ?string $from = null;
|
|
|
|
|
|
|
|
public function getName(): string
|
|
|
|
{
|
2023-08-24 08:23:52 +12:00
|
|
|
return "mails";
|
2022-06-09 01:57:34 +12:00
|
|
|
}
|
|
|
|
|
2023-08-22 04:45:15 +12:00
|
|
|
public function sms($record): ?SMSAdapter
|
|
|
|
{
|
|
|
|
$credentials = $record->getAttribute('credentials');
|
|
|
|
return match ($record->getAttribute('provider')) {
|
|
|
|
'twilio' => new Twilio($credentials['accountSid'], $credentials['authToken']),
|
|
|
|
'text-magic' => new TextMagic($credentials['username'], $credentials['apiKey']),
|
|
|
|
'telesign' => new Telesign($credentials['username'], $credentials['password']),
|
|
|
|
'msg91' => new Msg91($credentials['senderId'], $credentials['authKey']),
|
|
|
|
'vonage' => new Vonage($credentials['apiKey'], $credentials['apiSecret']),
|
|
|
|
default => null
|
2023-08-23 00:51:30 +12:00
|
|
|
};
|
2023-08-22 04:45:15 +12:00
|
|
|
}
|
|
|
|
|
2023-08-23 04:47:32 +12:00
|
|
|
public function push($record): ?PushAdapter
|
2023-08-22 04:45:15 +12:00
|
|
|
{
|
|
|
|
$credentials = $record->getAttribute('credentials');
|
|
|
|
return match ($record->getAttribute('provider')) {
|
|
|
|
'apns' => new APNS(
|
2023-08-23 00:51:30 +12:00
|
|
|
$credentials['authKey'],
|
|
|
|
$credentials['authKeyId'],
|
|
|
|
$credentials['teamId'],
|
|
|
|
$credentials['bundleId'],
|
2023-08-22 04:45:15 +12:00
|
|
|
$credentials['endpoint']
|
|
|
|
),
|
|
|
|
'fcm' => new FCM($credentials['serverKey']),
|
|
|
|
default => null
|
2023-08-23 00:51:30 +12:00
|
|
|
};
|
2023-08-22 04:45:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function email($record): ?EmailAdapter
|
2022-06-09 01:57:34 +12:00
|
|
|
{
|
2023-08-22 04:45:15 +12:00
|
|
|
$credentials = $record->getAttribute('credentials');
|
|
|
|
return match ($record->getAttribute('provider')) {
|
|
|
|
'mailgun' => new Mailgun($credentials['apiKey'], $credentials['domain']),
|
|
|
|
'sendgrid' => new SendGrid($credentials['apiKey']),
|
2022-06-09 01:57:34 +12:00
|
|
|
default => null
|
2023-08-23 00:51:30 +12:00
|
|
|
};
|
2023-08-22 04:45:15 +12:00
|
|
|
}
|
2022-06-29 01:32:59 +12:00
|
|
|
|
2023-08-22 04:45:15 +12:00
|
|
|
public function init(): void
|
|
|
|
{
|
2022-06-09 01:57:34 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run(): void
|
|
|
|
{
|
2023-08-30 04:13:21 +12:00
|
|
|
$providerId = $this->args['providerId'];
|
|
|
|
$providerRecord =
|
|
|
|
$this
|
|
|
|
->getConsoleDB()
|
|
|
|
->getDocument('providers', $providerId);
|
2023-08-22 04:45:15 +12:00
|
|
|
|
2023-08-29 08:27:55 +12:00
|
|
|
$provider = match ($providerRecord->getAttribute('type')) {//stubbbbbbed.
|
|
|
|
'sms' => $this->sms($providerRecord),
|
|
|
|
'push' => $this->push($providerRecord),
|
|
|
|
'email' => $this->email($providerRecord),
|
|
|
|
default => null
|
|
|
|
};
|
|
|
|
|
|
|
|
// Query for the provider
|
|
|
|
// switch on provider name
|
|
|
|
// call function passing needed credentials returns required provider.
|
|
|
|
|
2023-08-30 04:13:21 +12:00
|
|
|
$messageId = $this->args['messageId'];
|
|
|
|
$messageRecord =
|
|
|
|
$this
|
|
|
|
->getConsoleDB()
|
|
|
|
->getDocument('messages', $messageId);
|
|
|
|
|
|
|
|
$message = match ($providerRecord->getAttribute('type')) {
|
|
|
|
'sms' => $this->buildSMSMessage($messageRecord->getArrayCopy()),
|
|
|
|
'push' => $this->buildPushMessage($messageRecord->getArrayCopy()),
|
|
|
|
'email' => $this->buildEmailMessage($messageRecord->getArrayCopy()),
|
|
|
|
default => null
|
|
|
|
};
|
2023-08-29 08:27:55 +12:00
|
|
|
|
2023-08-24 08:23:52 +12:00
|
|
|
|
2023-08-30 04:13:21 +12:00
|
|
|
$provider->send($message);
|
2022-06-09 01:57:34 +12:00
|
|
|
}
|
|
|
|
|
2023-08-23 04:47:32 +12:00
|
|
|
public function shutdown(): void
|
2022-06-09 01:57:34 +12:00
|
|
|
{
|
|
|
|
}
|
2023-08-24 00:49:32 +12:00
|
|
|
|
|
|
|
private function buildEmailMessage($data): array
|
|
|
|
{
|
|
|
|
$from = $data['from'];
|
|
|
|
$to = $data['to'];
|
|
|
|
$subject = $data['subject'];
|
|
|
|
$body = $data['content'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'from' => $from,
|
|
|
|
'to' => $to,
|
|
|
|
'subject' => $subject,
|
|
|
|
'body' => $body,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildSMSMessage($data): array
|
|
|
|
{
|
|
|
|
$from = $data['from'];
|
|
|
|
$to = $data['to'];
|
|
|
|
$body = $data['content'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'from' => $from,
|
|
|
|
'to' => $to,
|
2023-08-24 08:23:52 +12:00
|
|
|
'body' => $body
|
2023-08-24 00:49:32 +12:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildPushMessage($data): array
|
|
|
|
{
|
|
|
|
$to = $data['to'];
|
|
|
|
$title = $data['title'];
|
|
|
|
$body = $data['body'];
|
|
|
|
$data = $data['data'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'to' => $to,
|
|
|
|
'title' => $title,
|
|
|
|
'body' => $body,
|
2023-08-24 08:23:52 +12:00
|
|
|
'data' => $data
|
2023-08-24 00:49:32 +12:00
|
|
|
];
|
|
|
|
}
|
2022-06-09 01:57:34 +12:00
|
|
|
}
|