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