如何解决@ bot.message_handler中的lamda函数在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')
此代码解决了语法错误,但仍然不能满足我的要求。当用户发送“ / test some text”时,我想识别它并在此之后执行一些操作。
我对python比较陌生,这是我第一次使用telebot和lambda函数。所以请帮助我
- 确定为什么第一个代码给我语法错误。
- 如何正确地以('/ test')开始。 提前非常感谢您。
解决方法
由于三元运算符具有特定的语法,因此必须遵循:
<value if True> if <condition> else <value if False>
您在第一个示例中所做的是:
<value if True> if <condition>
您也不必像以前那样做
True if msg.text.startswith('/test') else False
.startswith()
单独返回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')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。