0

im looking for create specifice event in telethon

@client.on(events.NewMessage(incoming=True))
async def my_event_handler(event):
    if event.is_private:
        print("ok")

in this code i need have "is_private" in NewMessage how can i do this?

and how can create events with specific Condition ? For example i want send_message at 12:00 every day how can i do this ? *Notice i have an event so i cant run for example

client.send_message(chat_id, "message") # i want 2 this at 12:00 for example

1 Answers1

3

Every event is a EventBuilder, which means they all support the func parameter:

@client.on(events.NewMessage(incoming=True, func=lambda e: e.is_private))
async def my_event_handler(event):
    print('ok')

You could also make a separate function for it if you're going to reuse it a lot:

def private_incoming(event):
    return not event.out and event.is_private

@client.on(events.NewMessage(func=private_incoming))
async def my_event_handler(event):
    print('ok')

create events with specific Condition ? For example i want send_message at 12:00 every day

This isn't a event. A event is something that happens as a Telegram update and Telethon dispatches. Your code can do whatever it wants, and you can use as many other libraries as you want when programming. It is not Telethon's job to run things at a certain date (it's a library to talk to Telegram and nothing more).

See Using asyncio to run a function at the start (00 seconds) of every minute or search for a library to do this in https://pypi.org.

Lonami
  • 5,945
  • 2
  • 20
  • 38
  • Thank you lonami ,it was helpful but i didnt undrestand when you have an event in your code it just run event not another section of code ,, i hope you get my mean.. i have an event for getting message and send message every 5 min at the same time is it possible ? – ali moradi Jun 20 '20 at 09:45
  • Yes it is possible, you need to learn `asyncio` (or search for existing questions) on how to do that. Please accept the answer if it has solved your issue. – Lonami Jun 20 '20 at 09:56
  • @Lonami Could you please give an example of using another module e.g.`requests_html` to handle js rendered webpages, to get html content from every url(loop) found on new incoming message? – Learner Jan 31 '21 at 18:13