feat: podpora notifikací z notify - základ
This commit is contained in:
@@ -24,3 +24,6 @@
|
||||
# To je užitečné pro odesílání upozornění na různé servery Gotify s různými klíči API.
|
||||
# Struktura dat je ve formátu JSON a je uložena jako řetězec.
|
||||
# GOTIFY_SERVERS_AND_KEYS='[{"server":"https://notification.server.eu", "api_keys":["key1", "key2"]},{"server":"https://notification.server2.eu", "api_keys":["key3", "key4"]}]'
|
||||
#NTFY_HOST = "http://192.168.0.113:80/topic"
|
||||
#NTFY_USERNAME="username"
|
||||
#NTFY_PASSWD="password"
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
/** Notifikace pro gotify*/
|
||||
import { GotifyServer, NotififaceInput, NotifikaceData } from '../../types';
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import {GotifyServer, NotififaceInput, NotifikaceData} from '../../types';
|
||||
import axios, {AxiosError} from 'axios';
|
||||
import dotenv from 'dotenv';
|
||||
import path from 'path';
|
||||
import {log} from "util";
|
||||
|
||||
const ENVIRONMENT = process.env.NODE_ENV || 'production'
|
||||
dotenv.config({ path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`) });
|
||||
dotenv.config({path: path.resolve(__dirname, `../.env.${ENVIRONMENT}`)});
|
||||
|
||||
const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}";
|
||||
const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw);
|
||||
|
||||
export const gotifyCall = async (data: NotififaceInput, gotifyServers?: GotifyServer[]): Promise<any[]> => {
|
||||
if (!Array.isArray(gotifyServers)) {
|
||||
return []
|
||||
@@ -23,10 +23,10 @@ export const gotifyCall = async (data: NotififaceInput, gotifyServers?: GotifySe
|
||||
priority: 7,
|
||||
};
|
||||
|
||||
const headers = { "Content-Type": "application/json" };
|
||||
const headers = {"Content-Type": "application/json"};
|
||||
|
||||
const promises = urls.map(url =>
|
||||
axios.post(url, dataPayload, { headers }).then(response => {
|
||||
axios.post(url, dataPayload, {headers}).then(response => {
|
||||
response.data = {
|
||||
success: true,
|
||||
message: "Notifikace doručena",
|
||||
@@ -51,21 +51,57 @@ export const gotifyCall = async (data: NotififaceInput, gotifyServers?: GotifySe
|
||||
return promises;
|
||||
};
|
||||
|
||||
/** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/
|
||||
export const callNotifikace = async ({ input, teams = true, gotify = true }: NotifikaceData) => {
|
||||
const notifications = [];
|
||||
|
||||
if (gotify) {
|
||||
const gotifyPromises = await gotifyCall(input, gotifyData);
|
||||
notifications.push(...gotifyPromises);
|
||||
export const ntfyCall = async (data: NotififaceInput) => {
|
||||
const url = process.env.NTFY_HOST
|
||||
const username = process.env.NTFY_USERNAME;
|
||||
const password = process.env.NTFY_PASSWD;
|
||||
if (url && username && password) {
|
||||
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64');
|
||||
console.log(url,username,password)
|
||||
axios({
|
||||
url: url,
|
||||
method: 'POST', // PUT works too
|
||||
data: `${data.udalost} - spustil:${data.user}`,
|
||||
headers: {
|
||||
'Authorization': `Basic ${token}`,
|
||||
'Tag':'meat_on_bone'
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
console.log(response.data);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error);
|
||||
});
|
||||
} else {
|
||||
if (!url) {
|
||||
console.log("NTFY_HOST není definován v env")
|
||||
}
|
||||
if (!username) {
|
||||
console.log("NTFY_USERNAME není definován v env")
|
||||
}
|
||||
if (!password) {
|
||||
console.log("NTFY_PASSWD není definován v env")
|
||||
}
|
||||
}
|
||||
}
|
||||
/** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/
|
||||
export const callNotifikace = async ({input, teams = true, gotify = false, ntfy = true}: NotifikaceData) => {
|
||||
if (ntfy) {
|
||||
await ntfyCall(input)
|
||||
}
|
||||
|
||||
/* Zatím není
|
||||
if (teams) {
|
||||
notifications.push(teamsCall(input));
|
||||
}*/
|
||||
|
||||
// Add more notifications as necessary
|
||||
const notifications = [];
|
||||
//gotify bych řekl, že už je deprecated
|
||||
if (gotify) {
|
||||
const gotifyPromises = await gotifyCall(input, gotifyData);
|
||||
notifications.push(...gotifyPromises);
|
||||
}
|
||||
|
||||
try {
|
||||
const results = await Promise.all(notifications);
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import express from "express";
|
||||
import { getLogin, getTrusted } from "../auth";
|
||||
import { getDateForWeekIndex, addChoice, removeChoices, removeChoice, updateDepartureTime, getToday } from "../service";
|
||||
import { getDayOfWeekIndex, parseToken } from "../utils";
|
||||
import { getWebsocket } from "../websocket";
|
||||
import {getLogin, getTrusted} from "../auth";
|
||||
import {addChoice, getDateForWeekIndex, getToday, removeChoice, removeChoices, updateDepartureTime} from "../service";
|
||||
import {getDayOfWeekIndex, parseToken} from "../utils";
|
||||
import {getWebsocket} from "../websocket";
|
||||
import {callNotifikace} from "../notifikace";
|
||||
import {UdalostEnum} from "../../../types";
|
||||
|
||||
/**
|
||||
* Ověří a vrátí index dne v týdnu z požadavku, za předpokladu, že byl předán, a je zároveň
|
||||
@@ -109,5 +111,11 @@ router.post("/changeDepartureTime", async (req, res, next) => {
|
||||
res.status(200).json(data);
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
router.post("/jdemeObed", async (req, res, next) => {
|
||||
const login = getLogin(parseToken(req));
|
||||
try {
|
||||
await callNotifikace({input: {user:login,udalost:UdalostEnum.JDEME_OBED},gotify:false})
|
||||
} catch (e: any) { next(e) }
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user