CampingCow
CampingCow

Reputation: 120

how to get channelname from channels using telethon

I want to get the username of a telegram channel. I have given the channel_id. I tried to get it via get_entity like:

channelData = await client.get_entity(channelId)

print(channelData.username)

and it works for some channels. But for e.g. 1628748088 i just get None in username field. So I tried a FullChannelRequest, but as you can see in Docs, there is no username attribute https://tl.telethon.dev/constructors/channel_full.html

I want to get tue channelName from GetHistoryRequest and NewMessage Event. Is there any way to get this attribute for all channels?

Upvotes: 4

Views: 4406

Answers (1)

Hamid
Hamid

Reputation: 1563

You can get the channel's name and id like this:

client = TelegramClient(session_name, api_id, api_hash)

for d in client.iter_dialogs():
   channelId = d.entity.id
   channelName = d.name
   print(f"channel id: {channelId}, channel name: {channelName}")

Upvotes: 3

Related Questions