@bot.message_handler() 中的 lambda 函数在远程机器人 python 中无法正常工作
lamda funtion in @bot.message_handler() not working properly in telebot python
我尝试在 python 脚本中为使用 telebot 构建电报机器人实现以下代码行。
@bot.message_handler(func=lambda msg:True if msg.text.startswith('/test'))
def test_start(message):
msg=bot.send_message(message.chat.id,'This is feature is under developement')
上面的代码给我一个语法错误。
@bot.message_handler(func=lambda msg:True if msg.text.startswith('/test') else False)
def test_start(message):
msg=bot.send_message(message.chat.id,'This is feature is under developement')
此代码解决了语法错误,但它仍然没有按照我的要求执行。当用户发送“/测试一些文本”时,我想识别它并在之后执行一些操作。
我对 python 比较陌生,这是我第一次使用远程机器人和 lambda 函数。所以请帮助我
- 找出第一个代码给我语法错误的原因。
- 如何正确实现这个startswith('/test')。
非常感谢您。
因为三元运算符有特定的语法,必须遵循:
<value if True> if <condition> else <value if False>
您在第一个示例中所做的是:
<value if True> if <condition>
此外,您不必像以前那样做
True if msg.text.startswith('/test') else False
.startswith()
returns bool
自己。
不清楚装饰器的作用,但为什么不在函数内部执行检查呢?
@bot.message_handler
def test_start(message):
if msg.startswith('/test'):
msg=bot.send_message(message.chat.id,'This is feature is under developement')
我尝试在 python 脚本中为使用 telebot 构建电报机器人实现以下代码行。
@bot.message_handler(func=lambda msg:True if msg.text.startswith('/test'))
def test_start(message):
msg=bot.send_message(message.chat.id,'This is feature is under developement')
上面的代码给我一个语法错误。
@bot.message_handler(func=lambda msg:True if msg.text.startswith('/test') else False)
def test_start(message):
msg=bot.send_message(message.chat.id,'This is feature is under developement')
此代码解决了语法错误,但它仍然没有按照我的要求执行。当用户发送“/测试一些文本”时,我想识别它并在之后执行一些操作。
我对 python 比较陌生,这是我第一次使用远程机器人和 lambda 函数。所以请帮助我
- 找出第一个代码给我语法错误的原因。
- 如何正确实现这个startswith('/test')。 非常感谢您。
因为三元运算符有特定的语法,必须遵循:
<value if True> if <condition> else <value if False>
您在第一个示例中所做的是:
<value if True> if <condition>
此外,您不必像以前那样做
True if msg.text.startswith('/test') else False
.startswith()
returns bool
自己。
不清楚装饰器的作用,但为什么不在函数内部执行检查呢?
@bot.message_handler
def test_start(message):
if msg.startswith('/test'):
msg=bot.send_message(message.chat.id,'This is feature is under developement')