简体   繁体   English

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

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

I have this code我有这个代码

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

How to implement this?如何实施?

if we are talking only about 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 (If we want to get the full name of chat entities, including 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() actually gets a name that you would see. get_display_name() 实际上得到一个你会看到的名字。 Works for types User, Channel, Chat That method shall not have await适用于User, Channel, Chat类型该方法不应await

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

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