Compare commits
3 Commits
4fe93cac7c
...
11801add49
| Author | SHA1 | Date | |
|---|---|---|---|
| 11801add49 | |||
| 6d7bce6a6f | |||
| 49621f1518 |
45
bot.js
45
bot.js
@ -40,11 +40,21 @@ class TelegramHistoryBot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
getUniqueChatIds() {
|
getUniqueChatIds() {
|
||||||
const ids = new Set();
|
const chatMap = new Map();
|
||||||
|
|
||||||
for (const msg of this.history) {
|
for (const msg of this.history) {
|
||||||
ids.add(msg.chat_id);
|
const current = chatMap.get(msg.chat_id);
|
||||||
|
if (!current || new Date(msg.timestamp) > new Date(current.timestamp)) {
|
||||||
|
chatMap.set(msg.chat_id, {
|
||||||
|
chat_id: msg.chat_id,
|
||||||
|
title: msg.chat_title || `(${msg.chat_id})`,
|
||||||
|
timestamp: msg.timestamp
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Array.from(ids);
|
|
||||||
|
// Преобразуем Map в массив, возвращаем только chat_id и title
|
||||||
|
return Array.from(chatMap.values()).map(({ chat_id, title }) => ({ chat_id, title }))
|
||||||
}
|
}
|
||||||
|
|
||||||
async init() {
|
async init() {
|
||||||
@ -139,9 +149,34 @@ class TelegramHistoryBot {
|
|||||||
logger.warn('Попытка вызова списка чатов из стороннего чата. Пользователь ' + JSON.stringify(ctx.message.from))
|
logger.warn('Попытка вызова списка чатов из стороннего чата. Пользователь ' + JSON.stringify(ctx.message.from))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const chatIds = this.getUniqueChatIds();
|
|
||||||
await ctx.reply(JSON.stringify(chatIds))
|
const message = this.getUniqueChatIds()
|
||||||
|
.map(chat => `• ${chat.title} (ID: ${chat.chat_id})`)
|
||||||
|
.join('\n');
|
||||||
|
await ctx.reply(message)
|
||||||
});
|
});
|
||||||
|
this.bot.command('broadcast', async (ctx) => {
|
||||||
|
if (ctx.message.chat.id!==Number(process.env.ADMIN_CHAT_ID)) {
|
||||||
|
logger.warn(JSON.stringify(ctx.message))
|
||||||
|
logger.warn('Попытка вызова бродкаста из стороннего чата. Пользователь ' + JSON.stringify(ctx.message.from))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const fullText = ctx.message.text || '';
|
||||||
|
const text = fullText.replace(/^\/broadcast\s*/, '').trim();
|
||||||
|
if (!text) {
|
||||||
|
return ctx.reply('❗️ Пожалуйста, укажите текст для рассылки после команды /broadcast');
|
||||||
|
}
|
||||||
|
logger.info('📊 Получена команда /broadcast с запросом:' + text)
|
||||||
|
const chatList= this.getUniqueChatIds()
|
||||||
|
for (const chat of chatList) {
|
||||||
|
try {
|
||||||
|
await this.bot.telegram.sendMessage(chat.chat_id, text);
|
||||||
|
logger.info(`✅ Сообщение отправлено в чат ${chat.title} (${chat.chat_id})`);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`❌ Ошибка при отправке в чат ${chat.title} (${chat.chat_id}):`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
// Команды суммаризации - должны быть ДО обработки обычных сообщений
|
// Команды суммаризации - должны быть ДО обработки обычных сообщений
|
||||||
// this.bot.command('summary_day', async (ctx) => {
|
// this.bot.command('summary_day', async (ctx) => {
|
||||||
// // if (!this.isAdmin(ctx)) {
|
// // if (!this.isAdmin(ctx)) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user