forked from LukeSkywalker92/TeleFrame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotonly.js
98 lines (84 loc) · 2.41 KB
/
botonly.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
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
/*
Script for only running the telegram bot to save the images and videos to
the images folder specified in the config
*/
const {
logger,
rendererLogger
} = require('./js/logger')
const {config} = require('./js/configuration')
const telebot = require('./js/bot')
const fs = require('fs');
if (config.botToken === 'bot-disabled') {
logger.error('Error running bot only version of TeleFrame! No valid botToken is configured.');
return;
}
logger.info('Running bot only version of TeleFrame ...');
var ImageWatchdog = class {
constructor(imageFolder, imageCount, logger) {
this.imageFolder = imageFolder;
this.imageCount = imageCount;
this.logger = logger;
this.images = []
//get paths of already downloaded images
if (fs.existsSync(this.imageFolder + '/' + "images.json")) {
fs.readFile(this.imageFolder + '/' + "images.json", (err, data) => {
if (err) throw err;
var jsonData = JSON.parse(data);
for (var image in jsonData) {
this.images.push(jsonData[image]);
}
});
} else {
this.saveImageArray()
}
}
newImage(src, sender, caption, chatId, chatName, messageId) {
//handle new incoming image
this.images.unshift({
'src': src,
'sender': sender,
'caption': caption,
'chatId': chatId,
'chatName': chatName,
'messageId': messageId,
'unseen': true
});
if (this.images.length >= this.imageCount) {
this.images.pop();
}
var type;
if (src.split('.').pop() == 'mp4') {
type = 'video';
} else {
type = 'image';
}
this.saveImageArray();
}
deleteImage(idx2bedeleted) {
//deleteImage
if (this.images.length <= idx2bedeleted){
return
}
if (this.images[idx2bedeleted].starred) {
this.imageCount--;
}
this.images.splice(idx2bedeleted, 1)
this.saveImageArray();
}
saveImageArray() {
var self = this;
// stringify JSON Object
var jsonContent = JSON.stringify(this.images);
fs.writeFile(this.imageFolder + '/' + "images.json", jsonContent, 'utf8', function(err) {
if (err) {
self.logger.error("An error occured while writing JSON Object to File.");
return console.log(err);
}
});
}
}
// create imageWatchdog and bot
const imageWatchdog = new ImageWatchdog(config.imageFolder, config.imageCount, logger);
var bot = new telebot(imageWatchdog, logger, config);
bot.startBot()