rozběhání gotify notifikací podle dat z env

This commit is contained in:
Reallag 2023-06-13 21:30:00 +02:00
parent 0b2edeaac3
commit 97aa41549b
4 changed files with 51 additions and 35 deletions

View File

@ -5,4 +5,10 @@
# Zapne režim mockování jídelních lístků. # Zapne režim mockování jídelních lístků.
# Vhodné pro vývoj o víkendech, svátcích a dalších dnech, pro které podniky nenabízejí obědové menu. # Vhodné pro vývoj o víkendech, svátcích a dalších dnech, pro které podniky nenabízejí obědové menu.
# V tomto režimu vrací server vždy falešné datum (pracovní den) a Food API pevně nadefinovanou, smyšlenou nabídku jídel. # V tomto režimu vrací server vždy falešné datum (pracovní den) a Food API pevně nadefinovanou, smyšlenou nabídku jídel.
# MOCK_DATA=true # MOCK_DATA=true
# Určuje servery Gotify a příslušné klíče API.
# Formát je pole objektů, kde každý objekt obsahuje adresu serveru a pole klíčů API.
# 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"]}]'

View File

@ -1,41 +1,46 @@
/** Notifikace pro gotify*/ /** Notifikace pro gotify*/
import axios, {AxiosError, AxiosResponse} from 'axios'; import axios, {AxiosError, AxiosResponse} from 'axios';
import {NotififaceInput, NotifikaceData, UdalostEnum} from "./types"; import {GotifyServer, NotififaceInput, NotifikaceData, UdalostEnum} from "./types";
import dotenv from 'dotenv';
dotenv.config()
const gotifyDataRaw = process.env.GOTIFY_SERVERS_AND_KEYS || "{}";
const gotifyData: GotifyServer[] = JSON.parse(gotifyDataRaw);
export const gotifyCall = async (data: NotififaceInput, gotifyServers: GotifyServer[]): Promise<any[]> => {
const urls = gotifyServers.flatMap(gotifyServer =>
gotifyServer.api_keys.map(apiKey => `${gotifyServer.server}/message?token=${apiKey}`));
const url = 'your_URL_here'; // Place your URL here const dataPayload = {
title: "Luncher",
export const gotifyCall = async (data: NotififaceInput): Promise<void> => { message: `${data.udalost} - spustil:${data.user}`,
priority: 7,
const options = {
headers: {"Content-Type": "application/json"},
data: {
title: "Luncher",
message: `${data.udalost} - spustil:${data.user}`,
priority: 7,
},
}; };
try { const headers = {"Content-Type": "application/json"};
const response = await axios.post(url, options);
response.data = { const promises = urls.map(url =>
success: true, axios.post(url, dataPayload, { headers }).then(response => {
message: "Notifikace doručena", response.data = {
}; success: true,
console.log(response) message: "Notifikace doručena",
} catch (error: unknown) { };
if (axios.isAxiosError(error)) { return response;
const axiosError = error as AxiosError; }).catch(error => {
if (axiosError.response) { if (axios.isAxiosError(error)) {
axiosError.response.data = { const axiosError = error as AxiosError;
success: false, if (axiosError.response) {
message: "fail", axiosError.response.data = {
}; success: false,
console.log(axiosError.response); message: "fail",
};
console.log(error)
return axiosError.response;
}
} }
} // Handle unknown error without a response
// Handle unknown error without a response console.log(error,"unknown error");
console.log("unkown error") })
} );
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*/ /** Zavolá notifikace na všechny konfigurované způsoby notifikace, přetížení proměných na false pro jednotlivé způsoby je vypne*/
@ -43,7 +48,8 @@ export const callNotifikace = async ({input, teams = true, gotify = true}: Notif
const notifications = []; const notifications = [];
if (gotify) { if (gotify) {
notifications.push(gotifyCall(input)); const gotifyPromises = await gotifyCall(input, gotifyData);
notifications.push(...gotifyPromises);
} }
/* Zatím není /* Zatím není

View File

@ -191,7 +191,7 @@ export function finishPizzaOrder(login: string) {
} }
clientData.pizzaDay.state = PizzaDayState.ORDERED; clientData.pizzaDay.state = PizzaDayState.ORDERED;
db.set(today, clientData); db.set(today, clientData);
callNotifikace({input:{udalost:UdalostEnum.ZAHAJENA_PIZZA,user:clientData?.pizzaDay?.creator}}) callNotifikace({input:{udalost:UdalostEnum.OBJEDNANA_PIZZA,user:clientData?.pizzaDay?.creator}})
return clientData; return clientData;
} }

View File

@ -82,4 +82,8 @@ export interface NotifikaceData{
input:NotififaceInput, input:NotififaceInput,
gotify?:boolean, gotify?:boolean,
teams?:boolean, teams?:boolean,
}
export interface GotifyServer {
server: string;
api_keys: string[];
} }