-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
}); | ||
|
||
} | ||
|
||
}; |