Добавить scripts/bot.js
This commit is contained in:
parent
eecac3b86f
commit
0d946df109
132
scripts/bot.js
Normal file
132
scripts/bot.js
Normal file
@ -0,0 +1,132 @@
|
||||
const TelegramBot = require("node-telegram-bot-api")
|
||||
const { getModules, addModule, findModules } = require("../lib/module-store")
|
||||
const { isAdmin } = require("../lib/auth")
|
||||
|
||||
const token = "123456"// <-- ЗАМЕНИТЕ ЭТО!
|
||||
|
||||
|
||||
if (token === "123456" || !token) {
|
||||
console.error("Пожалуйста, замените '123456' на реальный токен вашего бота в scripts/bot.js.")
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const bot = new TelegramBot(token, { polling: true })
|
||||
|
||||
console.log("Telegram bot started...")
|
||||
|
||||
bot.onText(/\/start/, (msg) => {
|
||||
const chatId = msg.chat.id
|
||||
bot.sendMessage(
|
||||
chatId,
|
||||
"Привет! Я бот для управления модулями Exteragram. Используйте /search для поиска модулей или /admin для доступа к админ-панели (если вы администратор).",
|
||||
)
|
||||
})
|
||||
|
||||
bot.onText(/\/admin/, (msg) => {
|
||||
const chatId = msg.chat.id
|
||||
if (isAdmin(msg.from.id)) {
|
||||
bot.sendMessage(
|
||||
chatId,
|
||||
"Добро пожаловать в админ-панель! Отправьте мне файл модуля, чтобы добавить его. Вы также можете переслать мне сообщение с файлом из другого канала.",
|
||||
)
|
||||
} else {
|
||||
bot.sendMessage(chatId, "У вас нет прав администратора.")
|
||||
}
|
||||
})
|
||||
|
||||
bot.onText(/\/search/, (msg) => {
|
||||
const chatId = msg.chat.id
|
||||
bot.sendMessage(chatId, "Введите название модуля для поиска:")
|
||||
})
|
||||
|
||||
bot.on("message", async (msg) => {
|
||||
const chatId = msg.chat.id
|
||||
const text = msg.text
|
||||
|
||||
if (text && !text.startsWith("/") && !msg.document && !msg.forward_from_chat) {
|
||||
const searchResults = findModules(text)
|
||||
if (searchResults.length > 0) {
|
||||
const inlineKeyboard = searchResults.map((module) => [
|
||||
{ text: module.name, callback_data: `send_module_${module.id}` },
|
||||
])
|
||||
bot.sendMessage(chatId, "Найденные модули:", {
|
||||
reply_markup: {
|
||||
inline_keyboard: inlineKeyboard,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
bot.sendMessage(chatId, "Модули по вашему запросу не найдены.")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
bot.on("document", async (msg) => {
|
||||
const chatId = msg.chat.id
|
||||
const document = msg.document
|
||||
|
||||
if (isAdmin(msg.from.id)) {
|
||||
const moduleName = document.file_name.replace(/\.exteragram_module$/, "")
|
||||
const fileId = document.file_id
|
||||
const sourceChannel = msg.forward_from_chat ? msg.forward_from_chat.title : "Direct Upload"
|
||||
|
||||
addModule({ name: moduleName, file_id: fileId, source_channel: sourceChannel })
|
||||
bot.sendMessage(chatId, `Модуль "${moduleName}" успешно добавлен!`)
|
||||
} else {
|
||||
bot.sendMessage(chatId, "У вас нет прав для загрузки модулей.")
|
||||
}
|
||||
})
|
||||
|
||||
bot.on("forward", async (msg) => {
|
||||
const chatId = msg.chat.id
|
||||
if (isAdmin(msg.from.id) && msg.document) {
|
||||
const document = msg.document
|
||||
const moduleName = document.file_name.replace(/\.exteragram_module$/, "")
|
||||
const fileId = document.file_id
|
||||
const sourceChannel = msg.forward_from_chat ? msg.forward_from_chat.title : "Unknown Channel"
|
||||
|
||||
addModule({ name: moduleName, file_id: fileId, source_channel: sourceChannel })
|
||||
bot.sendMessage(chatId, `Модуль "${moduleName}" из канала "${sourceChannel}" успешно добавлен!`)
|
||||
} else if (isAdmin(msg.from.id) && !msg.document) {
|
||||
bot.sendMessage(chatId, "Пересланное сообщение не содержит документа.")
|
||||
}
|
||||
})
|
||||
|
||||
bot.on("inline_query", async (inlineQuery) => {
|
||||
const query = inlineQuery.query
|
||||
const searchResults = findModules(query)
|
||||
|
||||
const results = searchResults.map((module) => ({
|
||||
type: "document",
|
||||
id: module.id,
|
||||
title: module.name,
|
||||
document_file_id: module.file_id,
|
||||
caption: `Модуль: ${module.name}\nИсточник: ${module.source_channel}`,
|
||||
}))
|
||||
|
||||
bot.answerInlineQuery(inlineQuery.id, results, { cache_time: 0 })
|
||||
})
|
||||
|
||||
bot.on("callback_query", async (callbackQuery) => {
|
||||
const message = callbackQuery.message
|
||||
const data = callbackQuery.data
|
||||
const chatId = message.chat.id
|
||||
|
||||
if (data.startsWith("send_module_")) {
|
||||
const moduleId = data.replace("send_module_", "")
|
||||
const modules = getModules()
|
||||
const moduleToSend = modules.find((m) => m.id === moduleId)
|
||||
|
||||
if (moduleToSend) {
|
||||
bot.sendDocument(chatId, moduleToSend.file_id, {
|
||||
caption: `Вот ваш модуль: ${moduleToSend.name}\nИсточник: ${moduleToSend.source_channel}`,
|
||||
})
|
||||
bot.answerCallbackQuery(callbackQuery.id, { text: "Модуль отправлен!" })
|
||||
} else {
|
||||
bot.answerCallbackQuery(callbackQuery.id, { text: "Модуль не найден.", show_alert: true })
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
bot.on("polling_error", (error) => {
|
||||
console.error("Polling error:", error)
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user