68 lines
2.3 KiB
JavaScript
68 lines
2.3 KiB
JavaScript
// external-bot-daemon.js
|
|
import TelegramBot from 'node-telegram-bot-api'
|
|
import { PrismaClient } from '@prisma/client'
|
|
import dotenv from 'dotenv'
|
|
|
|
dotenv.config()
|
|
// ...твой код ниже
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
// Мапа для хранения всех активных клиентов
|
|
const bots = {}
|
|
|
|
async function main() {
|
|
// Получаем все внешние интерфейсы из базы
|
|
const externalBots = await prisma.externalBot.findMany({
|
|
include: { bots: { include: { commands: { include: { groupIds: true } }, groups: true } } }
|
|
})
|
|
|
|
// Для каждого интерфейса запускаем TelegramBot
|
|
for (const ext of externalBots) {
|
|
if (!ext.token) continue
|
|
// Если уже создан, не пересоздаем
|
|
if (bots[ext.token]) continue
|
|
console.log('ext.token',ext.token);
|
|
const bot = new TelegramBot(ext.token, { polling: true })
|
|
bots[ext.token] = bot
|
|
|
|
bot.on('message', async msg => {
|
|
// user id
|
|
const userId = msg.from.id.toString()
|
|
// Можно искать User по platform/ID (если надо)
|
|
|
|
// Получаем все боты и команды для этого интерфейса
|
|
const freshExt = await prisma.externalBot.findUnique({
|
|
where: { id: ext.id },
|
|
include: { bots: { include: { commands: { include: { groupIds: true } }, groups: true } } }
|
|
})
|
|
|
|
// Можно получить все команды и группы
|
|
const routing = freshExt.bots.map(b => ({
|
|
name: b.name,
|
|
commands: b.commands.map(cmd => ({
|
|
command: cmd.command,
|
|
description: cmd.description,
|
|
groups: cmd.groupIds.map(g => g.name)
|
|
})),
|
|
groups: b.groups.map(g => g.name)
|
|
}))
|
|
|
|
// Для теста просто отправь список команд в ответ
|
|
const text =
|
|
routing.map(b =>
|
|
`Бот: ${b.name}\nКоманды:\n` +
|
|
b.commands.map(c =>
|
|
` /${c.command} (${c.groups.join(', ')})${c.description ? ': ' + c.description : ''}`
|
|
).join('\n')
|
|
).join('\n\n')
|
|
bot.sendMessage(msg.chat.id, text || 'Нет доступных команд')
|
|
})
|
|
|
|
console.log(`Started polling for ExternalBot "${ext.name}"`)
|
|
}
|
|
}
|
|
|
|
main().catch(console.error)
|
|
|