Додані повідомлення та перепрацьована структура застосунку та api

This commit is contained in:
2026-03-15 00:25:10 +02:00
parent 85483b85bb
commit 4bc9c11512
101 changed files with 5763 additions and 2546 deletions

View File

@@ -1,5 +1,9 @@
const db = require("../config/db");
const webpush = require('web-push');
const TelegramBot = require("node-telegram-bot-api");
const util = require('util');
const dbRun = util.promisify(db.run).bind(db);
const VAPID_PUBLIC_KEY = process.env.VAPID_PUBLIC_KEY;
const VAPID_PRIVATE_KEY = process.env.VAPID_PRIVATE_KEY;
@@ -10,6 +14,11 @@ webpush.setVapidDetails(
VAPID_PRIVATE_KEY
);
const TOKEN = process.env.TELEGRAM_TOKEN;
const STAND_CHAT_ID = process.env.STAND_CHAT_ID;
const bot = new TelegramBot(TOKEN, { polling: false });
class Notification {
async sendSheep({ sheep_id, title, body, page }) {
const sql = `
@@ -123,7 +132,7 @@ class Notification {
}
if (!rows.length) {
console.log(`🐑 No subscriptions found for sheep_id: ${sheep_id}`);
console.log(`🐑 No subscriptions`);
return;
}
@@ -146,6 +155,25 @@ class Notification {
const failed = results.filter(r => r.status === 'rejected').length;
console.log(`✅ Sent: ${rows.length - failed}, ❌ Failed: ${failed}`);
});
// Формуємо повне повідомлення
const fullMessage = `📢 <b>${title}</b>\n\n${body.replace('«', '«<b>').replace('»', '</b>»')}`;
try {
const sentMessage = await bot.sendMessage(STAND_CHAT_ID, fullMessage, {
parse_mode: 'HTML'
});
// Зберігаємо ID нового повідомлення у базі
await dbRun(
`INSERT INTO sent_messages (last_message_id, created_at) VALUES (?, ?)`, [sentMessage.message_id, Date.now()]
);
console.log(`✅ Сповіщення надіслано для стенду: ${stand.title}`);
} catch (err) {
console.error('❌ Помилка відправки тексту:', err.message);
}
}
};