forked from runnerty/notifier-telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
50 lines (45 loc) · 1.5 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
'use strict';
process.env.NTBA_FIX_319 = 1;
process.env.NTBA_FIX_350 = 1;
const Notifier = require('@runnerty/module-core').Notifier;
const TelegramBot = require('node-telegram-bot-api');
const fs = require('fs');
class telegramNotifier extends Notifier {
constructor(notification) {
super(notification);
}
async send(notification) {
try {
const bot = new TelegramBot(notification.token);
await bot.sendMessage(notification.chat_id, notification.message);
// Photo
if (notification.photo) {
try {
await fs.promises.access(notification.photo, fs.constants.F_OK);
const photoStream = fs.createReadStream(notification.photo);
await bot.sendPhoto(notification.chat_id, photoStream);
} catch (err) {
this.logger.log('warn', `Telegram Notifier: Photo not found: ${notification.photo}`);
}
}
// Video
if (notification.video) {
try {
await fs.promises.access(notification.video, fs.constants.F_OK);
const videoStream = fs.createReadStream(notification.video);
await bot.sendVideo(notification.chat_id, videoStream);
} catch (err) {
this.logger.log('warn', `Telegram Notifier: Video not found: ${notification.video}`);
}
}
this.end();
} catch (err) {
const endOptions = {
end: 'error',
messageLog: `Telegram notifier: ${err.message}`
};
this.end(endOptions);
}
}
}
module.exports = telegramNotifier;