|
| 1 | +var readline = require("readline"); |
| 2 | +var botgram = require("botgram"); |
| 3 | +var fs = require("fs"); |
| 4 | +var util = require("util"); |
| 5 | + |
| 6 | +// Wizard functions |
| 7 | + |
| 8 | +function stepAuthToken(rl, config) { |
| 9 | + return question(rl, "First, enter your bot API token: ") |
| 10 | + .then(function (token) { |
| 11 | + token = token.trim(); |
| 12 | + //if (!/^\d{5,}:[a-zA-Z0-9_+/-]{20,}$/.test(token)) |
| 13 | + // throw new Error(); |
| 14 | + config.authToken = token; |
| 15 | + return createBot(token); |
| 16 | + }).catch(function (err) { |
| 17 | + console.error("Invalid token was entered, please try again.\n%s\n", err); |
| 18 | + return stepAuthToken(rl, config); |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +function stepOwner(rl, config, getNextMessage) { |
| 23 | + console.log("Waiting for a message..."); |
| 24 | + return getNextMessage().then(function (msg) { |
| 25 | + var prompt = util.format("Should %s «%s» (%s) be the bot's owner? [y/n]: ", msg.chat.type, msg.chat.name, msg.chat.id); |
| 26 | + return question(rl, prompt) |
| 27 | + .then(function (answer) { |
| 28 | + console.log(); |
| 29 | + answer = answer.trim().toLowerCase(); |
| 30 | + if (answer === "y" || answer === "yes") |
| 31 | + config.owner = msg.chat.id; |
| 32 | + else |
| 33 | + return stepOwner(rl, config, getNextMessage); |
| 34 | + }); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function configWizard(options) { |
| 39 | + var rl = readline.createInterface({ |
| 40 | + input: process.stdin, |
| 41 | + output: process.stdout, |
| 42 | + }); |
| 43 | + var config = {}; |
| 44 | + var bot = null; |
| 45 | + |
| 46 | + return Promise.resolve() |
| 47 | + .then(function () { |
| 48 | + return stepAuthToken(rl, config); |
| 49 | + }) |
| 50 | + .then(function (bot_) { |
| 51 | + bot = bot_; |
| 52 | + console.log("\nNow, talk to me so I can discover your Telegram user:\n%s\n", bot.link()); |
| 53 | + }) |
| 54 | + .then(function () { |
| 55 | + var getNextMessage = getPromiseFactory(bot); |
| 56 | + return stepOwner(rl, config, getNextMessage); |
| 57 | + }) |
| 58 | + .then(function () { |
| 59 | + console.log("All done, writing the configuration..."); |
| 60 | + var contents = JSON.stringify(config, null, 4) + "\n"; |
| 61 | + return writeFile(options.configFile, contents); |
| 62 | + }) |
| 63 | + |
| 64 | + .catch(function (err) { |
| 65 | + console.error("Error, wizard crashed:\n%s", err.stack); |
| 66 | + process.exit(1); |
| 67 | + }) |
| 68 | + .then(function () { |
| 69 | + rl.close(); |
| 70 | + if (bot) bot.stop(); |
| 71 | + process.exit(0); |
| 72 | + }); |
| 73 | +} |
| 74 | + |
| 75 | +// Promise utilities |
| 76 | + |
| 77 | +function question(interface, query) { |
| 78 | + return new Promise(function (resolve, reject) { |
| 79 | + interface.question(query, resolve); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +function writeFile(file, contents) { |
| 84 | + return new Promise(function (resolve, reject) { |
| 85 | + fs.writeFile(file, contents, "utf-8", function (err) { |
| 86 | + if (err) reject(err); |
| 87 | + else resolve(); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function createBot(token) { |
| 93 | + return new Promise(function (resolve, reject) { |
| 94 | + var bot = botgram(token); |
| 95 | + bot.on("error", function (err) { |
| 96 | + bot.stop(); |
| 97 | + reject(err); |
| 98 | + }); |
| 99 | + bot.on("ready", resolve.bind(this, bot)); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +function getPromiseFactory(bot) { |
| 104 | + var resolveCbs = []; |
| 105 | + bot.message(function (msg, reply, next) { |
| 106 | + if (!msg.queued) { |
| 107 | + resolveCbs.forEach(function (resolve) { |
| 108 | + resolve(msg); |
| 109 | + }); |
| 110 | + resolveCbs = []; |
| 111 | + } |
| 112 | + next(); |
| 113 | + }); |
| 114 | + return function () { |
| 115 | + return new Promise(function (resolve, reject) { |
| 116 | + resolveCbs.push(resolve); |
| 117 | + }); |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +exports.configWizard = configWizard; |
0 commit comments