|
| 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 |
0 commit comments