apal21 / Telegram-Bot-Custom-Keyboard-Node.js

A Telegram bot with Custom Keyboard and which replies you with your message. An Echo Bot.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting Up

  1. Create a Bot in Telegram app and save the Token.
  2. Install node.js in your machine
  3. Open a Terminal/CMD and Type
    3.1 git clone https://github.com/apal21/Telegram-Bot-Custom-Keyboard-Node.js
    3.2 cd Telegram-Bot-Custom-Keyboard-Node.js
    3.3 Enter your token in the first line of index.js file.
    3.4 npm install
    3.5 node index.js

How to make your custom keyboard?

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.


There are 2 ways to differentiate messages.

  1. 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)
})
  1. And another one is to define using Regular Expressions
bot.onText(/(.+)/, (msg, match) => {

    if (match[0] == "/command") {
        bot.sendMessage(msg.chat.id, 'Received /command')
    } else {
        bot.sendMessage(msg.chat.id, match[0])
    }
    
})

About

A Telegram bot with Custom Keyboard and which replies you with your message. An Echo Bot.

License:MIT License


Languages

Language:JavaScript 100.0%