skip to Main Content

I all,
i want use patter for exclude words with pattern, can you help me?

list_words_to_exclude = [word1, word2, word3]
@client.on(events.NewMessage(incoming=True, from_users=lista_canali, pattern=list_words_to_exclude ))
async def gestione_eventi(evento):   

Thanks

2

Answers


  1. You can use a manual filter

    list_words_to_exclude = ["word1", "word2", "word3"]
    
    async def filter(event):
        for word in list_words_to_exclude :
             if word in event.raw_text:
                 return True
         return False
    @client.on(events.NewMessage(incoming=True, from_users=lista_canali, func=filter ))
    async def gestione_eventi(evento):   
    
    Login or Signup to reply.
  2. According to the telethon docs you can simply pass in a filter callback to func parameter of events.NewMessage:

    func (callable, optional)
    

    A callable (async or not) function that should accept the event as
    input parameter, and return a value indicating whether the event
    should be dispatched or not (any truthy value will do, it does not
    need to be a bool).

    So in your case that could be:

    list_words_to_exclude = ["word1", "word2", "word3"]
    
    # custom filter function
    def filter_words(event):
        for word in list_words_to_exclude:
             if word in event.raw_text:
                 return False  # do not dispatch this event 
         return True  # dispatch all other events
    
    
    # pass in the filter function as an argument to the `func` parameter
    @client.on(events.NewMessage(incoming=True, from_users=lista_canali, func=filter_words))
    async def gestione_eventi(evento):
        # other logic here  
        ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search