adds messaging worker and send an email controller
This commit is contained in:
parent
a6d613542b
commit
4406c7bd55
|
@ -1,9 +1,13 @@
|
|||
<?php
|
||||
|
||||
use Appwrite\Event\Event;
|
||||
use Appwrite\Extend\Exception;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\App;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Validator\Datetime;
|
||||
use Utopia\Validator\ArrayList;
|
||||
use Utopia\Validator\Text;
|
||||
|
||||
/**
|
||||
|
@ -26,7 +30,6 @@ App::post('/v1/messaging/providers/mailgun')
|
|||
->inject('dbForProject')
|
||||
->inject('response')
|
||||
->action(function (string $name, string $apiKey, string $domain, Database $dbForProject, Response $response) {
|
||||
$provider = $dbForProject->getDocument('providers', '64e33e70dd07f0d03efb');
|
||||
$provider = $dbForProject->createDocument('providers', new Document([
|
||||
'name' => $name,
|
||||
'provider' => 'Mailgun',
|
||||
|
@ -40,3 +43,53 @@ App::post('/v1/messaging/providers/mailgun')
|
|||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($provider, Response::MODEL_PROVIDER);
|
||||
});
|
||||
|
||||
App::post('/v1/messaging/messages/email')
|
||||
->desc('Send an email.')
|
||||
->groups(['api', 'messaging'])
|
||||
->label('event', 'messages.create')
|
||||
->label('scope', 'messages.write')
|
||||
->label('sdk.auth', [APP_AUTH_TYPE_ADMIN, APP_AUTH_TYPE_KEY])
|
||||
->label('sdk.namespace', 'messaging')
|
||||
->label('sdk.description', '/docs/references/messaging/send-email.md')
|
||||
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
|
||||
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||
->label('sdk.response.model', Response::MODEL_MESSAGE)
|
||||
->param('providerId', '', new Text(128), 'Email Provider ID.')
|
||||
->param('to', [], new ArrayList(new Text(0)), 'Email Recepient.', true)
|
||||
->param('subject', null, new Text(0), 'Email Subject.', true)
|
||||
->param('content', null, new Text(0), 'Email Content.', true)
|
||||
->param('from', null, new Text(0), 'Email from.', false)
|
||||
->param('html', null, new Text(0), 'Is content of type HTML', false)
|
||||
->param('deliveryTime', null, new Datetime(), 'Delivery time of the message', false)
|
||||
->inject('dbForProject')
|
||||
->inject('events')
|
||||
->inject('response')
|
||||
->action(function (string $providerId, string $to, string $subject, string $content, string $from, string $html, DateTime $deliveryTime, Database $dbForProject, Event $eventsInstance, Response $response) {
|
||||
$provider = $dbForProject->getDocument('providers', $providerId);
|
||||
|
||||
if ($provider->isEmpty()) {
|
||||
throw new Exception(Exception::PROVIDER_NOT_FOUND);
|
||||
}
|
||||
|
||||
$message = $dbForProject->createDocument('messages', new Document([
|
||||
'providerId' => $provider->getId(),
|
||||
'providerInternalId' => $provider->getInternalId(),
|
||||
'to' => $to,
|
||||
'data' => [
|
||||
'subject' => $subject,
|
||||
'content' => $content,
|
||||
],
|
||||
'deliveryTime' => $deliveryTime,
|
||||
'deliveryError' => null,
|
||||
'deliveredTo' => null,
|
||||
'delivered' => false,
|
||||
'search' => null
|
||||
]));
|
||||
|
||||
$eventsInstance->setParam('messageId', $message->getId());
|
||||
|
||||
$response
|
||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||
->dynamic($provider, Response::MODEL_MESSAGE);
|
||||
});
|
||||
|
|
|
@ -1,17 +1,23 @@
|
|||
<?php
|
||||
|
||||
use Appwrite\Resque\Worker;
|
||||
use Utopia\App;
|
||||
use Utopia\CLI\Console;
|
||||
use Utopia\DSN\DSN;
|
||||
use Utopia\Messaging\Adapter;
|
||||
use Utopia\Messaging\Adapters\SMS\Mock;
|
||||
|
||||
use Utopia\Messaging\Adapters\SMS as SMSAdapter;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
require_once __DIR__ . '/../init.php';
|
||||
|
||||
|
@ -20,7 +26,11 @@ Console::success(APP_NAME . ' messaging worker v1 has started' . "\n");
|
|||
|
||||
class MessagingV1 extends Worker
|
||||
{
|
||||
protected ?Adapter $sms = null;
|
||||
protected ?SMSAdapter $sms = null;
|
||||
protected ?PushAdapter $push = null;
|
||||
protected ?EmailAdapter $email = null;
|
||||
|
||||
|
||||
protected ?string $from = null;
|
||||
|
||||
public function getName(): string
|
||||
|
@ -28,51 +38,79 @@ class MessagingV1 extends Worker
|
|||
return "mails";
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
function push($record): ?PushAdapter
|
||||
{
|
||||
$credentials = $record->getAttribute('credentials');
|
||||
return match ($record->getAttribute('provider')) {
|
||||
'apns' => new APNS(
|
||||
$credentials['authKey'],
|
||||
$credentials['authKeyId'],
|
||||
$credentials['teamId'],
|
||||
$credentials['bundleId'],
|
||||
$credentials['endpoint']
|
||||
),
|
||||
'fcm' => new FCM($credentials['serverKey']),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
|
||||
public function email($record): ?EmailAdapter
|
||||
{
|
||||
$credentials = $record->getAttribute('credentials');
|
||||
return match ($record->getAttribute('provider')) {
|
||||
'mailgun' => new Mailgun($credentials['apiKey'], $credentials['domain']),
|
||||
'sendgrid' => new SendGrid($credentials['apiKey']),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$dsn = new DSN(App::getEnv('_APP_SMS_PROVIDER'));
|
||||
$user = $dsn->getUser();
|
||||
$secret = $dsn->getPassword();
|
||||
|
||||
$this->sms = match ($dsn->getHost()) {
|
||||
'mock' => new Mock($user, $secret), // used for tests
|
||||
'twilio' => new Twilio($user, $secret),
|
||||
'text-magic' => new TextMagic($user, $secret),
|
||||
'telesign' => new Telesign($user, $secret),
|
||||
'msg91' => new Msg91($user, $secret),
|
||||
'vonage' => new Vonage($user, $secret),
|
||||
default => null
|
||||
};
|
||||
|
||||
$this->from = App::getEnv('_APP_SMS_FROM');
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if (empty(App::getEnv('_APP_SMS_PROVIDER'))) {
|
||||
Console::info('Skipped sms processing. No Phone provider has been set.');
|
||||
return;
|
||||
}
|
||||
$providerId = $this->args['providerId'];
|
||||
$providerRecord =
|
||||
$this
|
||||
->getConsoleDB()
|
||||
->getDocument('providers', $providerId);
|
||||
|
||||
if (empty($this->from)) {
|
||||
Console::info('Skipped sms processing. No phone number has been set.');
|
||||
return;
|
||||
}
|
||||
$provider = match ($providerRecord->getAttribute('type')) {//stubbbbbbed.
|
||||
'sms' => $this->sms($providerRecord),
|
||||
'push' => $this->push($providerRecord),
|
||||
'email' => $this->email($providerRecord),
|
||||
default => null
|
||||
};
|
||||
|
||||
$message = new SMS(
|
||||
to: [$this->args['recipient']],
|
||||
content: $this->args['message'],
|
||||
from: $this->from,
|
||||
);
|
||||
// Query for the provider
|
||||
// switch on provider name
|
||||
// call function passing needed credentials returns required provider.
|
||||
|
||||
try {
|
||||
$this->sms->send($message);
|
||||
} catch (\Exception $error) {
|
||||
throw new Exception('Error sending message: ' . $error->getMessage(), 500);
|
||||
}
|
||||
$messageId = $this->args['messageId'];
|
||||
$message =
|
||||
$this
|
||||
->getConsoleDB()
|
||||
->getDocument('messages', $messageId);
|
||||
|
||||
// Contrust Message Object according to each provider type.
|
||||
// Send the message using respective adapter
|
||||
}
|
||||
|
||||
public function shutdown(): void
|
||||
function shutdown(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,13 +50,13 @@
|
|||
"utopia-php/cli": "0.15.*",
|
||||
"utopia-php/config": "0.2.*",
|
||||
"utopia-php/database": "0.42.*",
|
||||
"utopia-php/domains": "1.1.*",
|
||||
"utopia-php/domains": "0.3.*",
|
||||
"utopia-php/dsn": "0.1.*",
|
||||
"utopia-php/framework": "0.28.*",
|
||||
"utopia-php/image": "0.5.*",
|
||||
"utopia-php/locale": "0.4.*",
|
||||
"utopia-php/logger": "0.3.*",
|
||||
"utopia-php/messaging": "0.1.*",
|
||||
"utopia-php/messaging": "dev-feat-push as 0.1.1",
|
||||
"utopia-php/orchestration": "0.9.*",
|
||||
"utopia-php/platform": "0.4.*",
|
||||
"utopia-php/pools": "0.4.*",
|
||||
|
|
67
composer.lock
generated
67
composer.lock
generated
|
@ -4,7 +4,7 @@
|
|||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "2098172fc4b71eb0d41dcdbfea2f5061",
|
||||
"content-hash": "d599591131c16b547bb7ad8c83a35482",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adhocore/jwt",
|
||||
|
@ -1557,16 +1557,16 @@
|
|||
},
|
||||
{
|
||||
"name": "utopia-php/database",
|
||||
"version": "0.42.1",
|
||||
"version": "0.42.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/database.git",
|
||||
"reference": "9ff69a9b9eadc581771798833d423829c9d8cc90"
|
||||
"reference": "bc5ceb30c85fb685b0b5704d2f74886d813ebd41"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/database/zipball/9ff69a9b9eadc581771798833d423829c9d8cc90",
|
||||
"reference": "9ff69a9b9eadc581771798833d423829c9d8cc90",
|
||||
"url": "https://api.github.com/repos/utopia-php/database/zipball/bc5ceb30c85fb685b0b5704d2f74886d813ebd41",
|
||||
"reference": "bc5ceb30c85fb685b0b5704d2f74886d813ebd41",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1607,29 +1607,31 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/database/issues",
|
||||
"source": "https://github.com/utopia-php/database/tree/0.42.1"
|
||||
"source": "https://github.com/utopia-php/database/tree/0.42.2"
|
||||
},
|
||||
"time": "2023-08-14T16:09:09+00:00"
|
||||
"time": "2023-08-17T19:04:37+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/domains",
|
||||
"version": "v1.1.0",
|
||||
"version": "0.3.2",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/domains.git",
|
||||
"reference": "1665e1d9932afa3be63b5c1e0dcfe01fe77d8e73"
|
||||
"reference": "aaa8c9a96c69ccb397997b1f4f2299c66f77eefb"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/domains/zipball/1665e1d9932afa3be63b5c1e0dcfe01fe77d8e73",
|
||||
"reference": "1665e1d9932afa3be63b5c1e0dcfe01fe77d8e73",
|
||||
"url": "https://api.github.com/repos/utopia-php/domains/zipball/aaa8c9a96c69ccb397997b1f4f2299c66f77eefb",
|
||||
"reference": "aaa8c9a96c69ccb397997b1f4f2299c66f77eefb",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.1"
|
||||
"php": ">=8.0",
|
||||
"utopia-php/framework": "0.*.*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^7.0"
|
||||
"laravel/pint": "1.2.*",
|
||||
"phpunit/phpunit": "^9.3"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
|
@ -1645,6 +1647,10 @@
|
|||
{
|
||||
"name": "Eldad Fux",
|
||||
"email": "eldad@appwrite.io"
|
||||
},
|
||||
{
|
||||
"name": "Wess Cope",
|
||||
"email": "wess@appwrite.io"
|
||||
}
|
||||
],
|
||||
"description": "Utopia Domains library is simple and lite library for parsing web domains. This library is aiming to be as simple and easy to learn and use.",
|
||||
|
@ -1661,9 +1667,9 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/domains/issues",
|
||||
"source": "https://github.com/utopia-php/domains/tree/master"
|
||||
"source": "https://github.com/utopia-php/domains/tree/0.3.2"
|
||||
},
|
||||
"time": "2020-02-23T07:40:02+00:00"
|
||||
"time": "2023-07-19T16:39:24+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/dsn",
|
||||
|
@ -1915,16 +1921,16 @@
|
|||
},
|
||||
{
|
||||
"name": "utopia-php/messaging",
|
||||
"version": "0.1.1",
|
||||
"version": "dev-feat-push",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/utopia-php/messaging.git",
|
||||
"reference": "a75d66ddd59b834ab500a4878a2c084e6572604a"
|
||||
"reference": "5f85757316eb842e9169d318e234124ff252c404"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/utopia-php/messaging/zipball/a75d66ddd59b834ab500a4878a2c084e6572604a",
|
||||
"reference": "a75d66ddd59b834ab500a4878a2c084e6572604a",
|
||||
"url": "https://api.github.com/repos/utopia-php/messaging/zipball/5f85757316eb842e9169d318e234124ff252c404",
|
||||
"reference": "5f85757316eb842e9169d318e234124ff252c404",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
|
@ -1933,8 +1939,8 @@
|
|||
},
|
||||
"require-dev": {
|
||||
"laravel/pint": "^1.2",
|
||||
"phpmailer/phpmailer": "6.6.*",
|
||||
"phpunit/phpunit": "9.5.*"
|
||||
"phpmailer/phpmailer": "^6.8",
|
||||
"phpunit/phpunit": "^9.6"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
|
@ -1957,9 +1963,9 @@
|
|||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/utopia-php/messaging/issues",
|
||||
"source": "https://github.com/utopia-php/messaging/tree/0.1.1"
|
||||
"source": "https://github.com/utopia-php/messaging/tree/feat-push"
|
||||
},
|
||||
"time": "2023-02-07T05:42:46+00:00"
|
||||
"time": "2023-08-14T20:35:31+00:00"
|
||||
},
|
||||
{
|
||||
"name": "utopia-php/migration",
|
||||
|
@ -5351,9 +5357,18 @@
|
|||
"time": "2023-07-26T07:16:09+00:00"
|
||||
}
|
||||
],
|
||||
"aliases": [],
|
||||
"aliases": [
|
||||
{
|
||||
"package": "utopia-php/messaging",
|
||||
"version": "dev-feat-push",
|
||||
"alias": "0.1.1",
|
||||
"alias_normalized": "0.1.1.0"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"stability-flags": [],
|
||||
"stability-flags": {
|
||||
"utopia-php/messaging": 20
|
||||
},
|
||||
"prefer-stable": false,
|
||||
"prefer-lowest": false,
|
||||
"platform": {
|
||||
|
@ -5377,5 +5392,5 @@
|
|||
"platform-overrides": {
|
||||
"php": "8.0"
|
||||
},
|
||||
"plugin-api-version": "2.2.0"
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue