0

I want to have a flag that will change the answer of my bot. Here is my code:

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)

I edited my code snippet a bit to make it a bit clearer. The goal is: user types "/add" and then they can type the name of the medicine as soon as the flag pills_adding_state is now true. But the code doesn't work as intended.

Unfortunately, changing the variable "pills_adding_state" doesn't affect the behavior of my bot. What do I do wrong? Thank you.

IceTeaGreen
  • 133
  • 12

1 Answers1

1
  1. That's a very BAD way to do it,P lease don't globals like that!, that would mean, you can type /add and someone can type the medicine name, or two people can type /add one after another and hell will lose, ITS BAD.
  2. You should be using the global tag to reference a global (more on that here).
  3. You never set pills_adding_state to False, so Your command will be a one time use.
  4. elif pills_adding_state ==True: is redundant, since well pills_adding_state is True, the if block will run, elif pills_adding_state: does the job well.
Anunay
  • 397
  • 2
  • 10