Telethon:按事件来源的聊天类型过滤事件

Meh*_*ghi 2 python telegram telethon

我想过滤events.newMessage仅当消息来自私人聊天时才触发。但我不想将所有聊天 ID 放在事件的聊天列表参数中

你有什么想法 ?

@client.on(events.NewMessage)
async def my_event_handler(event):
    if 'hello' in event.raw_text:
        await client.delete_messages(await event.get_chat(), event.id)
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,我想删除来自私人聊天的所有消息(每个人)

pai*_*nor 7

NewMessage事件实现ChatGetter类,因此您可以使用以下内容:

event.is_private? 用户

event.is_group? 聊天和频道(大型组标志集)

event.is_channel? 渠道

也为了更完整的答案

any private = is_private
any group = is_group
any channel = is_channel
only small groups = is_group and not is_channel
only mega groups = is_group and is_channel
only broadcast channels = not is_group and is_channel
Run Code Online (Sandbox Code Playgroud)