How to disable "schedule" if there is a condition on python

97 views Asked by At

I am writing a bot tc on the telebot library, I am trying to send a newsletter to the user, if he chooses "yes", then a message is sent to him every day, if he chooses "no", then the newsletter is either turned off or simply not turned on

import telebot
import schedule
import time
from telebot import types
if message.text == "Подборка книг":
    podborka = types.InlineKeyboardMarkup()
    yes = types.InlineKeyboardButton("Да", callback_data="да")
    no = types.InlineKeyboardButton("Нет", callback_data="нет")
    podborka.add(yes,no)
    bot.send_message(message.chat.id, "Получать рассылку?", reply_markup=podborka)
    @bot.callback_query_handler(func=lambda call: True)
    def callback_inline(call):
        def good_luck():
            bot.send_message(call.message.chat.id, "Круто!")
        job = schedule.every(1).seconds.do(good_luck)
        while True:
            schedule.run_pending()
            time.sleep(1)

        if call.message:
            if call.data == "да":
                godo()
            else:
                schedule.cancel_job(job)
1

There are 1 answers

1
LhasaDad On

In your example above you could tag the job you create like so:

job = schedule.every(1).seconds.do(good_luck).tag("my_job")

Then later on when you need to clear it out do this:

schedule.clear('my_job')