AutoResponder Telegram Script

Question:

I want to use that script (https://github.com/Gunthersuper/telegram-autoresponder/blob/main/bot.py) but only in a specific period of time and dont reply my messages when is out of time.

Deleted api_id and api_hash

from telethon import TelegramClient, events

import time

print ("Hora actual " + time.strftime("%H:%M:%S"))
print ("Hora actual " + time.strftime("%H"))

horario = ["08", "09", "10", "11", "12", "13"]
estado = time.strftime("%H") in horario

x = True

if estado == x:
    
else:
    
    api_id = 
    api_hash = ' '

    client = TelegramClient('user', api_id, api_hash).start()


    message = "Hola! Esto es un mensaje automatizado:n Ahora mismo nos encontramos fuera de horario, le atenderemos en cuanto volvamos, muchas gracias."

    @client.on(events.NewMessage())
    async def handler(event):
        sender = await event.get_input_sender()
        await client.send_message(sender, message)

    client.run_until_disconnected()

Asked By: arodgon

||

Answers:

You need to validate the condition on every new message inside handler function. Since client decorator runs the code inside handler everytime and doesn’t care about outside things.

from telethon import TelegramClient, events

import time

horario = ["08", "09", "10", "11", "12", "13"]


    
api_id = 
api_hash = ' '

client = TelegramClient('user', api_id, api_hash).start()


message = "Hola! Esto es un mensaje automatizado:n Ahora mismo nos encontramos fuera de horario, le atenderemos en cuanto volvamos, muchas gracias."

@client.on(events.NewMessage())
async def handler(event):
    if time.strftime("%H") in horario:
        sender = await event.get_input_sender()
        await client.send_message(sender, message)

client.run_until_disconnected()
Answered By: msvstl