Files
pricebuddy/app/Notifications/Channels/TelegramChannel.php
JeremyandClaude Opus 4.8 0ebeb532e1 feat: add Telegram, Discord and ntfy notification channels
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>
2026-06-03 08:07:58 +10:00

103 lines
2.9 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 via a Telegram bot.
*
* The bot token is configured once at the app level (it is shared by every
* user), while each user provides their own chat id so messages are routed to
* their personal Telegram chat. This mirrors the Pushover pattern (global app
* token + per-user user key).
*
* @see https://core.telegram.org/bots/api#sendmessage
*/
class TelegramChannel
{
/**
* @param User $notifiable
*/
public function send($notifiable, Notification $notification): void
{
if (! method_exists($notification, 'toTelegram')) {
return;
}
$message = $notification->toTelegram($notifiable);
if (! $message instanceof GenericNotificationMessage) {
return;
}
$settings = self::getSettings($notifiable);
// Without a bot token (app level) and chat id (per user) there is
// nowhere to deliver the message, so skip silently.
if (empty($settings['bot_token']) || empty($settings['chat_id'])) {
return;
}
$response = self::sendRequest(
self::makeUrl($settings['bot_token']),
$settings['chat_id'],
$message->title,
$message->content,
$message->url
);
$response->throw();
}
/**
* @param User $notifiable
*/
public static function getSettings($notifiable): array
{
$settings = NotificationsHelper::getSettings(NotificationMethods::Telegram);
$settings['chat_id'] = $notifiable->getNotificationSettings(NotificationMethods::Telegram, 'chat_id', '');
return $settings;
}
public static function makeUrl(string $botToken): string
{
return 'https://api.telegram.org/bot'.$botToken.'/sendMessage';
}
public static function sendRequest(string $apiUrl, string $chatId, string $title, string $message, string $url = ''): Response
{
return Http::post($apiUrl, [
'chat_id' => $chatId,
'text' => self::formatText($title, $message, $url),
'parse_mode' => 'HTML',
'disable_web_page_preview' => false,
]);
}
/**
* Build the HTML message body Telegram will render.
*/
public static function formatText(string $title, string $message, string $url = ''): string
{
$text = '<b>'.e($title).'</b>';
if ($message !== '') {
$text .= "\n\n".e($message);
}
if ($url !== '') {
$text .= "\n\n".'<a href="'.e($url).'">'.__('View product').'</a>';
}
return $text;
}
}