-4
TOKEN = 'token'
bot = telebot.TeleBot(TOKEN)

def main():
   for i in range(0,100):
      print(i)

@bot.message_handler(commands=['start'])
def start(message):
  main()

@bot.message_handler(commands=['stop'])
def stopfunc(message):
   #how to stop the function main() ?

while True:
   bot.polling()
David Buck
  • 3,752
  • 35
  • 31
  • 35
Акмаль
  • 143
  • 1
  • 6
  • a way to achieve this would be threading, but i can't suggest this because i don't know what exactly you need to achieve. but why not use "if-else" statements in the main function?? is there a reason you can't? – Tochi Bedford Sep 06 '20 at 10:20

1 Answers1

1

Add stop flag :

  • Add logic to main function : when stop flag is True, the main function should return
  • In stopfunc set the stop flag is True
stop = False
def main():
   global stop
   for i in range(0,100):
       if stop:
          break
       print(i)
    
@bot.message_handler(commands=['stop'])
def stopfunc(message):
    global stop
    stop = True       

...

napuzba
  • 6,033
  • 3
  • 21
  • 32