Skip to content

Commit

Permalink
Adds pluginFolder
Browse files Browse the repository at this point in the history
  • Loading branch information
mullwar committed May 4, 2017
1 parent 142ba1a commit 0abb2f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -218,9 +219,19 @@ This code adds emoji to every `text` message.

## Plugins

Use ```bot.use(require(<plugin_path>))``` 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(<plugin_path>))``` to plug an external plugin.

***[Check out build-in plugin folder!](/plugins)***

### Plugin structure

Expand Down
21 changes: 13 additions & 8 deletions lib/telebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 || {};

Expand Down Expand Up @@ -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));
}

}
Expand Down Expand Up @@ -236,7 +241,7 @@ class TeleBot {
updateList, props: extendProps(props, eventProps)
});

updateList = mod.list;
updateList = mod.updateList;
props = mod.props;

// Every Telegram update
Expand Down

0 comments on commit 0abb2f8

Please sign in to comment.