[英]How to add a flag to my python code for telegram bot?
我想要一个标志来改变我的机器人的答案。 这是我的代码:
import config
import telebot
bot = telebot.TeleBot(config.token)
pills_adding_state = False
@bot.message_handler(commands=['start'])
def handle_start_message(message):
bot.send_message(message.chat.id, "Hi. I will help you remember all the medicine you need to take. Type add to add a medicine to your list")
@bot.message_handler(content_types = ["text"])
def handle_list_of_pills_message(message):
if message.text == "/add":
pills_adding_state = True
bot.send_message(message.chat.id,"Type the medicine name")
elif pills_adding_state ==True:
bot.send_message(message.chat.id,"medicine {} added".format(message.text))
if __name__ == '__main__':
bot.polling(none_stop=True)
我编辑了我的代码片段以使其更清晰一些。 目标是:用户输入“/add”,然后只要标志药丸添加状态现在为真,他们就可以输入药物的名称。 但是代码没有按预期工作。
不幸的是,更改变量“pills_adding_state”不会影响我的机器人的行为。 我做错了什么? 谢谢你。
pills_adding_state
添加状态设置为False
,因此您的命令将是一次性使用。elif pills_adding_state ==True:
是多余的,因为很好,因为pills_adding_state
是True
,if 块将运行, elif pills_adding_state:
做得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.