Telethon python:我如何正确使用新消息的模式?
Telethon python: how can i use pattern correctly for new message?
我都,
我想对 exclude 单词使用模式,你能帮我吗?
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):
根据the telethon docs,您可以简单地将过滤器回调传递给events.NewMessage
的func
参数:
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).
所以在你的情况下可能是:
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
...
我都, 我想对 exclude 单词使用模式,你能帮我吗?
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):
根据the telethon docs,您可以简单地将过滤器回调传递给events.NewMessage
的func
参数:
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).
所以在你的情况下可能是:
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
...