yagop / node-telegram-bot-api

Telegram Bot API for NodeJS
MIT License
8.12k stars 1.49k forks source link

bot.on('message', ...) works unexpected #1180

Closed ngugcx closed 3 months ago

ngugcx commented 3 months ago

Bug Report

I have read:

I am using the latest version of the library, 0.65.1.

Expected Behavior

Below is my code, I expect bot.on('message', ...) is triggered whenever there is a message.

import TelegramBot from 'node-telegram-bot-api'

async function main () {
  const token = '__telegram_bot_token__'

  const bot = new TelegramBot(token, { polling: true })

  bot.onText(/\/echo (.+)/, (msg, match) => {
    const chatId = msg.chat.id
    console.log(chatId)
    const resp = match[1]

    bot.sendMessage(chatId, resp)
  })

  bot.on('message', (msg) => {
    const chatId = msg.chat.id.toString()
    console.log(chatId)
    bot.sendMessage(chatId, 'New channel enabled')
  })
}

try {
  main()
} catch (err) {
  console.error(err)
}

Actual Behavior

bot.on('message', ...) is only triggered when bot.onText is triggered.