- Create a Bot in Telegram app and save the Token.
- Install node.js in your machine
- Open a Terminal/CMD and Type
3.1git clone https://github.com/apal21/Telegram-Bot-Custom-Keyboard-Node.js
3.2cd Telegram-Bot-Custom-Keyboard-Node.js
3.3 Enter your token in the first line ofindex.js
file.
3.4npm install
3.5node index.js
const KEYBOARD = {
reply_markup: JSON.stringify({
keyboard: [
['/example', '/keyboard'],
['by', 'Apal', 'Shah'],
['github.com/apal21']
]
})
}
Note: You can send your custom keyboard inside the 3rd argument of onText() method which is optional.
- Write different message text inside the first argument of onText(). Which is something like this:
bot.onText(/\/start/, (msg) => {
bot.sendMessage(msg.chat.id, 'You sent the /start command', KEYBOARD)
})
bot.onText(/\/help/, (msg) => {
bot.sendMessage(msg.chat.id, 'You sent the /help command', KEYBOARD)
})
- And another one is to define using Regular Expressions
- Here the "match" is an array and its first element is the message you've sent to your bot.
- You can fetch that message by just typing "match[0]"
- Detailed Documentation: https://github.com/yagop/node-telegram-bot-api/
bot.onText(/(.+)/, (msg, match) => {
if (match[0] == "/command") {
bot.sendMessage(msg.chat.id, 'Received /command')
} else {
bot.sendMessage(msg.chat.id, match[0])
}
})