如何使用 Telethon 获取传入电报消息的聊天或群组名称?

How to get the Chat or Group name of Incoming Telegram message using Telethon?

我有这个代码

from telethon.sync import TelegramClient, events

with TelegramClient('name', api_id, api_hash) as client:
   @client.on(events.NewMessage(pattern=pattern))
   async def handler(event):
      await event.reply("Here should be the Chat or Group name")

如何实现?

如果我们只谈论 groups/channels

chat_from = event.chat if event.chat else (await event.get_chat()) # telegram MAY not send the chat enity
chat_title = chat_from.title

Else(如果我们想获取聊天实体的全名,包括Users):

from telethon import utils

chat_from = event.chat if event.chat else (await event.get_chat()) # telegram MAY not send the chat enity
chat_title = utils.get_display_name(chat_from)

get_display_name() 实际上得到了一个你会看到的名字。适用于 User, Channel, Chat 类型 该方法不得有await