问题描述
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()
解决方法
添加停止标志:
- 向主功能添加逻辑:当停止标志为True时,主功能应返回
- 在stopfunc中将stop标志设置为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
...