带有 lambda 的电报机器人 message_handler

我正在尝试使用带有 lambda 的 @bot.message_handler 来捕获与我的机器人一起发送的消息中的一些单词。我看到很多例子,每个人都使用类似的代码:


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()

它运行,但是如果我在组中发送“hi”(里面有 BOT),BOT 不会说“我也爱你!” 我不知道为什么。但是如果我说 /start,BOT 会说“欢迎!!”


正如我在https://github.com/eternnoir/pyTelegramBotAPI#a-simple-echo-bot 中看到的那样,我尝试使用 @bot.message_handler(func=lambda message: True)但它再次不起作用。


如何使用 message_handler 并捕获消息中的一些单词?


吃鸡游戏
浏览 187回答 1
1回答

慕码人8056858

默认情况下,为 Telegram 机器人启用隐私模式。以隐私模式运行的机器人不会收到人们发送给群组的所有消息。相反,它只会收到:以斜杠“/”开头的消息(请参阅上面的命令)回复机器人自己的消息服务消息(从群组中添加或删除的人员等)来自其成员频道的消息您可以通过 BotFather 为您的机器人禁用隐私模式。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python