@ bot.message_handler()中的lamda函数在Telebot python中无法正常工作 [英] lamda funtion in @bot.message_handler() not working properly in telebot python
问题描述
我试图在python脚本中实现以下代码行,以使用Telebot构建电报机器人.
I tried to implement the following line of code in python script for a telegram bot building using 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')
上面的代码给我一个语法错误.
Above code gives me a syntax error.
@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')
此代码解决了语法错误,但仍然不能满足我的要求.当用户发送"/test some text"时,我想识别它并在此之后执行一些操作.
This code solves the syntax error, but still, it doesn't do what I want it to do. When a user sends '/test some text' I want to identify this and do some actions after that.
我对python比较陌生,这是我第一次使用telebot和lambda函数.所以请帮助我
I am relatively new to python and this is my first time using telebot and lambda functions. So please help me in
- 确定为什么第一个代码给我语法错误.
- 如何正确地以('/test')开始. 提前非常感谢您.
- identifying why the 1st code gave me a syntax error.
- How to implement this startswith('/test') properly. Thank you so much in advance.
推荐答案
因为三元运算符具有特定的语法,因此必须遵循:
Because ternary operator has a specific syntax, that has to be followed:
<value if True> if <condition> else <value if False>
您在第一个示例中所做的是:
What you did in the first sample is:
<value if True> if <condition>
您也不必像以前那样做
True if msg.text.startswith('/test') else False
.startswith()
单独返回bool
.
不清楚装饰器是做什么的,但是为什么不只在函数内部执行检查呢?
It's unclear what decorator does, but why don't you just perform the check inside of the function?
@bot.message_handler
def test_start(message):
if msg.startswith('/test'):
msg=bot.send_message(message.chat.id,'This is feature is under developement')
这篇关于@ bot.message_handler()中的lamda函数在Telebot python中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!