|
1 | 1 | import TelegramBot from 'node-telegram-bot-api';
|
2 |
| - |
3 |
| -const token = process.env.TELEGRAM_TOKEN || ''; |
4 |
| -const chatId = process.env.TELEGRAM_CHAT_ID || ''; |
5 |
| -const disableNotification = !!parseInt(process.env.TELEGRAM_SEND_SILENTLY || '0', 10); |
6 |
| -const bot = new TelegramBot(token); |
| 2 | +import { prisma } from '../db/client'; |
7 | 3 |
|
8 | 4 | export const sendNotification = async (title: string, body: string, url?: string) => {
|
9 |
| - if (!token || !chatId) { |
10 |
| - return; |
11 |
| - } |
| 5 | + const settings = await prisma.settings.findFirstOrThrow(); |
12 | 6 |
|
13 |
| - const message = ` |
| 7 | + if (settings.telegramEnabled && settings.telegramChatId && settings.telegramToken) { |
| 8 | + const bot = new TelegramBot(settings.telegramToken); |
| 9 | + const message = ` |
14 | 10 | <b>${title}</b>
|
15 | 11 |
|
16 | 12 | ${body}
|
17 | 13 |
|
18 | 14 | ${url || ''}
|
19 |
| - `; |
20 |
| - await bot.sendMessage(chatId, message, { parse_mode: 'HTML', disable_notification: disableNotification }); |
| 15 | + `; |
| 16 | + await bot.sendMessage(settings.telegramChatId, message, { |
| 17 | + parse_mode: 'HTML', |
| 18 | + disable_notification: settings.telegramSendSilently, |
| 19 | + }); |
| 20 | + } |
| 21 | + |
| 22 | + if (settings.appriseEnabled && settings.appriseHost && settings.appriseUrls.length !== 0) { |
| 23 | + const appriseServiceUrl = new URL( |
| 24 | + '/notify', |
| 25 | + settings.appriseHost.toLowerCase().startsWith('http') ? settings.appriseHost : `http://${settings.appriseHost}`, |
| 26 | + ).href; |
| 27 | + await fetch(appriseServiceUrl, { |
| 28 | + body: JSON.stringify({ |
| 29 | + urls: settings.appriseUrls, |
| 30 | + title, |
| 31 | + body: `${body} ${url}`, |
| 32 | + format: 'html', |
| 33 | + }), |
| 34 | + headers: { |
| 35 | + 'Content-Type': 'application/json', |
| 36 | + }, |
| 37 | + method: 'POST', |
| 38 | + }); |
| 39 | + } |
21 | 40 | };
|
0 commit comments