0

how to do in python to make two interval counters like the setTimeout of JS for example in python? because time.sleep() becomes a problem when having two because it will generate a delay

I need a counter in trigger_message and another one in check_signal_time where both will be a loop triggered after this counter reaches 0

I am not able to make the last message that my bot sends be edited by itself when the time is equal or greater so that it updates the last message with another one communicating that the message is obsolete for example

def trigger_message():
    global last_message_id
    while signal_enabled:
        set_next_signal_time()
        current_time = datetime.now().strftime("%H:%M")
        check_signal_time(current_time, time_signal_valid)
        random_interval = random.choice(time_intervals_seconds)
        total_time = 3 * 60 + random_interval
        message = generate_signal_message(register_link, time_signal_valid, chance_acerto)
        sent_message = bot.send_message(chat_id=CHAT_ID, text=message, parse_mode='markdown')
        last_message_id = sent_message.message_id
        if signal_enabled:
            time.sleep(total_time)

def check_signal_time(current_time, time_signal_valid):
    if current_time >= time_signal_valid:
        end_message = generate_end_message(register_link)
        bot.edit_message_text(text=end_message, chat_id=CHAT_ID, message_id=last_message_id, parse_mode='markdown')


def set_next_signal_time():
    global time_signal_valid
    timezone = pytz.timezone('America/Recife')  # Configura o fuso horário de Recife
    next_signal_time = datetime.now(timezone) + timedelta(minutes=3)  # Calcula o tempo para o próximo sinal
    time_signal_valid = next_signal_time.strftime("%H:%M")
    # signal_send_message.format(time_signal_valid=time_signal_valid)
    print("Hora atual:", datetime.now(timezone))
    print("Próximo sinal válido até:", time_signal_valid)
netryx
  • 11
  • 3
  • It is unclear to me what the problem is. Please try to rephares your question to be more precise including desired outcome and actual outcome. Pease also include any exception tracebacks if relevant. Please also read [this article](https://stackoverflow.com/help/how-to-ask) on asking good question. Finally, please only tag libraries that you are actually using. `python-telgeram-bot` and `py-telegram-bot-api` are two independent python libraries. – CallMeStag Jul 17 '23 at 16:08
  • Changes were made! – netryx Jul 17 '23 at 17:00

0 Answers0