-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtelegram.js
54 lines (43 loc) · 1.18 KB
/
telegram.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
const { Telegram } = require('telegraf');
class TelegramClient {
constructor() {
this.telegram = new Telegram(process.env.TELEGRAM_NOTIFIER_BOT_TOKEN);
this.threadId =
process.env.TELEGRAM_NOTIFIER_TOPIC_ID ||
process.env.TELEGRAM_NOTIFIER_THREAD_ID ||
null;
}
async send(message, overrides = {}) {
const options = {
parse_mode: 'HTML',
disable_web_page_preview: true
};
const threadId = overrides.threadId || this.threadId;
if (threadId) {
options.message_thread_id = parseInt(threadId);
}
const chatId = overrides.chatId || process.env.TELEGRAM_NOTIFIER_CHAT_ID;
return this.telegram.sendMessage(
chatId,
message,
options
);
}
async sendError(e, overrides = {}) {
const options = {};
const threadId = overrides.threadId || this.threadId;
if (threadId) {
options.message_thread_id = parseInt(threadId);
}
const chatId = overrides.chatId || process.env.TELEGRAM_NOTIFIER_CHAT_ID;
return this.telegram.sendMessage(
chatId,
`Error: ${e}`,
options
);
}
check() {
return this.telegram.getMe();
}
}
module.exports = TelegramClient;