pythonlambdabotstelegrammessage-handlers

Telegram bot message_handler with lambda


I´m trying to use the @bot.message_handler with lambda to capture some words in the messages sending in a group with my bot. I see a lot of examples and everybody use a code similar to this:

import telebot

telebot.logger.setLevel(__import__('logging').DEBUG)

bot_token = 'Blablabla'

bot = telebot.TeleBot(bot_token)

# filter on a specific message
@bot.message_handler(func=lambda message: message.text == "hi")
def command_text_hi(m):
    bot.send_message(m.chat.id, "I love you too!")

@bot.message_handler(commands=['start'])
def send_welcome(m):
    bot.send_message(m.chat.id, 'Welcome!')

@bot.message_handler(func=lambda message: True, content_types=['text'])
def command_default(m):
    # this is the standard reply to a normal message
    bot.send_message(m.chat.id, "I don't understand, try with /help")

bot.polling()

It runs, but if I send "hi" in the group (with the BOT inside), the BOT didn't say "I love you too!" and I don't why. But if I say /start, the BOT says "Welcome!!"

I tried with @bot.message_handler(func=lambda message: True) as I saw in https://github.com/eternnoir/pyTelegramBotAPI#a-simple-echo-bot but again it doesn't work.

What can I do to use message_handler and capture some words in the message?


Solution

  • By default privacy mode is enabled for Telegram bots.

    A bot running in privacy mode will not receive all messages that people send to the group. Instead, it will only receive:

    Messages that start with a slash ‘/’ (see Commands above)

    Replies to the bot's own messages

    Service messages (people added or removed from the group, etc.)

    Messages from channels where it's a member

    You could disable privacy mode for your bot through BotFather.