Change Telegram Message
This commit is contained in:
105
src/index.js
105
src/index.js
@@ -1,11 +1,11 @@
|
||||
import pdfLib from "pdf-to-printer";
|
||||
import { jsPDF } from "jspdf";
|
||||
import fs from "fs";
|
||||
import puppeteer from 'puppeteer';
|
||||
import express from 'express';
|
||||
import Joi from 'joi';
|
||||
import TelegramBot from 'node-telegram-bot-api';
|
||||
import https from 'https';
|
||||
import puppeteer from "puppeteer";
|
||||
import express from "express";
|
||||
import Joi from "joi";
|
||||
import TelegramBot from "node-telegram-bot-api";
|
||||
import https from "https";
|
||||
import axios from "axios";
|
||||
|
||||
const Logo = fs.readFileSync("./assets/Logo_fwaur.png", { encoding: "latin1" });
|
||||
@@ -40,23 +40,22 @@ const callWebhooks = async (title) => {
|
||||
let filterPass = true;
|
||||
webhook?.filter?.forEach((filter) => {
|
||||
if (title.includes(filter)) filterPass = false;
|
||||
})
|
||||
});
|
||||
|
||||
try {
|
||||
if (filterPass) axios.get(webhook.url);
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
} else {
|
||||
try {
|
||||
axios.get(webhook.url);
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let bot = null;
|
||||
|
||||
@@ -67,12 +66,11 @@ function sendMessage(msg, idList) {
|
||||
try {
|
||||
bot.sendMessage(id, msg);
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
if (config.activate_telegram) {
|
||||
const token = "6150769224:AAGhBagPEi23QtBJl65YNuwEBBXwHguDV0E";
|
||||
|
||||
@@ -81,19 +79,18 @@ if (config.activate_telegram) {
|
||||
bot.setMyCommands([
|
||||
{ command: "/chatid", description: "Zeigt deine Chat ID" },
|
||||
{ command: "/druckerliste", description: "Zeigt alle Drucker" },
|
||||
{ command: "/testWebhook [label]", description: "Test Webhook", },
|
||||
{ command: "/testWebhook [label]", description: "Test Webhook" },
|
||||
]);
|
||||
|
||||
bot.on("message", async (msg) => {
|
||||
const chatId = msg.chat.id;
|
||||
|
||||
|
||||
switch (msg.text) {
|
||||
case "/chatid":
|
||||
try {
|
||||
bot.sendMessage(chatId, "Deine Chat ID ist: " + chatId);
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error(err);
|
||||
}
|
||||
break;
|
||||
case "/druckerliste":
|
||||
@@ -107,7 +104,7 @@ if (config.activate_telegram) {
|
||||
try {
|
||||
bot.sendMessage(chatId, "Drucker Liste:\n" + message);
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
console.error(err);
|
||||
}
|
||||
default:
|
||||
if (msg.text.includes("/testWebhook")) {
|
||||
@@ -141,11 +138,7 @@ async function createPDF(_alarmData) {
|
||||
|
||||
doc.setFontSize(20);
|
||||
|
||||
doc.text(
|
||||
_alarmData.title + " - " + new Date(_alarmData.ts_create * 1000).toLocaleString(),
|
||||
10,
|
||||
15
|
||||
);
|
||||
doc.text(_alarmData.title + " - " + new Date(_alarmData.ts_create * 1000).toLocaleString(), 10, 15);
|
||||
|
||||
doc.setFontSize(14);
|
||||
doc.text(_alarmData.text.split("RIC")[0], 10, 22);
|
||||
@@ -186,18 +179,26 @@ async function puppeteerGetImage(lat, lng) {
|
||||
|
||||
async function main(data) {
|
||||
if (config.activate_telegram) {
|
||||
|
||||
sendMessage("[INFO]: Einsatdepesche wird Gedruckt - " + data.title, messageIDs);
|
||||
sendMessage(
|
||||
`[INFO]: Einsatz\nStichwort: ${data.title}\nMeldung: ${data.text}\nAdresse: ${
|
||||
data.address
|
||||
}\nhttps://www.google.com/maps/search/${data.address.replace(" ", "+")}`,
|
||||
messageIDs
|
||||
);
|
||||
}
|
||||
|
||||
await callWebhooks(data.title);
|
||||
|
||||
if (data.title.includes("Probealarm") || data.title.includes("TH") || data.title.includes("ELW") || data.title.includes("Drehleiter")) {
|
||||
if (
|
||||
data.title.includes("Probealarm") ||
|
||||
data.title.includes("TH") ||
|
||||
data.title.includes("ELW") ||
|
||||
data.title.includes("Drehleiter")
|
||||
) {
|
||||
sendMessage("[INFO]: Einsatdepesche abgebrochen", messageIDs);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const filePath = cacheFolder + data.foreign_id + ".pdf";
|
||||
await createPDF({ filePath, ...data });
|
||||
console.log("PDF created, printing now");
|
||||
@@ -211,29 +212,31 @@ const app = express();
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/depesche', async (req, res) => {
|
||||
app.get("/depesche", async (req, res) => {
|
||||
console.log("[INFO] Received print request");
|
||||
|
||||
let value = {}
|
||||
let value = {};
|
||||
try {
|
||||
await axios.get("https://app.divera247.com/api/v2/alarms?accesskey=qX1RZwmThqB0mFDkd_Bx7CNja6eHvoSCRjyb4RyGBZ40afdai6lWXbH_1YvbB5Uy")
|
||||
await axios
|
||||
.get(
|
||||
"https://app.divera247.com/api/v2/alarms?accesskey=qX1RZwmThqB0mFDkd_Bx7CNja6eHvoSCRjyb4RyGBZ40afdai6lWXbH_1YvbB5Uy"
|
||||
)
|
||||
.then((res) => {
|
||||
if (config.debug) {
|
||||
fs.writeFileSync(cacheFolder + "data.json", JSON.stringify(res.data))
|
||||
fs.writeFileSync(cacheFolder + "data.json", JSON.stringify(res.data));
|
||||
}
|
||||
console.log(res.data)
|
||||
value = res.data.data.items[res.data.data.sorting[0]]
|
||||
})
|
||||
|
||||
console.log(res.data);
|
||||
value = res.data.data.items[res.data.data.sorting[0]];
|
||||
});
|
||||
} catch (error) {
|
||||
if (config.activate_telegram) {
|
||||
sendMessage("[ERROR] Divera data error(Drucker Service): " + error, messageIDs)
|
||||
sendMessage("[ERROR] Divera data error(Drucker Service): " + error, messageIDs);
|
||||
}
|
||||
return res.status(500).send(error);
|
||||
}
|
||||
if (value == {}) {
|
||||
if (config.activate_telegram) {
|
||||
sendMessage("[ERROR] No Value Data from Divera (Drucker Service): ", messageIDs)
|
||||
sendMessage("[ERROR] No Value Data from Divera (Drucker Service): ", messageIDs);
|
||||
}
|
||||
return res.status(500).send("No Data");
|
||||
}
|
||||
@@ -256,32 +259,38 @@ app.get("/health", (req, res) => {
|
||||
app.post("/states/status", (req, res) => {
|
||||
if (req.query.accesskey != config.accesskey) {
|
||||
return res.status(401).json({
|
||||
"success": false,
|
||||
"error": "Unauthorized"
|
||||
success: false,
|
||||
error: "Unauthorized",
|
||||
});
|
||||
}
|
||||
// Append request body to file
|
||||
fs.appendFileSync(cacheFolder + "status.json", JSON.stringify({ method: "POST", body: req.body, query: req.query }) + "\n");
|
||||
// Append request body to file
|
||||
fs.appendFileSync(
|
||||
cacheFolder + "status.json",
|
||||
JSON.stringify({ method: "POST", body: req.body, query: req.query }) + "\n"
|
||||
);
|
||||
|
||||
res.json({
|
||||
"success": true,
|
||||
"data": req.body
|
||||
success: true,
|
||||
data: req.body,
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/states/status", (req, res) => {
|
||||
if (req.query.accesskey != config.accesskey) {
|
||||
return res.status(401).json({
|
||||
"success": false,
|
||||
"error": "Unauthorized"
|
||||
success: false,
|
||||
error: "Unauthorized",
|
||||
});
|
||||
}
|
||||
// Append request body to file
|
||||
fs.appendFileSync(cacheFolder + "status.json", JSON.stringify({ method: "GET", body: req.body, query: req.query }) + "\n");
|
||||
// Append request body to file
|
||||
fs.appendFileSync(
|
||||
cacheFolder + "status.json",
|
||||
JSON.stringify({ method: "GET", body: req.body, query: req.query }) + "\n"
|
||||
);
|
||||
|
||||
res.json({
|
||||
"success": true,
|
||||
data: req.query
|
||||
success: true,
|
||||
data: req.query,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -289,4 +298,4 @@ const httpsServer = https.createServer(credentials, app);
|
||||
|
||||
httpsServer.listen(PORT, () => {
|
||||
console.log(`PDF Printing Service listening on port ${PORT}`);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user