W
W
Wynell_ru2020-04-27 12:57:47
Python
Wynell_ru, 2020-04-27 12:57:47

[Telethon] How to get group/chat name from a message?

Good afternoon.

with TelegramClient('name', api_id, api_hash) as client:
   @client.on(events.NewMessage(pattern=pattern))
   async def handler(event):
      await event.reply("Тут должно быть название беседы/группы/имя пользователя")

How to implement?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
doevent, 2020-06-14
@Wynell_ru

from telethon import TelegramClient, events, utils
import os

session = os.environ.get('TG_SESSION', 'printer')
api_id = '888888888'
api_hash = '8888888888888888888888'

client = TelegramClient(session, api_id, api_hash, retry_delay=30, auto_reconnect=True).start()

@client.on(events.NewMessage()) 
async def handler_all(event):
    chat_id = event.chat_id #ID чата
    
    sender_id = event.sender_id # Получаем ID Юзера
    msg_id = event.id # Получаем ID сообщения
    
    
    sender = await event.get_sender() # получаем имя юзера
    name = utils.get_display_name(sender) # Имя Юзера

    chat_from = event.chat if event.chat else (await event.get_chat()) # получаем имя группы
    chat_title = utils.get_display_name(chat_from)  # получаем имя группы
    
    print(f"ID: {chat_id} {chat_title} >>  (ID: {sender_id})  {name} - (ID: {msg_id}) {event.text}") 

with client:
    client.run_until_disconnected()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question