tripstorekrabi-shared 1.22.2 → 1.22.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -2
- package/telegram.js +57 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tripstorekrabi-shared",
|
|
3
|
-
"version": "1.22.
|
|
3
|
+
"version": "1.22.3",
|
|
4
4
|
"description": "Shared library for Trip Store Krabi",
|
|
5
5
|
"author": "Stefan Sommer <tonprince@gmail.com>",
|
|
6
6
|
"repository": {
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"./availability": "./availability.js",
|
|
14
14
|
"./booking": "./booking.js",
|
|
15
15
|
"./product": "./product.js",
|
|
16
|
-
"./ota": "./ota.js"
|
|
16
|
+
"./ota": "./ota.js",
|
|
17
|
+
"./telegram": "./telegram.js"
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|
|
19
20
|
"dayjs": "^1.11.19",
|
package/telegram.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const ENV_BOT_TOKEN = process.env.TELEGRAM_API_TOKEN;
|
|
2
|
+
const ENV_CHAT_ID = process.env.TELEGRAM_CHAT_ID;
|
|
3
|
+
const TELEGRAM_MAX_CHARS = 4096;
|
|
4
|
+
|
|
5
|
+
export async function sendTelegramMessage(message, options = {}) {
|
|
6
|
+
const {
|
|
7
|
+
botToken,
|
|
8
|
+
chatId,
|
|
9
|
+
parseMode = 'HTML',
|
|
10
|
+
prefix = '',
|
|
11
|
+
truncate = true
|
|
12
|
+
} = options;
|
|
13
|
+
|
|
14
|
+
const BOT_TOKEN = botToken || ENV_BOT_TOKEN;
|
|
15
|
+
const CHAT_ID = chatId || ENV_CHAT_ID;
|
|
16
|
+
|
|
17
|
+
if (!BOT_TOKEN || !CHAT_ID) {
|
|
18
|
+
console.error("FATAL: Telegram secrets are not configured in options or the environment.");
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let finalMessage = `${prefix} ${message}`.trim();
|
|
23
|
+
|
|
24
|
+
if (truncate && finalMessage.length > TELEGRAM_MAX_CHARS) {
|
|
25
|
+
const note = "\n\n... [Message Truncated]";
|
|
26
|
+
const usableLength = TELEGRAM_MAX_CHARS - note.length;
|
|
27
|
+
finalMessage = finalMessage.substring(0, usableLength) + note;
|
|
28
|
+
console.warn(`Message truncated from ${message.length} to ${TELEGRAM_MAX_CHARS} characters.`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const telegramApiUrl = `https://api.telegram.org/bot${BOT_TOKEN}/sendMessage`;
|
|
33
|
+
const body = {
|
|
34
|
+
chat_id: CHAT_ID,
|
|
35
|
+
text: finalMessage,
|
|
36
|
+
parse_mode: parseMode.toUpperCase(),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const res = await fetch(telegramApiUrl, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: {
|
|
42
|
+
'Content-Type': 'application/json'
|
|
43
|
+
},
|
|
44
|
+
body: JSON.stringify(body)
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (!res.ok) {
|
|
48
|
+
const errorBody = await res.text();
|
|
49
|
+
console.error(`Failed to send Telegram message: HTTP Status ${res.status} ${res.statusText}`);
|
|
50
|
+
console.error('Telegram API Detailed Error:', errorBody);
|
|
51
|
+
} else {
|
|
52
|
+
console.log('Telegram message sent successfully.');
|
|
53
|
+
}
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error('An error occurred while trying to send a Telegram message:', e);
|
|
56
|
+
}
|
|
57
|
+
}
|