const express = require('express'); const router = express.Router(); const { sendPhotoToUser, sendPhotosToUser } = require('../bot'); const { authenticate } = require('../middleware/auth'); // Endpoint для отправки одного фото в ЛС router.post('/send-photo', authenticate, async (req, res) => { try { const { userId, photoUrl, caption } = req.body; if (!userId || !photoUrl) { return res.status(400).json({ error: 'userId и photoUrl обязательны' }); } const result = await sendPhotoToUser(userId, photoUrl, caption); res.json({ success: true, message: 'Изображение отправлено в ваш Telegram', result }); } catch (error) { console.error('Ошибка отправки:', error); res.status(500).json({ error: 'Ошибка отправки изображения', details: error.message }); } }); // Endpoint для отправки нескольких фото группой router.post('/send-photos', authenticate, async (req, res) => { try { const { userId, photos } = req.body; if (!userId || !photos || !Array.isArray(photos) || photos.length === 0) { return res.status(400).json({ error: 'userId и массив photos обязательны' }); } if (photos.length > 50) { return res.status(400).json({ error: 'Максимум 50 фото за раз' }); } const results = await sendPhotosToUser(userId, photos); res.json({ success: true, message: `${photos.length} изображений отправлено в ваш Telegram`, results }); } catch (error) { console.error('Ошибка отправки фото:', error); res.status(500).json({ error: 'Ошибка отправки изображений', details: error.message }); } }); // Отправить сообщение всем пользователям (только админы) router.post('/broadcast', authenticate, async (req, res) => { try { // Проверка прав админа if (req.user.role !== 'admin') { return res.status(403).json({ error: 'Требуются права администратора' }); } const { message } = req.body; if (!message || !message.trim()) { return res.status(400).json({ error: 'Сообщение обязательно' }); } const { sendMessageToAllUsers } = require('../bots/mainBot'); const result = await sendMessageToAllUsers(message); res.json({ success: true, message: `Сообщение отправлено ${result.sent} пользователям`, result }); } catch (error) { console.error('Ошибка рассылки:', error); res.status(500).json({ error: 'Ошибка отправки сообщений', details: error.message }); } }); module.exports = router;