【问题标题】:Telegram bot message_handler with lambda带有 lambda 的 Telegram bot message_handler
【发布时间】:2019-05-10 02:54:02
【问题描述】:

我正在尝试使用带有 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 会说“欢迎!”

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

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

【问题讨论】:

  • 您好,我刚刚启动了您的代码,没有遇到任何问题。请在import telebot 之后插入此行telebot.logger.setLevel(__import__('logging').DEBUG),然后尝试与您的机器人通信并共享您的程序将打印的日志。
  • 当我说“嗨”时,什么都没有发生。当我尝试“/ hi”时,它可以工作。仅当我使用 / 编写时才有效
  • 如何关闭调试模式?我删除了“telebot.logger.setLevel(__import__('logging').DEBUG)”行,但它继续显示它。

标签: python lambda bots telegram message-handlers


【解决方案1】:

默认情况下,privacy mode 为 Telegram 机器人启用。

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

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

回复机器人自己的消息

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

来自其会员频道的消息

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

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 2021-10-06
    • 1970-01-01
    • 2020-06-10
    • 2018-11-30
    • 2020-08-14
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多