|
| 1 | +process.env.NTBA_FIX_319 = 1; |
| 2 | + |
| 3 | +var TelegramBot = require('node-telegram-bot-api'); |
| 4 | + |
| 5 | +var emoji = require('node-emoji'); |
| 6 | +var fs = require('fs'); |
| 7 | +var exec = require('child_process').exec; |
| 8 | +module.exports.fs = fs; |
| 9 | +module.exports.emoji = emoji; |
| 10 | +module.exports.exec = exec; |
| 11 | + |
| 12 | +var init = require('./init.js'); |
| 13 | +module.exports.init = init; |
| 14 | +var config = require('./config.js'); |
| 15 | +var edit = require('./edit.js'); |
| 16 | +module.exports.edit = edit; |
| 17 | + |
| 18 | +config.loadConfigurationFile().then(() => { |
| 19 | + config.getDataFolder().then(() => { |
| 20 | + config.checkMode().then(() => { |
| 21 | + start_bot(); |
| 22 | + }, error => { |
| 23 | + printSyntax(error); |
| 24 | + }); |
| 25 | + }, error => { |
| 26 | + printSyntax(error); |
| 27 | + }); |
| 28 | +}, error => { |
| 29 | + printSyntax(error); |
| 30 | +}); |
| 31 | + |
| 32 | +function start_bot(){ |
| 33 | + var bot = new TelegramBot(init.TOKEN, {polling: true}); |
| 34 | + |
| 35 | + bot.on('message', function(msg){ |
| 36 | + // Actions to SAVE |
| 37 | + if(edit.edit_actions['add_actions']["userid"+msg.from.id] != undefined){ |
| 38 | + var content; |
| 39 | + var content_type; |
| 40 | + if(msg.text != undefined){ |
| 41 | + content_type = "text"; |
| 42 | + content = msg.text; |
| 43 | + } else if(msg.voice != undefined){ |
| 44 | + content_type = "voice"; |
| 45 | + content = msg.voice.file_id; |
| 46 | + } else if(msg.sticker != undefined){ |
| 47 | + content_type = "sticker"; |
| 48 | + content = msg.sticker.file_id; |
| 49 | + } else if(msg.photo != undefined){ |
| 50 | + content_type = "photo"; |
| 51 | + content = msg.photo[msg.photo.length-1].file_id; |
| 52 | + } else if(msg.video != undefined){ |
| 53 | + content_type = "video"; |
| 54 | + content = msg.video.file_id; |
| 55 | + } else if(msg.location != undefined){ |
| 56 | + content_type = "location"; |
| 57 | + content = msg.location.longitude + "_" + msg.location.latitude; |
| 58 | + } else if(msg.document != undefined){ |
| 59 | + content_type = "document"; |
| 60 | + content = msg.document.file_id; |
| 61 | + } else if(msg.contact != undefined){ |
| 62 | + content_type = "contact"; |
| 63 | + var name = msg.contact.first_name; |
| 64 | + if(name == '') |
| 65 | + name = msg.contact.last_name; |
| 66 | + content = msg.contact.phone_number + "_" + name; |
| 67 | + } |
| 68 | + |
| 69 | + edit.edit_actions['add_actions']["userid"+msg.from.id].actions.push({'type':content_type, 'value':content}); |
| 70 | + |
| 71 | + bot.deleteMessage(msg.from.id, edit.edit_actions['add_actions']["userid"+msg.from.id].btn_save_id).then(() => { |
| 72 | + var key = edit.edit_actions['add_actions']["userid"+msg.from.id].catalog; |
| 73 | + var index = init.menu[key].values.indexOf(edit.edit_actions['add_actions']["userid"+msg.from.id].menu_item); |
| 74 | + bot.sendMessage(msg.from.id, init.MSG_ADD_ACTION_REQ, init.options_with_save(init.menu[key].values[index-1], checkAdmins(msg.from), msg.from.id)).then(btn_save => { |
| 75 | + edit.edit_actions['add_actions']["userid"+msg.from.id].btn_save_id = btn_save.message_id; |
| 76 | + }); |
| 77 | + }); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if(msg.text == undefined) |
| 82 | + return; |
| 83 | + |
| 84 | + /* Start */ |
| 85 | + if(msg.text == "/start"){ |
| 86 | + bot.sendMessage(msg.from.id, init.MSG_START, init.options(undefined, checkAdmins(msg.from))); |
| 87 | + return edit.reset_variables(msg.from.id); |
| 88 | + } |
| 89 | + |
| 90 | + /* EXPORT from config to data */ |
| 91 | + if(msg.text == "/export" && checkAdmins(msg.from) == 1){ |
| 92 | + console.log("export"); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + /* IMPORT from data to config */ |
| 97 | + if(msg.text == "/import" && checkAdmins(msg.from) == 1){ |
| 98 | + console.log("import"); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + /* Back button */ |
| 103 | + if(msg.text.indexOf(emoji.get('arrow_left')+" ") == 0){ |
| 104 | + var catalog = msg.text.split(emoji.get('arrow_left')+" ")[1]; |
| 105 | + if(init.menu[catalog] != undefined){ |
| 106 | + bot.sendMessage(msg.from.id, init.MSG_BACK(catalog), init.options(catalog, checkAdmins(msg.from))); |
| 107 | + } |
| 108 | + return edit.reset_variables(msg.from.id); |
| 109 | + } |
| 110 | + |
| 111 | + /* Button */ |
| 112 | + if(init.menu[msg.text] != undefined){ |
| 113 | + var message = init.MSG_DEFAULT; |
| 114 | + |
| 115 | + if(init.menu[msg.text].actions.length > 0) |
| 116 | + sendActions(bot, msg); |
| 117 | + else |
| 118 | + bot.sendMessage(msg.from.id, message, init.options(msg.text, checkAdmins(msg.from))); |
| 119 | + |
| 120 | + return edit.reset_variables(msg.from.id); |
| 121 | + } |
| 122 | + |
| 123 | + /* Button without action */ |
| 124 | + if(init.menu[msg.text.split("|")[0]] != undefined && msg.text.split("|")[1] == "wa"){ |
| 125 | + var message = init.MSG_DEFAULT; |
| 126 | + |
| 127 | + bot.sendMessage(msg.from.id, message, init.options(msg.text.split("|")[0], checkAdmins(msg.from))); |
| 128 | + |
| 129 | + return edit.reset_variables(msg.from.id); |
| 130 | + } |
| 131 | + |
| 132 | + /* Delete menu item */ |
| 133 | + if(msg.text.indexOf(emoji.get('heavy_minus_sign')+" ") == 0){ |
| 134 | + var key = msg.text.split("delete ")[1].split("|")[0]; |
| 135 | + if(checkAdmins(msg.from) == 1){ |
| 136 | + var index = msg.text.split("delete ")[1].split("|")[1]; |
| 137 | + var item = init.menu[key].values[index-1]; |
| 138 | + edit.deleteItem(key, item, index).then(() => { |
| 139 | + bot.sendMessage(msg.from.id, init.MSG_DELETE_ITEM(key, item), init.options(key, checkAdmins(msg.from))); |
| 140 | + }, error => { |
| 141 | + bot.sendMessage(msg.from.id, init.MSG_DELETE_FAIL(key, item, error), init.options(key, checkAdmins(msg.from))); |
| 142 | + bot.sendMessage(msg.from.id, init.MSG_DESYNC, init.options(key, checkAdmins(msg.from))); |
| 143 | + }); |
| 144 | + } else { |
| 145 | + bot.sendMessage(msg.from.id, init.MSG_EDIT_OFF, init.options(key, checkAdmins(msg.from))); |
| 146 | + } |
| 147 | + return edit.reset_variables(msg.from.id); |
| 148 | + } |
| 149 | + |
| 150 | + /* Add menu item */ |
| 151 | + if(msg.text.indexOf(emoji.get('heavy_plus_sign')+" ") == 0){ |
| 152 | + var key = msg.text.split("add button to ")[1].split(" ]")[0]; |
| 153 | + var message = init.MSG_ADD_ITEM_REQ; |
| 154 | + if(checkAdmins(msg.from) != 1) |
| 155 | + message = init.MSG_EDIT_OFF; |
| 156 | + else |
| 157 | + edit.set_variable('add_item', msg.from.id, { 'catalog': key, 'parrent': init.menu[key].parrent }); |
| 158 | + |
| 159 | + bot.sendMessage(msg.from.id, message, init.options(key, checkAdmins(msg.from))); |
| 160 | + return edit.reset_variables(msg.from.id, 'add_item'); |
| 161 | + } |
| 162 | + |
| 163 | + /* Edit actions menu item */ |
| 164 | + if(msg.text.indexOf(emoji.get('writing_hand')+" ") == 0){ |
| 165 | + var key = msg.text.split("edit actions answer ")[1].split("|")[0]; |
| 166 | + var index = msg.text.split("edit actions answer ")[1].split("|")[1]; |
| 167 | + var message = init.MSG_ADD_ACTION_REQ; |
| 168 | + if(checkAdmins(msg.from) != 1) |
| 169 | + message = init.MSG_EDIT_OFF; |
| 170 | + else |
| 171 | + edit.set_variable('add_actions', msg.from.id, { 'catalog': key } ); |
| 172 | + |
| 173 | + bot.sendMessage(msg.from.id, message, init.options_with_save(init.menu[key].values[index-1], checkAdmins(msg.from), msg.from.id)).then(btn_save => { |
| 174 | + edit.set_variable('add_actions', msg.from.id, { 'catalog': key, 'menu_item': init.menu[key].values[index-1], 'btn_save_id': btn_save.message_id, 'actions': [] }); |
| 175 | + }); |
| 176 | + return edit.reset_variables(msg.from.id, 'add_actions'); |
| 177 | + } |
| 178 | + |
| 179 | + // Input menu item for add new item |
| 180 | + if(edit.edit_actions.add_item["userid"+msg.from.id]){ |
| 181 | + edit.addItem(msg.from.id, msg.text).then(key => { |
| 182 | + bot.sendMessage(msg.from.id, init.MSG_ADD_ITEM(msg.text), init.options(key, checkAdmins(msg.from))); |
| 183 | + }, error => { |
| 184 | + bot.sendMessage(msg.from.id, init.MSG_ADD_FAIL(msg.text, error), init.options(undefined, checkAdmins(msg.from))); |
| 185 | + bot.sendMessage(msg.from.id, init.MSG_DESYNC, init.options(undefined, checkAdmins(msg.from))); |
| 186 | + }); |
| 187 | + |
| 188 | + return edit.reset_variables(msg.from.id); |
| 189 | + } |
| 190 | + }); |
| 191 | + |
| 192 | + bot.on('callback_query', function (msg) { |
| 193 | + if(msg.data.indexOf('Cancel') !== -1 && edit.edit_actions['add_actions']["userid"+msg.from.id] != undefined){ |
| 194 | + bot.deleteMessage(msg.from.id, edit.edit_actions['add_actions']["userid"+msg.from.id].btn_save_id).then(() => { |
| 195 | + bot.sendMessage(msg.from.id, init.MSG_EDIT_ITEM_CANC, init.options(edit.edit_actions['add_actions']["userid"+msg.from.id].catalog, checkAdmins(msg.from))); |
| 196 | + edit.reset_variables(msg.from.id); |
| 197 | + }); |
| 198 | + } |
| 199 | + |
| 200 | + if(msg.data.indexOf('Save') !== -1 && edit.edit_actions['add_actions']["userid"+msg.from.id] != undefined){ |
| 201 | + bot.deleteMessage(msg.from.id, edit.edit_actions['add_actions']["userid"+msg.from.id].btn_save_id).then(() => { |
| 202 | + edit.saveActions(msg.from.id).then(() => { |
| 203 | + bot.sendMessage(msg.from.id, init.MSG_EDIT_ITEM(edit.edit_actions['add_actions']["userid"+msg.from.id].menu_item), init.options(edit.edit_actions['add_actions']["userid"+msg.from.id].catalog, checkAdmins(msg.from))); |
| 204 | + edit.reset_variables(msg.from.id); |
| 205 | + }, error => { |
| 206 | + bot.sendMessage(msg.from.id, init.MSG_EDIT_FAIL(edit.edit_actions['add_actions']["userid"+msg.from.id].menu_item, error), init.options(edit.edit_actions['add_actions']["userid"+msg.from.id].catalog, checkAdmins(msg.from))); |
| 207 | + edit.reset_variables(msg.from.id); |
| 208 | + }); |
| 209 | + }); |
| 210 | + } |
| 211 | + }); |
| 212 | +} |
| 213 | + |
| 214 | +function checkAdmins(from){ |
| 215 | + var mode = init.MODE; |
| 216 | + if(init.admins.length > 0 && |
| 217 | + init.admins.indexOf(from.id) == -1 && |
| 218 | + init.admins.indexOf(from.username) == -1){ |
| 219 | + |
| 220 | + mode = 0; |
| 221 | + } |
| 222 | + |
| 223 | + return mode; |
| 224 | +} |
| 225 | + |
| 226 | +function sendActions(bot, msg, _index = 0){ |
| 227 | + if(init.menu[msg.text].actions[_index] == undefined) |
| 228 | + return; |
| 229 | + |
| 230 | + var content = init.menu[msg.text].actions[_index].value; |
| 231 | + |
| 232 | + if(init.menu[msg.text].actions[_index].type == "text"){ |
| 233 | + bot.sendMessage(msg.from.id, content, init.options(msg.text, checkAdmins(msg.from))).then(() => { sendActions(bot, msg, _index+1) }); |
| 234 | + } else if(init.menu[msg.text].actions[_index].type == "photo"){ |
| 235 | + bot.sendPhoto(msg.from.id, content).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 236 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, error.toString()); |
| 237 | + }); |
| 238 | + } else if(init.menu[msg.text].actions[_index].type == "voice"){ |
| 239 | + bot.sendVoice(msg.from.id, content).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 240 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, error.toString()); |
| 241 | + }); |
| 242 | + } else if(init.menu[msg.text].actions[_index].type == "sticker"){ |
| 243 | + bot.sendSticker(msg.from.id, content).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 244 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, error.toString()); |
| 245 | + }); |
| 246 | + } else if(init.menu[msg.text].actions[_index].type == "video"){ |
| 247 | + bot.sendVideo(msg.from.id, content).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 248 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, error.toString()); |
| 249 | + }); |
| 250 | + } else if(init.menu[msg.text].actions[_index].type == "location"){ |
| 251 | + var longitude_latitude = content.split('_'); |
| 252 | + if(longitude_latitude.length < 2){ |
| 253 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, "BED FORMAT"); |
| 254 | + sendActions(bot, msg, _index+1); |
| 255 | + return; |
| 256 | + } |
| 257 | + |
| 258 | + bot.sendLocation(msg.from.id, longitude_latitude[0], longitude_latitude[1]).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 259 | + console.log("ERROR", "sendActions", msg.text, error.toString()); |
| 260 | + }); |
| 261 | + } else if(init.menu[msg.text].actions[_index].type == "document"){ |
| 262 | + bot.sendDocument(msg.from.id, content).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 263 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, error.toString()); |
| 264 | + }); |
| 265 | + } else if(init.menu[msg.text].actions[_index].type == "contact"){ |
| 266 | + var phone_name = content.split('_'); |
| 267 | + if(phone_name.length < 2){ |
| 268 | + console.log("ERROR", "sendActions", msg.text, init.menu[msg.text].actions[_index].type, "BED FORMAT"); |
| 269 | + sendActions(bot, msg, _index+1); |
| 270 | + return; |
| 271 | + } |
| 272 | + |
| 273 | + bot.sendContact(msg.from.id, phone_name[0], phone_name[1]).then(() => { sendActions(bot, msg, _index+1) }).catch(error => { |
| 274 | + console.log("ERROR", "sendActions", msg.text, error.toString()); |
| 275 | + }); |
| 276 | + } |
| 277 | + |
| 278 | + return; |
| 279 | +} |
| 280 | + |
| 281 | +function printSyntax(message){ |
| 282 | + if(message != undefined) |
| 283 | + console.log(message); |
| 284 | + console.log("Syntax: node bot.js menu.file data.folder [mode [username|telegram_id|all]]"); |
| 285 | + console.log("\tmenu.file \t- file with menu catalogs in format:"); |
| 286 | + console.log("\t \t parrent"+init.DELIMETER+"catalog"+init.DELIMETER+"menubutton["+init.DELIMETER+"action]"); |
| 287 | + console.log("\t \t You can set DELIMETER in init.js"); |
| 288 | + console.log("\tdata.folder \t- path to folder with data for files from menu.file"); |
| 289 | + console.log("\tmode \t- bot mode: \"edit\" for update menu"); |
| 290 | + console.log("\tusername|telegram_id\t- username or telegram_id users separated by commas which can edit menu"); |
| 291 | + console.log("\t \t if mode=edit without this parameter, all users can edit menu"); |
| 292 | + console.log("\t \t You can use \"all\" to all users can edit menu"); |
| 293 | +} |
0 commit comments