-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()
4

1 回答 1

1

添加停止标志:

  • 向主函数添加逻辑:当停止标志为 True 时,主函数应返回
  • 在 stopfunc 中设置停止标志为 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       

...

于 2020-09-06T10:21:35.500 回答