-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmsg.js
109 lines (86 loc) · 3.42 KB
/
testmsg.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const fs = require('fs');
const pikudHaoref = require('pikud-haoref-api');
const config = require('./config.json');
const { typeInHebrew } = require('./functions/typeInHebrew.js');
const { groupCities } = require('./functions/citiesTime.js');
const { format } = require('date-fns');
const user = config.sendToUser;
var interval = 5000;
const wwebVersion = '2.2407.2';
// Load the session data if it has been previously saved
// let sessionData;
// if (fs.existsSync(SESSION_FILE_PATH)) {
// sessionData = JSON.parse(fs.readFileSync(SESSION_FILE_PATH, 'utf-8'));
// }
const client = new Client({
authStrategy: new LocalAuth(),
webVersionCache: {
type: 'remote',
remotePath: `https://raw.githubusercontent.com/wppconnect-team/wa-version/main/html/${wwebVersion}.html`,
}
});
client.on('qr', (qrCode) => {
qrcode.generate(qrCode, { small: true });
});
client.on('ready', async () => {
console.log('Red-Alerts Bot is ready!');
poll();
});
client.initialize();
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);
}
const test = {
type: 'missiles',
cities: ['זיקים', 'בארי', 'נחל עוז', 'קריית שמונה', 'מרגליות', 'חולון', 'אזור', 'בת-ים'],
instructions: 'היכנסו למבנה, נעלו את הדלתות וסגרו את החלונות',
};
sendMessage(test, user);
console.log("Please stop the program now, or it'll keep sending messages to the user every 5 seconds");
// Line break for readability
console.log();
if(!(JSON.parse(JSON.stringify(alert)).type === `none`) && alertCheck !== alert)
{
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');
}