diff --git a/config.json b/config.json new file mode 100644 index 0000000..1dd043b --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ +{ + "activate_telegram": false, + "printer_device_id": "Microsoft Print to PDF" +} \ No newline at end of file diff --git a/package.json b/package.json index 3b9f569..4a99ac0 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "printer_service", "version": "1.0.0", "description": "", - "main": "index.js", + "main": "src/index.js", "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" @@ -20,4 +20,4 @@ "pdf-to-printer": "^5.6.0", "puppeteer": "^19.9.0" } -} +} \ No newline at end of file diff --git a/index.js b/src/index.js similarity index 95% rename from index.js rename to src/index.js index d065064..323c1d6 100644 --- a/index.js +++ b/src/index.js @@ -8,53 +8,52 @@ import path, { join } from 'path'; import { fileURLToPath } from 'url'; import TelegramBot from 'node-telegram-bot-api'; +const config = JSON.parse(fs.readFileSync("./config.json", "utf8")); + // replace the value below with the Telegram token you receive from @BotFather -const token = '6150769224:AAGhBagPEi23QtBJl65YNuwEBBXwHguDV0E'; +if (config.activate_telegram) { + const token = '6150769224:AAGhBagPEi23QtBJl65YNuwEBBXwHguDV0E'; -const messageIDs = [1034133487] + const messageIDs = [1034133487] -// Create a bot that uses 'polling' to fetch new updates -const bot = new TelegramBot(token, { polling: true }); + // 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' }, - { command: '/druckerliste', description: 'Zeigt alle Drucker' }, -]) + bot.setMyCommands([ + { command: '/chatid', description: 'Zeigt deine Chat ID' }, + { command: '/druckerliste', description: 'Zeigt alle Drucker' }, + ]) -bot.on('message', async (msg) => { - const chatId = msg.chat.id; + bot.on('message', async (msg) => { + const chatId = msg.chat.id; - switch (msg.text) { - case '/chatid': - bot.sendMessage(chatId, 'Deine Chat ID ist: ' + chatId); - break; - case '/druckerliste': - let message = "" + switch (msg.text) { + case '/chatid': + bot.sendMessage(chatId, 'Deine Chat ID ist: ' + chatId); + break; + case '/druckerliste': + let message = "" - const printers = await pdfLib.getPrinters() - printers.forEach((printer) => { - message += printer.deviceId + "\n" - }); + const printers = await pdfLib.getPrinters() + printers.forEach((printer) => { + message += printer.deviceId + "\n" + }); - bot.sendMessage(chatId, 'Drucker Liste:\n' + message); - default: + bot.sendMessage(chatId, 'Drucker Liste:\n' + message); + default: + } + + }); + + function sendMessage(msg, idList) { + idList.forEach((id) => { + bot.sendMessage(id, msg); + }) } -}); - -function sendMessage(msg, idList) { - idList.forEach((id) => { - bot.sendMessage(id, msg); - }) + sendMessage("[INFO]: Drucker Service wird gestartet", messageIDs) } -// 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); @@ -73,8 +72,7 @@ const schema = Joi.object({ var Logo = fs.readFileSync("./assets/Logo_fwaur.png", { encoding: "latin1" }); -const printerName = "Microsoft Print to PDF" -// const printerName = "OKI-MC853-CE9408" +const printerName = config.printer_device_id; const cacheFolder = "./cache/" const app = express(); @@ -89,7 +87,9 @@ app.post('/', async (req, res) => { } catch (err) { console.log("[ERROR]", err); - sendMessage("[ERROR] Validation Error (Drucker Service): " + err, messageIDs) + if (config.activate_telegram) { + sendMessage("[ERROR] Validation Error (Drucker Service): " + err, messageIDs) + } return res.status(403).send(err); } @@ -97,8 +97,9 @@ app.post('/', async (req, res) => { await main(value).catch( (err) => { console.log(err); - sendMessage("[ERROR] Internal Server Error (Drucker Service): " + err, messageIDs) - + if (config.activate_telegram) { + sendMessage("[ERROR] Internal Server Error (Drucker Service): " + err, messageIDs) + } return res.status(500).send(err); } ); @@ -1668,7 +1669,7 @@ async function createPDF(_alarmData) { await puppeteerGetImage(_alarmData.lat, _alarmData.lng) - const mapImg = fs.readFileSync("./cache/map.png", "base64") + const mapImg = fs.readFileSync(cacheFolder + "map.png", "base64") doc.addImage(mapImg, 10, 25); @@ -1690,7 +1691,7 @@ async function puppeteerGetImage(lat, lng) { await page.evaluate(() => { document.body.innerHTML += `
`; }) - await page.screenshot({ path: './cache/map.png' }); + await page.screenshot({ path: cacheFolder + 'map.png' }); await browser.close(); return; @@ -1712,6 +1713,4 @@ async function main(data = testData) { app.listen(2000, () => { console.log(`PDF Printing Service listening on port 2000`); -}) - -// main() \ No newline at end of file +}) \ No newline at end of file