-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
166 lines (138 loc) · 5.08 KB
/
bot.js
File metadata and controls
166 lines (138 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import fetch from "node-fetch";
import cron from "node-cron";
import http from "http";
const ntfyTopic = "temposquach";
// URL de l'API de couleur Tempo d'EDF
const tempoAPI = "https://www.api-couleur-tempo.fr/api/jourTempo/today";
const tempoAPItomorrow =
"https://www.api-couleur-tempo.fr/api/jourTempo/tomorrow";
const serverURL = "https://tempobot.onrender.com"; // Remplace par l'URL de ton serveur
async function getTempoAndNotify() {
try {
// Appel à l'API pour récupérer le jour Tempo de demain
const response = await fetch(tempoAPI, {
method: "GET",
headers: {
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error("Erreur lors de la récupération des données Tempo");
}
// Récupérer la réponse JSON
const tempoData = await response.json();
console.log("Données Tempo reçues:", tempoData); // Ajout de log pour vérifier les données reçues
let tempoDay = tempoData.codeJour; // Supposons que la réponse contient un champ 'codeJour'
const today = new Date();
let day = today.getDate();
let month = today.getMonth() + 1;
let year = today.getFullYear();
day = day < 10 ? "0" + day : day;
month = month < 10 ? "0" + month : month;
// Format the date as dd/mm/yyyy
const formattedDate = `${day}/${month}/${year}`;
if (!tempoDay) {
throw new Error("Erreur lors de la récupération du jour Tempo");
}
if (tempoDay == "2") {
tempoDay = "⚪️ Blanc";
} else if (tempoDay == "1") {
tempoDay = "🔵 Bleu";
} else if (tempoDay == "3") {
tempoDay = "🔴 Rouge";
}
console.log(`Aujourd'hui (${formattedDate}) Tempo EDF: ${tempoDay}`);
// Envoi de la notification via ntfy.sh
const ntfyResponse = await fetch(`https://ntfy.sh/${ntfyTopic}`, {
method: "POST",
headers: {
// "Content-Type": "text/plain",
Markdown: "yes",
},
body: `Aujourd'hui (${formattedDate}): ${tempoDay}`,
});
if (!ntfyResponse.ok) {
throw new Error("Erreur lors de l'envoi de la notification");
}
// Envoyer la notification (ajoutez votre logique d'envoi ici)
console.log(`Notification envoyée pour le ${formattedDate}: ${tempoDay}`);
} catch (error) {
console.error("Erreur lors de la récupération des données Tempo:", error);
}
}
async function getTomorrow() {
try {
// Appel à l'API pour récupérer le jour Tempo de demain
const response = await fetch(tempoAPItomorrow, {
method: "GET",
headers: {
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error("Erreur lors de la récupération des données Tempo");
}
const tempoData = await response.json();
console.log("Données Tempo reçues:", tempoData); // Ajout de log pour vérifier les données reçues
let tempoDay = tempoData.codeJour; // Supposons que la réponse contient un champ 'codeJour'
const today = new Date();
// Calculer la date de demain
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
let day = tomorrow.getDate();
let month = tomorrow.getMonth() + 1; // Les mois sont indexés à partir de 0
let year = tomorrow.getFullYear();
day = day < 10 ? "0" + day : day;
month = month < 10 ? "0" + month : month;
// Format the date as dd/mm/yyyy
const formattedDate = `${day}/${month}/${year}`;
console.log(`Demain est le ${formattedDate}`);
if (!tempoDay) {
throw new Error("Erreur lors de la récupération du jour Tempo");
}
if (tempoDay == "2") {
tempoDay = "⚪️ Blanc";
} else if (tempoDay == "1") {
tempoDay = "🔵 Bleu";
} else if (tempoDay == "3") {
tempoDay = "🔴 Rouge";
}
console.log(`Demain (${formattedDate}): ${tempoDay}`);
// Envoi de la notification via ntfy.sh
const ntfyResponse = await fetch(`https://ntfy.sh/${ntfyTopic}`, {
method: "POST",
headers: {
// "Content-Type": "text/plain",
Markdown: "yes",
},
body: `Demain (${formattedDate}): ${tempoDay}`,
});
if (!ntfyResponse.ok) {
throw new Error("Erreur lors de l'envoi de la notification");
}
// Envoyer la notification (ajoutez votre logique d'envoi ici)
console.log(`Notification envoyée pour le ${formattedDate}: ${tempoDay}`);
} catch (error) {
console.error("Erreur:", error);
}
}
// Fonction pour garder le bot actif
async function keepAlive() {
try {
const response = await fetch(serverURL, {
method: "GET",
});
if (!response.ok) {
throw new Error("Erreur lors du ping du serveur");
}
console.log("Ping réussi:", new Date().toLocaleTimeString());
} catch (error) {
console.error("Erreur lors du ping du serveur:", error);
}
}
// Planifier la tâche pour récupérer et notifier les données Tempo tous les jours à 6h00
console.log(new Date().toLocaleString());
console.log("Exécution de getTempoAndNotify à", new Date().toLocaleString());
getTempoAndNotify();
console.log("Exécution de getTomorrow à", new Date().toLocaleString());
getTomorrow();