Telethon-python:如何正确使用新消息的模式



I all,我想对使用pattern,用pattern排除单词,你能帮我吗?

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

感谢

您可以使用手动过滤器

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

根据电视文档,您可以简单地将过滤器回调传递给events.NewMessage:的func参数

func (callable, optional)

一个可调用(异步或非异步(函数,应接受事件为输入参数,并返回指示事件是否是否应该发送(任何真实的价值都可以,但不能需要是一个布尔(。

因此,在您的情况下,可能是:

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

最新更新