2

我正在尝试使用带有 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()

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

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

我可以做些什么来使用 message_handler 并捕获消息中的一些单词?

4

1 回答 1

2

默认情况下,Telegram 机器人启用隐私模式。

在隐私模式下运行的机器人不会收到人们发送给群组的所有消息。相反,它只会收到:

以斜杠“/”开头的消息(参见上面的命令)

回复机器人自己的消息

服务消息(在群组中添加或删除的人等)

来自其会员频道的消息

您可以通过 BotFather 为您的机器人禁用隐私模式。

于 2018-12-08T17:37:23.467 回答