Есть вопрос с телеграмм ботом на telebot. Нужно, чтобы бот отправлял сообщение (картинку), через определенный интервал времени. Основная проблема заключается в том, что при получении сообщения с новым интервалом бот должен отменить старый цикл и рассылать по новому интервалу, но этого не происходит. У меня не получается сделать это через while и schedule. В моем коде, когда он принимает сообщение с интервалом, то отправляет сообщения, но когда пишу 'Отключить', то не реагирует. В этом и проблема. Помогите, пожалуйста.
import schedule
import time
import random
import telebot
from glob import glob
from telebot import types
key = '...'
bot = telebot.TeleBot(key)
@bot.message_handler(commands=['Начать', 'start', '/start', 'Главная'])
def start(message):
markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
item1 = types.KeyboardButton('Рассылка')
markup.add(item1)
bot.send_message(message.chat.id, 'Используйте меню', reply_markup = markup)
@bot.message_handler(content_types=['text', 'photo'])
def main(message):
lists = glob('images/*')
pic = random.choice(lists)
if message.chat.type == 'private':
if message.text == 'Рассылка':
sub_menu = types.ReplyKeyboardMarkup(resize_keyboard=True)
item8 = types.KeyboardButton('Назад')
item15 = types.KeyboardButton('10 мин')
item9 = types.KeyboardButton('30 мин')
item10 = types.KeyboardButton('1 час')
item11= types.KeyboardButton('10 часов')
item12 = types.KeyboardButton('24 часа')
item16 = types.KeyboardButton('Отключить')
sub_menu.add(item8, item15, item9, item10, item11, item12, item16)
bot.send_message(message.chat.id, 'Здесь можно настроить частоту автоматической',
reply_markup=sub_menu)
elif message.text == '10 мин':
while True:
time.sleep(600)
bot.send_photo(message.chat.id, photo=(open(pic, 'rb')))
pic = random.choice(lists)
if message.text == 'Отключить' or message.text == '30 мин' or message.text == '1 час' or message.text == '10 часов' or message.text == '24 часа':
break
elif message.text == '30 мин':
while True:
time.sleep(1800)
bot.send_photo(message.chat.id, photo=(open(pic, 'rb')))
pic = random.choice(lists)
if message.text == 'Отключить' or message.text == '10 мин' or message.text == '1 час' or message.text == '10 часов' or message.text == '24 часа':
break
elif message.text == 'Отключить':
bot.send_message(message.chat.id, 'Рассылка отключена!')
else:
bot.send_message(message.chat.id, 'Используйте кнопки!')
bot.polling(none_stop=True)