mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
init
This commit is contained in:
16
app/NotificationChannels/AbstractProvider.php
Normal file
16
app/NotificationChannels/AbstractProvider.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\NotificationChannels;
|
||||
|
||||
use App\Contracts\NotificationChannel as NotificationChannelContract;
|
||||
use App\Models\NotificationChannel;
|
||||
|
||||
abstract class AbstractProvider implements NotificationChannelContract
|
||||
{
|
||||
protected NotificationChannel $notificationChannel;
|
||||
|
||||
public function __construct(NotificationChannel $notificationChannel)
|
||||
{
|
||||
$this->notificationChannel = $notificationChannel;
|
||||
}
|
||||
}
|
58
app/NotificationChannels/Discord.php
Normal file
58
app/NotificationChannels/Discord.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\NotificationChannels;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class Discord extends AbstractProvider
|
||||
{
|
||||
public function validationRules(): array
|
||||
{
|
||||
return [
|
||||
'webhook_url' => 'required|url',
|
||||
];
|
||||
}
|
||||
|
||||
public function data(array $input): array
|
||||
{
|
||||
return [
|
||||
'webhook_url' => $input['webhook_url'],
|
||||
];
|
||||
}
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
$connect = $this->checkConnection(
|
||||
__('Congratulations! 🎉'),
|
||||
__("You've connected your Discord to Vito")."\n".
|
||||
__('Manage your notification channels')."\n".
|
||||
route('notification-channels')
|
||||
);
|
||||
|
||||
if (! $connect) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendMessage(string $subject, string $text): void
|
||||
{
|
||||
dispatch(function () use ($subject, $text) {
|
||||
$data = $this->notificationChannel->data;
|
||||
Http::post($data['webhook_url'], [
|
||||
'content' => '*'.$subject.'*'."\n".$text,
|
||||
]);
|
||||
})->onQueue('default');
|
||||
}
|
||||
|
||||
private function checkConnection(string $subject, string $text): bool
|
||||
{
|
||||
$data = $this->notificationChannel->data;
|
||||
$connect = Http::post($data['webhook_url'], [
|
||||
'content' => '*'.$subject.'*'."\n".$text,
|
||||
]);
|
||||
|
||||
return $connect->ok();
|
||||
}
|
||||
}
|
37
app/NotificationChannels/Email.php
Normal file
37
app/NotificationChannels/Email.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\NotificationChannels;
|
||||
|
||||
use App\Mail\NotificationChannelMessage;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class Email extends AbstractProvider
|
||||
{
|
||||
public function validationRules(): array
|
||||
{
|
||||
return [
|
||||
'email' => 'required|email',
|
||||
];
|
||||
}
|
||||
|
||||
public function data(array $input): array
|
||||
{
|
||||
return [
|
||||
'email' => $input['email'],
|
||||
];
|
||||
}
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
$this->notificationChannel->connected = true;
|
||||
$this->notificationChannel->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendMessage(string $subject, mixed $text): void
|
||||
{
|
||||
$data = $this->notificationChannel->data;
|
||||
Mail::to($data['email'])->send(new NotificationChannelMessage($subject, $text));
|
||||
}
|
||||
}
|
58
app/NotificationChannels/Slack.php
Normal file
58
app/NotificationChannels/Slack.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\NotificationChannels;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class Slack extends AbstractProvider
|
||||
{
|
||||
public function validationRules(): array
|
||||
{
|
||||
return [
|
||||
'webhook_url' => 'required|url',
|
||||
];
|
||||
}
|
||||
|
||||
public function data(array $input): array
|
||||
{
|
||||
return [
|
||||
'webhook_url' => $input['webhook_url'],
|
||||
];
|
||||
}
|
||||
|
||||
public function connect(): bool
|
||||
{
|
||||
$connect = $this->checkConnection(
|
||||
__('Congratulations! 🎉'),
|
||||
__("You've connected your Slack to Vito")."\n".
|
||||
__('Manage your notification channels')."\n".
|
||||
route('notification-channels')
|
||||
);
|
||||
|
||||
if (! $connect) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function sendMessage(string $subject, string $text): void
|
||||
{
|
||||
dispatch(function () use ($subject, $text) {
|
||||
$data = $this->notificationChannel->data;
|
||||
Http::post($data['webhook_url'], [
|
||||
'text' => '*'.$subject.'*'."\n".$text,
|
||||
]);
|
||||
})->onQueue('default');
|
||||
}
|
||||
|
||||
private function checkConnection(string $subject, string $text): bool
|
||||
{
|
||||
$data = $this->notificationChannel->data;
|
||||
$connect = Http::post($data['webhook_url'], [
|
||||
'text' => '*'.$subject.'*'."\n".$text,
|
||||
]);
|
||||
|
||||
return $connect->ok();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user