From 8897fb632110f501e97235758d34a1d6e42bd8ea Mon Sep 17 00:00:00 2001 From: Stefan080106 Date: Sun, 16 Apr 2023 19:51:20 +0200 Subject: [PATCH] feat(main): telegram --- index.js | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 8f47ab7..5d28c4e 100644 --- a/index.js +++ b/index.js @@ -11,14 +11,41 @@ import TelegramBot from 'node-telegram-bot-api'; // replace the value below with the Telegram token you receive from @BotFather const token = '6150769224:AAGhBagPEi23QtBJl65YNuwEBBXwHguDV0E'; +const messageIDs = [1034133487] + // Create a bot that uses 'polling' to fetch new updates const bot = new TelegramBot(token, { polling: true }); +bot.setMyCommands([ + { command: '/chatid', description: 'Zeigt deine Chat ID' }, +]) + bot.on('message', (msg) => { const chatId = msg.chat.id; - msg.reply_to_message("Your Chat ID is: " + chatId); + + switch (msg.text) { + case '/chatid': + bot.sendMessage(chatId, 'Deine Chat ID ist: ' + chatId); + break; + default: + } + }); +function sendMessage(msg, idList) { + idList.forEach((id) => { + bot.sendMessage(id, msg); + }) +} + +// setInterval(() => { +// messageIDs.forEach((id) => { +// bot.set(id, new Date().toLocaleString()); +// }) +// }, 3 * 60 * 1000) + +sendMessage("[INFO]: Drucker Service wird gestartet", messageIDs) + const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -46,15 +73,22 @@ app.use(express.json()); app.post('/', async (req, res) => { console.log("[INFO] Received print request"); + let value = {} try { - schema.validate(req.body); - } catch (err) { + value = await schema.validateAsync(req.body, { allowUnknown: true }); + } + catch (err) { console.log("[ERROR]", err); + sendMessage("[ERROR] Validation Error (Drucker Service): " + err, messageIDs) return res.status(403).send(err); } - await main(req.body).catch( + + + await main(value).catch( (err) => { console.log(err); + sendMessage("[ERROR] Internal Server Error (Drucker Service): " + err, messageIDs) + return res.status(500).send(err); } ); @@ -1663,6 +1697,7 @@ async function main(data = testData) { console.log("PDF created, printing now") console.log(cacheFolder + data.foreign_id + ".pdf") await printFile(filePath, 2) + } app.listen(2000, () => {