rozběhání gotify notifikací podle dat z env
This commit is contained in:
parent
0b2edeaac3
commit
97aa41549b
@ -6,3 +6,9 @@
|
||||
# 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.
|
||||
# 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"]}]'
|
||||
|
@ -1,41 +1,46 @@
|
||||
/** Notifikace pro gotify*/
|
||||
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
|
||||
|
||||
export const gotifyCall = async (data: NotififaceInput): Promise<void> => {
|
||||
|
||||
const options = {
|
||||
headers: {"Content-Type": "application/json"},
|
||||
data: {
|
||||
title: "Luncher",
|
||||
message: `${data.udalost} - spustil:${data.user}`,
|
||||
priority: 7,
|
||||
},
|
||||
const dataPayload = {
|
||||
title: "Luncher",
|
||||
message: `${data.udalost} - spustil:${data.user}`,
|
||||
priority: 7,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await axios.post(url, options);
|
||||
response.data = {
|
||||
success: true,
|
||||
message: "Notifikace doručena",
|
||||
};
|
||||
console.log(response)
|
||||
} catch (error: unknown) {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const axiosError = error as AxiosError;
|
||||
if (axiosError.response) {
|
||||
axiosError.response.data = {
|
||||
success: false,
|
||||
message: "fail",
|
||||
};
|
||||
console.log(axiosError.response);
|
||||
const headers = {"Content-Type": "application/json"};
|
||||
|
||||
const promises = urls.map(url =>
|
||||
axios.post(url, dataPayload, { headers }).then(response => {
|
||||
response.data = {
|
||||
success: true,
|
||||
message: "Notifikace doručena",
|
||||
};
|
||||
return response;
|
||||
}).catch(error => {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const axiosError = error as AxiosError;
|
||||
if (axiosError.response) {
|
||||
axiosError.response.data = {
|
||||
success: false,
|
||||
message: "fail",
|
||||
};
|
||||
console.log(error)
|
||||
return axiosError.response;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Handle unknown error without a response
|
||||
console.log("unkown error")
|
||||
}
|
||||
// Handle unknown error without a response
|
||||
console.log(error,"unknown 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*/
|
||||
@ -43,7 +48,8 @@ export const callNotifikace = async ({input, teams = true, gotify = true}: Notif
|
||||
const notifications = [];
|
||||
|
||||
if (gotify) {
|
||||
notifications.push(gotifyCall(input));
|
||||
const gotifyPromises = await gotifyCall(input, gotifyData);
|
||||
notifications.push(...gotifyPromises);
|
||||
}
|
||||
|
||||
/* Zatím není
|
||||
|
@ -191,7 +191,7 @@ export function finishPizzaOrder(login: string) {
|
||||
}
|
||||
clientData.pizzaDay.state = PizzaDayState.ORDERED;
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -83,3 +83,7 @@ export interface NotifikaceData{
|
||||
gotify?:boolean,
|
||||
teams?:boolean,
|
||||
}
|
||||
export interface GotifyServer {
|
||||
server: string;
|
||||
api_keys: string[];
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user