Skip to content

Commit 8efd380

Browse files
author
Denis
committed
export and import configuration
1 parent afab16d commit 8efd380

File tree

4 files changed

+190
-17
lines changed

4 files changed

+190
-17
lines changed

bot.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ module.exports.emoji = emoji;
1010
module.exports.exec = exec;
1111

1212
var init = require('./init.js');
13-
module.exports.init = init;
1413
var config = require('./config.js');
1514
var edit = require('./edit.js');
15+
var impex = require('./impex.js');
16+
module.exports.init = init;
17+
module.exports.config = config;
1618
module.exports.edit = edit;
1719

1820
config.loadConfigurationFile().then(() => {
@@ -88,15 +90,17 @@ function start_bot(){
8890
}
8991

9092
/* EXPORT from config to data */
91-
if(msg.text == "/export" && checkAdmins(msg.from) == 1){
92-
console.log("export");
93-
return;
93+
if(msg.text.indexOf("/export ") == 0 && checkAdmins(msg.from) == 1){
94+
var file = msg.text.split(' ')[1];
95+
if(file != undefined && file != "")
96+
impex.export_configuration(bot, file);
9497
}
9598

9699
/* IMPORT from data to config */
97-
if(msg.text == "/import" && checkAdmins(msg.from) == 1){
98-
console.log("import");
99-
return;
100+
if(msg.text.indexOf("/import ") == 0 && checkAdmins(msg.from) == 1){
101+
var file = msg.text.split(' ')[1];
102+
if(file != undefined && file != "")
103+
impex.import_configuration(bot, file, msg);
100104
}
101105

102106
/* Back button */

config.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
var bot = module.parent.exports;
22

3-
var find = require('array.prototype.find');
4-
5-
function loadConfigurationFile(){
3+
function loadConfigurationFile(filename = process.argv[2]){
64
return new Promise(function(ok, fail){
7-
if(process.argv[2] == undefined){
5+
if(filename == undefined){
86
fail("No configuration file");
97
} else {
10-
bot.fs.readFile(process.argv[2], {encoding: 'utf-8'}, function(err,data){
8+
bot.fs.readFile(filename, {encoding: 'utf-8'}, function(err,data){
119
if (err){
1210
fail(err.toString());
1311
} else {
@@ -82,7 +80,7 @@ function loadConfigurationFile(){
8280
if(!hasRoot)
8381
fail("No root catalog in menu at parrent position");
8482

85-
checkEmptyStringInConfig(last_string).then(() => {
83+
checkEmptyStringInConfig(last_string, filename).then(() => {
8684
ok();
8785
}, error => {
8886
fail(error);
@@ -94,11 +92,11 @@ function loadConfigurationFile(){
9492
}
9593
module.exports.loadConfigurationFile = loadConfigurationFile;
9694

97-
function checkEmptyStringInConfig(str){
95+
function checkEmptyStringInConfig(str, filename){
9896
return new Promise(function(ok, fail){
9997
if(str != ""){
10098
// add empty string
101-
bot.exec("echo >> " + process.argv[2], function(error, out, err){
99+
bot.exec("echo >> " + filename, function(error, out, err){
102100
if(error != null)
103101
fail("Error when add empty string to end of config file: " + error.toString());
104102
if(err != "")

impex.js

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
var bot = module.parent.exports;
2+
3+
const IMPEX_EXPORT = 0;
4+
const IMPEX_IMPORT = 1;
5+
6+
function export_configuration(telegram, filename){
7+
// clear export file
8+
clearFile(filename);
9+
10+
// fill export file
11+
generateNewFile(IMPEX_EXPORT, telegram, filename);
12+
13+
}
14+
module.exports.export_configuration = export_configuration;
15+
16+
function generateNewFile(impex, telegram, filename, msg){
17+
return new Promise(function(theend){
18+
var keys = Object.keys(bot.init.menu);
19+
20+
var count_loops = 0;
21+
var finished_loops = 0
22+
23+
for(var i = 0; i < keys.length; i++){
24+
for(var ii = 0; ii < bot.init.menu[keys[i]].values.length; ii++){
25+
count_loops++;
26+
var item = bot.init.menu[keys[i]].values[ii];
27+
var config = bot.init.menu[keys[i]].parrent + bot.init.DELIMETER + keys[i] + bot.init.DELIMETER + item;
28+
29+
indexActions(impex, telegram, config, item, bot.init.menu[item].actions, msg).then(new_config => {
30+
bot.exec("echo \"" + new_config.replace(/"/g, "\\\"") + "\" >> " + filename + "\n", function(error, out, err){
31+
if(error != null)
32+
console.log("ERROR", "impex="+impex, "add string to impex file["+impex+"]: " + error.toString());
33+
if(err != "")
34+
console.log("ERROR", "impex="+impex, "add string to impex file["+impex+"]: " + err.toString());
35+
36+
finished_loops++;
37+
});
38+
}, error => {
39+
console.log("ERROR", "impex="+impex, error.item, error.msg);
40+
});
41+
}
42+
}
43+
44+
var loopsInterval = setInterval(function(){
45+
if(count_loops == finished_loops){
46+
clearInterval(loopsInterval);
47+
theend();
48+
}
49+
}, 200);
50+
});
51+
}
52+
53+
/*
54+
* impex - route variable
55+
* 0 - export
56+
* 1 - import
57+
*/
58+
function indexActions(impex, telegram, config, item, actions, msg, index = 0){
59+
return new Promise(function(ok, fail){
60+
if(actions[index] == undefined)
61+
ok(config);
62+
63+
var impex_action = actions[index];
64+
if(["voice", "sticker", "photo", "video", "document"].indexOf(impex_action.type) != -1){
65+
// export
66+
if(impex == IMPEX_EXPORT){
67+
telegram.downloadFile(impex_action.value, bot.init.DATA_FOLDER).then(file => {
68+
impex_action.value = file;
69+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
70+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
71+
ok(new_config);
72+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
73+
}).catch(e => { fail({'item':item, 'msg':e}) });
74+
}
75+
76+
// import
77+
if(impex == IMPEX_IMPORT){
78+
if(impex_action.type == "voice"){
79+
telegram.sendVoice(msg.from.id, impex_action.value).then(data => {
80+
impex_action.value = data.voice.file_id;
81+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
82+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
83+
ok(new_config);
84+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
85+
}).catch(e => { fail({'item':item, 'msg':e}) });
86+
}
87+
88+
if(impex_action.type == "sticker"){
89+
telegram.sendSticker(msg.from.id, impex_action.value).then(data => {
90+
impex_action.value = data.sticker.file_id;
91+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
92+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
93+
ok(new_config);
94+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
95+
}).catch(e => { fail({'item':item, 'msg':e}) });
96+
}
97+
98+
if(impex_action.type == "photo"){
99+
telegram.sendPhoto(msg.from.id, impex_action.value).then(data => {
100+
impex_action.value = data.photo[data.photo.length-1].file_id;
101+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
102+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
103+
ok(new_config);
104+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
105+
}).catch(e => { fail({'item':item, 'msg':e}) });
106+
}
107+
108+
if(impex_action.type == "video"){
109+
telegram.sendVideo(msg.from.id, impex_action.value).then(data => {
110+
impex_action.value = data.video.file_id;
111+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
112+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
113+
ok(new_config);
114+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
115+
}).catch(e => { fail({'item':item, 'msg':e}) });
116+
}
117+
118+
if(impex_action.type == "document"){
119+
telegram.sendDocument(msg.from.id, impex_action.value).then(data => {
120+
impex_action.value = data.document.file_id;
121+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
122+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
123+
ok(new_config);
124+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
125+
}).catch(e => { fail({'item':item, 'msg':e}) });
126+
}
127+
}
128+
129+
return;
130+
}
131+
132+
config += bot.init.DELIMETER + JSON.stringify(impex_action);
133+
indexActions(impex, telegram, config, item, actions, msg, index+1).then(new_config => {
134+
ok(new_config);
135+
}, error => { fail({'item':error.item, 'msg':error.msg}) });
136+
});
137+
}
138+
139+
function clearFile(filename){
140+
bot.exec("cat /dev/null > " + filename + "\n", function(error, out, err){
141+
if(error != null)
142+
console.log("ERROR", filename, "clear file: " + error.toString());
143+
if(err != "")
144+
console.log("ERROR", filename, "clear file: " + err.toString());
145+
});
146+
}
147+
148+
function import_configuration(telegram, filename_from, msg){
149+
var filename_to = process.argv[2];
150+
151+
// clear menu
152+
bot.init.menu = {};
153+
154+
// fill menu
155+
bot.config.loadConfigurationFile(filename_from).then(() => {
156+
// clear new config file
157+
clearFile(filename_to);
158+
159+
// fill new config file
160+
generateNewFile(IMPEX_IMPORT, telegram, filename_to, msg).then(() => {
161+
// refresh menu with new config
162+
bot.init.menu = {};
163+
bot.config.loadConfigurationFile(filename_to).then(() => { }, error => {
164+
console.log("ERROR", "import", "loadConfigurationFile", filename_to, error);
165+
});
166+
});
167+
168+
}, error => {
169+
console.log("ERROR", "import", "loadConfigurationFile", filename_from, error);
170+
});
171+
}
172+
module.exports.import_configuration = import_configuration

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "telegram-bot-menu",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "",
55
"main": "bot.js",
66
"scripts": {
@@ -17,7 +17,6 @@
1717
},
1818
"homepage": "https://github.com/zZoMROT/telegram-bot-menu#readme",
1919
"dependencies": {
20-
"array.prototype.find": "^2.0.4",
2120
"node-emoji": "^1.8.1",
2221
"node-telegram-bot-api": "^0.30.0"
2322
}

0 commit comments

Comments
 (0)