v0.1.10
This commit is contained in:
26
kernel/modules/notification/channels/DatabaseChannel.php
Normal file
26
kernel/modules/notification/channels/DatabaseChannel.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\notification\channels;
|
||||
|
||||
use kernel\Flash;
|
||||
use kernel\modules\notification\contracts\NotificationChannelInterface;
|
||||
use kernel\modules\notification\contracts\NotificationMessage;
|
||||
use kernel\modules\notification\models\User;
|
||||
|
||||
class DatabaseChannel implements NotificationChannelInterface
|
||||
{
|
||||
public function send(NotificationMessage $notification, User $user): bool
|
||||
{
|
||||
try {
|
||||
$user->notifications()->create([
|
||||
'message' => $notification->getMessage(),
|
||||
'data' => $notification->toArray()
|
||||
]);
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Flash::setMessage("error", $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
34
kernel/modules/notification/channels/EmailChannel.php
Normal file
34
kernel/modules/notification/channels/EmailChannel.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\notification\channels;
|
||||
|
||||
use kernel\helpers\SMTP;
|
||||
use kernel\modules\notification\contracts\NotificationChannelInterface;
|
||||
use kernel\modules\notification\contracts\NotificationMessage;
|
||||
use kernel\modules\user\models\User;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
class EmailChannel implements NotificationChannelInterface
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function send(NotificationMessage $notification, User $user): bool
|
||||
{
|
||||
$smtp = new SMTP();
|
||||
// Здесь можно использовать Laravel Mail
|
||||
// \Illuminate\Support\Facades\Mail::to($user->email)
|
||||
// ->send(new \App\Mail\NotificationMail(
|
||||
// $notification->getSubject(),
|
||||
// $notification->getMessage()
|
||||
// ));
|
||||
|
||||
return $smtp->send_html([
|
||||
'address' => $user->email,
|
||||
'subject' => $notification->getSubject(),
|
||||
'body' => $notification->getMessage(),
|
||||
'from_name' => $_ENV['MAIL_SMTP_USERNAME'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
22
kernel/modules/notification/channels/SmsChannel.php
Normal file
22
kernel/modules/notification/channels/SmsChannel.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\notification\channels;
|
||||
|
||||
use kernel\modules\notification\contracts\NotificationChannelInterface;
|
||||
use kernel\modules\notification\contracts\NotificationMessage;
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
class SmsChannel implements NotificationChannelInterface
|
||||
{
|
||||
public function send(NotificationMessage $notification, User $user): bool
|
||||
{
|
||||
if (empty($user->phone)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Интеграция с SMS-сервисом
|
||||
//$smsService = new \App\Services\SmsService();
|
||||
//return $smsService->send($user->phone, $notification->getMessage());
|
||||
return true;
|
||||
}
|
||||
}
|
42
kernel/modules/notification/channels/TelegramChannel.php
Normal file
42
kernel/modules/notification/channels/TelegramChannel.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\notification\channels;
|
||||
|
||||
use kernel\Flash;
|
||||
use kernel\modules\notification\contracts\NotificationChannelInterface;
|
||||
use kernel\modules\notification\contracts\NotificationMessage;
|
||||
use kernel\modules\notification\models\User;
|
||||
|
||||
class TelegramChannel implements NotificationChannelInterface
|
||||
{
|
||||
protected string $botToken;
|
||||
|
||||
public function __construct(string $botToken)
|
||||
{
|
||||
$this->botToken = $botToken;
|
||||
}
|
||||
|
||||
public function send(NotificationMessage $notification, User $user): bool
|
||||
{
|
||||
if (empty($user->telegram_chat_id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$httpClient = new \GuzzleHttp\Client();
|
||||
|
||||
try {
|
||||
$response = $httpClient->post("https://api.telegram.org/bot{$this->botToken}/sendMessage", [
|
||||
'form_params' => [
|
||||
'chat_id' => $user->telegram_chat_id,
|
||||
'text' => $notification->getMessage(),
|
||||
'parse_mode' => 'HTML'
|
||||
]
|
||||
]);
|
||||
|
||||
return $response->getStatusCode() === 200;
|
||||
} catch (\Exception $e) {
|
||||
Flash::setMessage("error", $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user