【发布时间】: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