简体   繁体   English

Telethon python:我怎样才能正确地为新消息使用模式?

[英]Telethon python: how can i use pattern correctly for new message?

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谢谢

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):   

According to the telethon docs you can simply pass in a filter callback to func parameter of events.NewMessage :根据Telethon 文档,您可以简单地将过滤器回调传递给events.NewMessagefunc参数:

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).一个可调用的(异步与否)function,它应该接受事件作为输入参数,并返回一个指示是否应该分派事件的值(任何真实值都可以,它不需要是布尔值)。

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  
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM