mirror of
https://github.com/jez500/pricebuddy.git
synced 2026-11-24 19:58:13 +01:00
Adds three new push notification channels to reach feature parity with PriceGhost, following the existing channel pattern (app-level service config + per-user routing target): - Telegram: shared bot token (app) + per-user chat id, HTML messages - Discord: rich embed via webhook, app-level default + per-user override - ntfy: app-level server/auth (defaults to ntfy.sh) + per-user topic, optional basic auth for self-hosted servers Each channel is wired into NotificationMethods, exposes its config in the app settings and per-user profile forms, and Telegram/Discord get a Test action. Full unit coverage for settings merging, send payloads, routing guards and per-channel enable/disable. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
90 lines
2.4 KiB
PHP
90 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Notifications\Channels;
|
|
|
|
use App\Enums\NotificationMethods;
|
|
use App\Models\User;
|
|
use App\Notifications\Messages\GenericNotificationMessage;
|
|
use App\Services\Helpers\NotificationsHelper;
|
|
use Illuminate\Http\Client\Response;
|
|
use Illuminate\Notifications\Notification;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
/**
|
|
* Sends notifications to a Discord channel via an incoming webhook.
|
|
*
|
|
* A webhook url points at a single Discord channel, so it doubles as the
|
|
* routing target. An optional default webhook can be set at the app level and
|
|
* each user may override it with their own (mirrors the Apprise token-override
|
|
* pattern).
|
|
*
|
|
* @see https://discord.com/developers/docs/resources/webhook#execute-webhook
|
|
*/
|
|
class DiscordChannel
|
|
{
|
|
/**
|
|
* Green embed colour used for price-drop alerts.
|
|
*/
|
|
public const COLOR_PRICE_DROP = 0x57F287;
|
|
|
|
/**
|
|
* @param User $notifiable
|
|
*/
|
|
public function send($notifiable, Notification $notification): void
|
|
{
|
|
if (! method_exists($notification, 'toDiscord')) {
|
|
return;
|
|
}
|
|
|
|
$message = $notification->toDiscord($notifiable);
|
|
|
|
if (! $message instanceof GenericNotificationMessage) {
|
|
return;
|
|
}
|
|
|
|
$webhookUrl = self::getSettings($notifiable)['webhook_url'] ?? '';
|
|
|
|
if (empty($webhookUrl)) {
|
|
return;
|
|
}
|
|
|
|
$response = self::sendRequest(
|
|
$webhookUrl,
|
|
$message->title,
|
|
$message->content,
|
|
$message->url
|
|
);
|
|
|
|
$response->throw();
|
|
}
|
|
|
|
/**
|
|
* @param User $notifiable
|
|
*/
|
|
public static function getSettings($notifiable): array
|
|
{
|
|
$settings = NotificationsHelper::getSettings(NotificationMethods::Discord);
|
|
$userWebhook = $notifiable->getNotificationSettings(NotificationMethods::Discord, 'webhook_url', '');
|
|
$settings['webhook_url'] = empty($userWebhook) ? ($settings['webhook_url'] ?? '') : $userWebhook;
|
|
|
|
return $settings;
|
|
}
|
|
|
|
public static function sendRequest(string $webhookUrl, string $title, string $message, string $url = '', int $color = self::COLOR_PRICE_DROP): Response
|
|
{
|
|
$embed = [
|
|
'title' => $title,
|
|
'description' => $message,
|
|
'color' => $color,
|
|
];
|
|
|
|
if ($url !== '') {
|
|
$embed['url'] = $url;
|
|
}
|
|
|
|
return Http::post($webhookUrl, [
|
|
'embeds' => [$embed],
|
|
]);
|
|
}
|
|
}
|