Repository navigation
Expand file tree
/
Copy pathbot.js
More file actions
125 lines (102 loc) · 2.7 KB
/
Copy pathbot.js
File metadata and controls
125 lines (102 loc) · 2.7 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
const DICE_EMOJI = "\u{1F3B2}";
const TELEGRAM_API_BASE = "https://api.telegram.org";
const POLL_TIMEOUT_SECONDS = 25;
const RETRY_DELAY_MS = 5000;
const token = process.env.TELEGRAM_BOT_TOKEN;
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function requireToken() {
if (!token) {
throw new Error("TELEGRAM_BOT_TOKEN is not configured.");
}
}
async function callTelegram(method, payload = {}) {
requireToken();
const response = await fetch(`${TELEGRAM_API_BASE}/bot${token}/${method}`, {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify(payload)
});
const body = await response.json().catch(() => ({
ok: false,
description: `Telegram returned HTTP ${response.status}.`
}));
if (!body.ok) {
throw new Error(body.description || "Telegram request failed.");
}
return body.result;
}
function dicePrediction(message, random = Math.random) {
const dice = message?.dice;
if (
dice?.emoji === DICE_EMOJI &&
Number.isInteger(dice.value) &&
dice.value >= 1 &&
dice.value <= 6
) {
return dice.value;
}
if (message?.text?.trim() === DICE_EMOJI) {
return Math.floor(random() * 6) + 1;
}
return null;
}
async function sendMessage(chatId, text) {
return callTelegram("sendMessage", {
chat_id: chatId,
text
});
}
async function handleUpdate(update) {
const message = update?.message || update?.edited_message;
const chatId = message?.chat?.id;
const text = message?.text?.trim();
if (!chatId) {
return false;
}
const prediction = dicePrediction(message);
if (prediction) {
await sendMessage(chatId, `Prediction: ${prediction}`);
return true;
}
if (text === "/start") {
await sendMessage(chatId, "Send dice emoji and I will predict the number.");
return true;
}
if (text === "/ping") {
await sendMessage(chatId, "pong");
return true;
}
return false;
}
async function startBot() {
requireToken();
let offset = 0;
console.log("Dice bot started. Send dice emoji to test it.");
while (true) {
try {
const updates = await callTelegram("getUpdates", {
offset,
timeout: POLL_TIMEOUT_SECONDS,
allowed_updates: ["message", "edited_message"]
});
for (const update of updates) {
offset = update.update_id + 1;
const handled = await handleUpdate(update);
if (handled) {
console.log(`Handled update ${update.update_id}.`);
}
}
} catch (error) {
console.error(`Bot error: ${error.message}`);
await sleep(RETRY_DELAY_MS);
}
}
}
startBot().catch((error) => {
console.error(error.message);
process.exit(1);
});