Skip to content

Commit

Permalink
New plugin - namedButtons
Browse files Browse the repository at this point in the history
  • Loading branch information
mullwar committed May 4, 2017
1 parent 0abb2f8 commit 0cc714e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/plugin-namedButtons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const TeleBot = require('../');

const BUTTONS = {
hello: {
label: '👋 Hello',
command: '/hello'
},
world: {
label: '🌍 World',
command: '/world'
},
hide: {
label: '⌨️ Hide keyboard',
command: '/hide'
}
};

const bot = new TeleBot({
token: 'TELEGRAM_BOT_TOKEN',
usePlugin: ['namedButtons'],
pluginConfig: {
namedButtons: {
buttons: BUTTONS
}
}
});


bot.on('/hello', (msg) => msg.reply.text('Hello command!'));
bot.on('/world', (msg) => msg.reply.text('World command!'));
bot.on('/hide', (msg) => msg.reply.text('Type /start to show keyboard again.', {markup: 'hide'}));

bot.on('/start', (msg) => {

let markup = bot.keyboard([
[BUTTONS.hello.label, BUTTONS.world.label],
[BUTTONS.hide.label]
], {resize: true});

return bot.sendMessage(msg.from.id, 'See keyboard below.', {markup});

});

bot.start();
34 changes: 34 additions & 0 deletions plugins/namedButtons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Named buttons which triggers bot commands.
*/

module.exports = {

id: 'namedButtons',
defaultConfig: {
buttons: {
// myButton: {
// label: '😄 My Button Name',
// command: '/myBotCommand'
// }
}
},

plugin(bot, cfg) {

const buttons = cfg.buttons || {};

bot.on('text', (msg, props) => {
const text = msg.text;
for (let buttonId in buttons) {
const button = buttons[buttonId];
if (button.label === text) {
return bot.event(button.command, msg, props);
}
}

});

}

};

0 comments on commit 0cc714e

Please sign in to comment.