-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram-bot-api.js
More file actions
164 lines (138 loc) · 4.61 KB
/
Copy pathtelegram-bot-api.js
File metadata and controls
164 lines (138 loc) · 4.61 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
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
const TelegramBot = require('node-telegram-bot-api');
const nconf = require('nconf');
let bot = null;
/**
* Initialize the Telegram bot
* @returns {TelegramBot|null}
*/
function initBot() {
if (bot) return bot;
const token = nconf.get('telegram:token');
if (!token || token.length === 0) {
console.error('No token configured in telegram:token');
return null;
}
// Create bot instance without polling (we only send messages)
bot = new TelegramBot(token, { polling: false });
return bot;
}
/**
* Sends a Telegram notification using node-telegram-bot-api
* @param {string} msg - The message to send
* @param {Object} opts - Options for the message
* @param {string} opts.parse_mode - "Markdown", "HTML", or undefined for plain text
* @param {boolean} opts.disable_web_page_preview - Disable link previews
* @param {boolean} opts.disable_notification - Silent notification
* @returns {Promise<void>}
*/
function sendTelegramNotification(msg, opts = {}) {
const chatIds = nconf.get('telegram:chatId');
if (!chatIds || chatIds.length === 0) {
return Promise.reject(new Error('No chat IDs configured in telegram:chatId'));
}
const botInstance = initBot();
if (!botInstance) {
return Promise.reject(new Error('Failed to initialize Telegram bot'));
}
// Send to all chat IDs
const promises = chatIds.map(chatId => {
return sendToChat(botInstance, chatId, msg, opts);
});
return Promise.all(promises);
}
/**
* Send message to a single chat
* @param {TelegramBot} botInstance - The bot instance
* @param {string|number} chatId - The chat ID
* @param {string} msg - The message
* @param {Object} opts - Options
* @returns {Promise<Object>}
*/
function sendToChat(botInstance, chatId, msg, opts = {}) {
const sendOptions = {};
// Parse mode options
if (opts.parse_mode) {
sendOptions.parse_mode = opts.parse_mode;
}
// Disable web page preview
if (opts.disable_web_page_preview) {
sendOptions.disable_web_page_preview = true;
}
// Disable notification (silent)
if (opts.disable_notification) {
sendOptions.disable_notification = true;
}
return botInstance.sendMessage(chatId, msg, sendOptions)
.then(response => {
return {
success: true,
chatId: chatId,
messageId: response.message_id
};
})
.catch(error => {
throw new Error(`Failed to send message to chat ${chatId}: ${error.message}`);
});
}
/**
* Send a file via Telegram
* @param {string} filePath - Path to the file
* @param {Object} opts - Options
* @param {string} opts.type - "image", "video", or "document" (default: "document")
* @param {string} opts.caption - Caption for the file
* @returns {Promise<void>}
*/
function sendTelegramFile(filePath, opts = {}) {
const chatIds = nconf.get('telegram:chatId');
if (!chatIds || chatIds.length === 0) {
return Promise.reject(new Error('No chat IDs configured in telegram:chatId'));
}
const botInstance = initBot();
if (!botInstance) {
return Promise.reject(new Error('Failed to initialize Telegram bot'));
}
const promises = chatIds.map(chatId => {
return sendFileToChat(botInstance, chatId, filePath, opts);
});
return Promise.all(promises);
}
/**
* Send file to a single chat
* @param {TelegramBot} botInstance - The bot instance
* @param {string|number} chatId - The chat ID
* @param {string} filePath - Path to the file
* @param {Object} opts - Options
* @returns {Promise<Object>}
*/
function sendFileToChat(botInstance, chatId, filePath, opts = {}) {
const fileOptions = {};
// Caption
if (opts.caption) {
fileOptions.caption = opts.caption;
}
// Determine which method to use based on type
const fileType = opts.type || 'document';
let sendPromise;
if (fileType === 'image') {
sendPromise = botInstance.sendPhoto(chatId, filePath, fileOptions);
} else if (fileType === 'video') {
sendPromise = botInstance.sendVideo(chatId, filePath, fileOptions);
} else {
sendPromise = botInstance.sendDocument(chatId, filePath, fileOptions);
}
return sendPromise
.then(response => {
return {
success: true,
chatId: chatId,
messageId: response.message_id
};
})
.catch(error => {
throw new Error(`Failed to send file to chat ${chatId}: ${error.message}`);
});
}
module.exports = {
sendTelegramNotification,
sendTelegramFile
};