diff --git a/README.md b/README.md index 8e8bf29..d0fe55c 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ const bot = new TeleBot({ host: '0.0.0.0', // Webhook server host. port: 443 // Server port. }, - usePlugins: ['shortReply'], // Optional. Use build-in plugins from /plugins folder. + usePlugins: ['askUser'], // Optional. Use build-in plugins from pluginFolder. + pluginFolder: '../plugins/', // Optional. Plugin folder location relative to telebot package. pluginConfig: { // Optional. Plugin configuration. // myPluginName: { // data: 'my custom value' @@ -218,9 +219,19 @@ This code adds emoji to every `text` message. ## Plugins -Use ```bot.use(require())``` to plug an external plugin. +Use `usePlugins` config option to load build-in plugins from `pluginFolder`: -***[Check out plugins folder!](/plugins)*** +```js +const bot = new TeleBot({ + token: 'TELEGRAM_BOT_TOKEN', + usePlugins: ['askUser', 'commandButtons'], + pluginFolder: '../plugins/' +}); +``` + +Or use ```bot.use(require())``` to plug an external plugin. + +***[Check out build-in plugin folder!](/plugins)*** ### Plugin structure diff --git a/lib/telebot.js b/lib/telebot.js index 5d10fa6..889f0cf 100644 --- a/lib/telebot.js +++ b/lib/telebot.js @@ -3,6 +3,7 @@ const webhook = require('./webhook.js'); const standardUpdates = require('./updates.js'); const standardMethods = require('./methods.js'); +const DEFAULT_PLUGIN_FOLDER = '../plugins/'; const DEFAULT_PLUGINS = ['regExpMessage', 'shortReply']; class TeleBot { @@ -17,12 +18,14 @@ class TeleBot { this.cfg = cfg; this.token = cfg.token; - this.pluginConfig = cfg.pluginConfig || {}; this.id = this.token.split(':')[0]; this.api = `https://api.telegram.org/bot${ this.token }`; this.fileLink = `https://api.telegram.org/file/bot${ this.token }/`; + + this.pluginFolder = cfg.pluginFolder || DEFAULT_PLUGIN_FOLDER; this.usePlugins = Array.isArray(cfg.usePlugins) ? cfg.usePlugins : []; this.defaultPlugins = cfg.defaultPlugins !== undefined ? (cfg.defaultPlugins || []) : DEFAULT_PLUGINS; + this.pluginConfig = cfg.pluginConfig || {}; const poll = cfg.polling || {}; @@ -50,17 +53,19 @@ class TeleBot { this.updateTypes = standardUpdates; this.processUpdate = (update, props) => { - for (let name in this.updateTypes) { - if (name in update) { - update = update[name]; - return this.updateTypes[name].call(this, update, props); + if (update) { + for (let name in this.updateTypes) { + if (name in update) { + update = update[name]; + return this.updateTypes[name].call(this, update, props); + } } } }; // Load build-in plugins - for (let name of Object.assign(this.defaultPlugins, this.usePlugins)) { - this.use(require(`../plugins/${ name }`)); + for (let pluginName of Object.assign(this.defaultPlugins, this.usePlugins)) { + this.use(require(this.pluginFolder + pluginName)); } } @@ -236,7 +241,7 @@ class TeleBot { updateList, props: extendProps(props, eventProps) }); - updateList = mod.list; + updateList = mod.updateList; props = mod.props; // Every Telegram update