带有 lambda 的电报机器人 message_handler
Telegram bot message_handler with lambda
我正在尝试使用带有 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 没有说 "I love you too!" 我也不知道为什么。但是如果我说 /start,BOT 会说 "Welcome!!"
我尝试使用 @bot.message_handler(func=lambda message: True) 正如我在 https://github.com/eternnoir/pyTelegramBotAPI#a-simple-echo-bot 中看到的那样,但它再次不起作用。
如何使用 message_handler 并捕获消息中的一些单词?
默认情况下 privacy mode 为 Telegram 机器人启用。
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
您可以通过 BotFather 为您的机器人禁用隐私模式。
我正在尝试使用带有 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 没有说 "I love you too!" 我也不知道为什么。但是如果我说 /start,BOT 会说 "Welcome!!"
我尝试使用 @bot.message_handler(func=lambda message: True) 正如我在 https://github.com/eternnoir/pyTelegramBotAPI#a-simple-echo-bot 中看到的那样,但它再次不起作用。
如何使用 message_handler 并捕获消息中的一些单词?
默认情况下 privacy mode 为 Telegram 机器人启用。
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
您可以通过 BotFather 为您的机器人禁用隐私模式。