-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (73 loc) · 2.7 KB
/
index.js
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
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const pikudHaoref = require('pikud-haoref-api');
const fs = require('fs');
const { typeInHebrew } = require('./functions/typeInHebrew.js');
const config = require('./config.json');
const { groupCities } = require('./functions/citiesTime.js');
const groupId = config.groupId;
var interval = 5000;
// Whatsapp-Web.js Bug Fix:
const wwebVersion = '2.2407.2';
const client = new Client({
authStrategy: new LocalAuth(),
webVersionCache: {
type: 'remote',
remotePath: `https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${wwebVersion}.html`,
}
});
var alertCheck = "";
client.on('qr', (qrCode) => {
qrcode.generate(qrCode, { small: true });
});
client.on('ready', async () => {
console.log('Red-Alerts Bot is ready!');
poll();
});
var poll = function () {
// Optional Israeli proxy if running outside Israeli borders
// Get currently active alert
// Example response:
// {
// type: 'missiles',
// cities: ['תל אביב - מזרח', 'חיפה - כרמל ועיר תחתית', 'עין גדי'],
// instructions: 'היכנסו למבנה, נעלו את הדלתות וסגרו את החלונות'
// }
pikudHaoref.getActiveAlert(function (err, alert) {
// Schedule polling in X millis
setTimeout(poll, interval);
// Log errors
if (err) {
return console.log('Retrieving active alert failed: ', err);
}
if(!(JSON.parse(JSON.stringify(alert)).type === `none`) && !(JSON.stringify(alert) === JSON.stringify(alertCheck)))
{
sendMessage(alert, groupId);
alertCheck = alert;
}
else if (JSON.parse(JSON.stringify(alert)).type === `none`)
{
alertCheck = "";
}
});
}
function sendMessage(alert, groupId) {
// Get current date and time
const date = new Date();
const formattedDate = format(date, "dd/MM/yyyy | HH:mm:ss");
// Group cities based on time and zone
const groupedCities = groupCities(alert.cities);
// Create the message
let message = `*🔴 צבע אדום (${formattedDate})*\n`;
message += `סוג ההתרעה: ${typeInHebrew(alert.type)}\n`;
// Add cities and towns
message += `ערים וישובים:\n`;
for (const [zone, cities] of Object.entries(groupedCities)) {
message += `\n• ${zone}: ${cities.map(city => `${city.city}`).join(', ')} (${cities[0].time})\n`;
}
message += `\n${alert.instructions}`;
// Send the message
client.sendMessage(groupId, message);
console.log('Message sent successfully to group');
}
client.initialize();