This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
205 lines (193 loc) · 6.22 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const { inspect } = require("util");
const turndown = new (require("turndown"))();
const grammy = require("grammy");
const get = require("miniget");
const wrs = require("wrs-bmkg")();
wrs.recvWarn = 0;
require("dotenv").config();
const bot = new grammy.Bot(process.env.BOT_TOKEN);
const subscriber = [];
const pendingNarasi = new Set();
function getShakemap(n) {
return new Promise(async (res, rej) => {
let stream = get(
"https://bmkg-content-inatews.storage.googleapis.com/" + n
);
stream.on("error", async (e) => {
stream.destroy();
setTimeout(async (_) => res(await getShakemap(n)), 3000);
});
stream.on("response", (_) => res(stream));
});
}
async function sendWarning(id, msg, t = 3000) {
try {
await bot.api.sendPhoto(
id,
new grammy.InputFile(await getShakemap(msg.shakemap)),
{
caption: `*${msg.subject}*\n\n${msg.description}\n\n${msg.potential}\n\n${msg.instruction}`,
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: [
[
{
text: "Buka WRS-BMKG",
url: "https://warning.bmkg.go.id",
},
],
],
},
}
);
} catch (e) {
console.error(e);
setTimeout(async (_) => await sendWarning(msg, id, t + 1000), t);
}
}
async function sendNarasi(id, ts, t = 3000) {
if (pendingNarasi.has(id * ts)) return;
pendingNarasi.add(id * ts);
try {
let text = await get(`https://bmkg-content-inatews.storage.googleapis.com/${ts}_narasi.txt`).text();
bot.api.sendMessage(id, turndown.turndown(text).replace(RegExp("#", "g"), ""), {
parse_mode: "Markdown",
reply_markup: {
inline_keyboard: [
[{
text: "InaTEWS-BMKG",
url: "https://inatews.bmkg.go.id/"
}, {
text: "WRS-BMKG",
url: "https://warning.bmkg.go.id/"
}],
[{
text: "InaTEWS-BMKG (Telegram)",
url: "https://t.me/InaTEWS_BMKG"
}]
]
}
});
pendingNarasi.delete(id * ts);
} catch (e) {
console.error(e);
setTimeout(async (_) => {
pendingNarasi.delete(id * ts);
await sendNarasi(id, ts, t + 1000);
}, t);
}
}
bot.command("start", async (ctx) => {
if (!subscriber.includes(ctx.message.chat.id))
subscriber.push(ctx.message.chat.id);
if (!wrs.lastAlert)
return ctx.reply(
"Halo. Bot baru saja bangun & sedang menerima informasi baru. Mohon coba lagi nanti."
);
await ctx.reply(wrs.lastAlert.info.headline);
bot.api.sendChatAction(ctx.message.chat.id, "upload_photo");
await sendWarning(ctx.message.chat.id, wrs.lastAlert.info);
sendNarasi(ctx.message.chat.id, wrs.lastAlert.info.eventid);
let text = `*${wrs.lastRealtimeQL.properties.place}*\``;
text += `\nTanggal : ${new Date(
wrs.lastRealtimeQL.properties.time
).toLocaleDateString("id")}`;
text += `\nWaktu : ${new Date(
wrs.lastRealtimeQL.properties.time
).toLocaleTimeString("us", {
timeZone: "Asia/Jakarta",
})} (WIB)`;
text += `\nMagnitude : M${Number(wrs.lastRealtimeQL.properties.mag).toFixed(
1
)}`;
text += `\nFase : ${wrs.lastRealtimeQL.properties.fase}`;
text += `\nStatus : ${wrs.lastRealtimeQL.properties.status}`;
text += `\nKedalaman : ${Math.floor(
wrs.lastRealtimeQL.properties.depth
)} KM\``;
if (Number(wrs.lastRealtimeQL.properties.mag) >= 7)
text = "*Peringatan:* Gempa berskala M >= 7\n" + text;
else if (Number(wrs.lastRealtimeQL.properties.mag) >= 6)
text = "*Peringatan:* Gempa berskala M >= 6\n" + text;
else if (Number(wrs.lastRealtimeQL.properties.mag) >= 5)
text += "\n\nPeringatan: Gempa berskala M >= 5";
let locationMessage = await bot.api.sendVenue(
ctx.message.chat.id,
wrs.lastRealtimeQL.geometry.coordinates[1],
wrs.lastRealtimeQL.geometry.coordinates[0],
wrs.lastRealtimeQL.properties.place,
"M" +
Number(wrs.lastRealtimeQL.properties.mag).toFixed(1) +
", " +
new Date(wrs.lastRealtimeQL.properties.time).toLocaleTimeString("us", {
timeZone: "Asia/Jakarta",
}) +
" (WIB)"
);
await ctx.reply(text, {
parse_mode: "Markdown",
reply_to_message_id: locationMessage.message_id,
});
});
wrs.on("Gempabumi", (msg) => {
if (wrs.recvWarn !== 2) return wrs.recvWarn++;
subscriber.forEach(async (id) => {
await bot.api.sendMessage(id, msg.headline);
await sendWarning(id, msg);
await sendNarasi(id, msg.eventid);
});
});
wrs.on("realtime", (msg) => {
if (wrs.recvWarn !== 2) return wrs.recvWarn++;
let text = `*${wrs.lastRealtimeQL.properties.place}*\``;
text += `\nTanggal : ${new Date(msg.properties.time).toLocaleDateString(
"id"
)}`;
text += `\nWaktu : ${new Date(msg.properties.time).toLocaleTimeString(
"us",
{
timeZone: "Asia/Jakarta",
}
)} (WIB)`;
text += `\nMagnitude : M${Number(msg.properties.mag).toFixed(1)}`;
text += `\nFase : ${msg.properties.fase}`;
text += `\nStatus : ${msg.properties.status}`;
text += `\nKedalaman : ${Math.floor(msg.properties.depth)} KM\``;
if (Number(msg.properties.mag) >= 7)
text = "*Peringatan:* Gempa berskala M >= 7\n" + text;
else if (Number(msg.properties.mag) >= 6)
text = "*Peringatan:* Gempa berskala M >= 6\n" + text;
else if (Number(msg.properties.mag) >= 5)
text += "\n\nPeringatan: Gempa berskala M >= 5";
subscriber.forEach(async (id) => {
let locationMessage = await bot.api.sendVenue(
id,
msg.geometry.coordinates[1],
msg.geometry.coordinates[0],
wrs.lastRealtimeQL.properties.place,
"M" +
Number(msg.properties.mag).toFixed(1) +
", " +
new Date(msg.properties.time).toLocaleTimeString("us", {
timeZone: "Asia/Jakarta",
}) +
" (WIB)"
);
await bot.api.sendMessage(id, text, {
parse_mode: "Markdown",
reply_to_message_id: locationMessage.message_id,
});
});
});
wrs.on("error", (err) => {
console.error(err);
wrs.stopPolling();
wrs.startPolling();
});
wrs.startPolling();
bot.start();
bot.api
.getMe()
.then(({ username }) => console.log(`Berhasil masuk sebagai @${username}`));
bot.catch((e) => console.error(e));
process.on("unhandledRejection", console.error);