Telegram Chat ID vs Peer ID: Telethon Guide

Refer to the Telethon official documentation https://docs.telethon.dev/en/stable/concepts/chats-vs-channels.html


ID is something like this

4011678806
2079829990
6702958858

If you use Forkgram on the desktop, you can see these IDs directly on the interface

 
While Peer ID is like this:
460693903
-4016131921
-1002079829990

Numbers without a sign mean this is a user, a bot is a type of user.

If the number has a minus sign in front, it is a private group.

If the number has -100 in front, it is a channel, or a public group. 


When you use the web version of Telegram, you can find such IDs in the browser's address bar


When using the Telethon framework, chat_id is the Peer ID. For example,

@client.on(events.MessageEdited)
@client.on(events.NewMessage())
async def on_greeting(event):    
  real_id, peer_type = resolve_id(event.chat_id) # Convert to the official real id
  print(f"event.chat_id {event.chat_id}")
  print(f"real_id {real_id}")

The command line prints something like this:

event.chat_id -1001730207212
real_id 1730207212
event.chat_id -4016131921
real_id 4016131921
Note, resolve_id is an official Telethon function.
The corresponding reverse conversion function is get_peer_id
You need to import it like this
from telethon.utils import resolve_id, get_peer_id
peer_type may be PeerUser, PeerChat, PeerChannel
You need to import it like this
from telethon.tl.types import PeerUser, PeerChat, PeerChannel

========
update

When using the Telethon framework, event.chat_id and event.message.chat.id are different. Printing them with logs looks something like this:

event.chat_id: -1001742766507
event.message.chat.id: 1742766507
event.chat_id: -4085528054
event.message.chat.id: 4085528054
Comments