forked from zZoMROT/telegram-bot-menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impex.js
172 lines (147 loc) · 6.12 KB
/
impex.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
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
165
166
167
168
169
170
171
172
var bot = module.parent.exports;
const IMPEX_EXPORT = 0;
const IMPEX_IMPORT = 1;
function export_configuration(telegram, filename){
// clear export file
clearFile(filename);
// fill export file
generateNewFile(IMPEX_EXPORT, telegram, filename);
}
module.exports.export_configuration = export_configuration;
function generateNewFile(impex, telegram, filename, msg){
return new Promise(function(theend){
var keys = Object.keys(bot.init.menu);
var count_loops = 0;
var finished_loops = 0
for(var i = 0; i < keys.length; i++){
for(var ii = 0; ii < bot.init.menu[keys[i]].values.length; ii++){
count_loops++;
var item = bot.init.menu[keys[i]].values[ii];
var config = bot.init.menu[keys[i]].parent + bot.init.DELIMITER + keys[i] + bot.init.DELIMITER + item;
indexActions(impex, telegram, config, item, bot.init.menu[item].actions, msg).then(new_config => {
bot.exec("echo \"" + new_config.replace(/"/g, "\\\"") + "\" >> " + filename + "\n", function(error, out, err){
if(error != null)
console.error(init.MSG_TERM_ERROR_PREFIX, "impex="+impex, "add string to impex file["+impex+"]: " + error.toString());
if(err != "")
console.error(init.MSG_TERM_ERROR_PREFIX, "impex="+impex, "add string to impex file["+impex+"]: " + err.toString());
finished_loops++;
});
}, error => {
console.error(init.MSG_TERM_ERROR_PREFIX, "impex="+impex, error.item, error.msg);
});
}
}
var loopsInterval = setInterval(function(){
if(count_loops == finished_loops){
clearInterval(loopsInterval);
theend();
}
}, 200);
});
}
/*
* impex - route variable
* 0 - export
* 1 - import
*/
function indexActions(impex, telegram, config, item, actions, msg, index = 0){
return new Promise(function(ok, fail){
if(actions[index] == undefined)
ok(config);
var impex_action = actions[index];
if(["voice", "sticker", "photo", "video", "document"].indexOf(impex_action.type) != -1){
// export
if(impex == IMPEX_EXPORT){
telegram.downloadFile(impex_action.value, bot.init.DATA_FOLDER).then(file => {
impex_action.value = file;
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
}).catch(e => { fail({'item':item, 'msg':e}) });
}
// import
if(impex == IMPEX_IMPORT){
if(impex_action.type == "voice"){
telegram.sendVoice(msg.from.id, impex_action.value).then(data => {
impex_action.value = data.voice.file_id;
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
}).catch(e => { fail({'item':item, 'msg':e}) });
}
if(impex_action.type == "sticker"){
telegram.sendSticker(msg.from.id, impex_action.value).then(data => {
impex_action.value = data.sticker.file_id;
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
}).catch(e => { fail({'item':item, 'msg':e}) });
}
if(impex_action.type == "photo"){
telegram.sendPhoto(msg.from.id, impex_action.value).then(data => {
impex_action.value = data.photo[data.photo.length-1].file_id;
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
}).catch(e => { fail({'item':item, 'msg':e}) });
}
if(impex_action.type == "video"){
telegram.sendVideo(msg.from.id, impex_action.value).then(data => {
impex_action.value = data.video.file_id;
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
}).catch(e => { fail({'item':item, 'msg':e}) });
}
if(impex_action.type == "document"){
telegram.sendDocument(msg.from.id, impex_action.value).then(data => {
impex_action.value = data.document.file_id;
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
}).catch(e => { fail({'item':item, 'msg':e}) });
}
}
return;
}
config += bot.init.DELIMITER + JSON.stringify(impex_action);
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
ok(new_config);
}, error => { fail({'item':error.item, 'msg':error.msg}) });
});
}
function clearFile(filename){
bot.exec("cat /dev/null > " + filename + "\n", function(error, out, err){
if(error != null)
console.error(init.MSG_TERM_ERROR_PREFIX, filename, "clear file: " + error.toString());
if(err != "")
console.error(init.MSG_TERM_ERROR_PREFIX, filename, "clear file: " + err.toString());
});
}
function import_configuration(telegram, filename_from, msg){
var filename_to = process.argv[2];
// clear menu
bot.init.menu = {};
// fill menu
bot.config.loadConfigurationFile(filename_from).then(() => {
// clear new config file
clearFile(filename_to);
// fill new config file
generateNewFile(IMPEX_IMPORT, telegram, filename_to, msg).then(() => {
// refresh menu with new config
bot.init.menu = {};
bot.config.loadConfigurationFile(filename_to).then(() => { }, error => {
console.error(init.MSG_TERM_ERROR_PREFIX, "import", "loadConfigurationFile", filename_to, error);
});
});
}, error => {
console.error(init.MSG_TERM_ERROR_PREFIX, "import", "loadConfigurationFile", filename_from, error);
});
}
module.exports.import_configuration = import_configuration