Skip to content

Instantly share code, notes, and snippets.

@casper
Last active February 16, 2021 09:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save casper/444daf52c1fde5a1b839306b551f757f to your computer and use it in GitHub Desktop.
Save casper/444daf52c1fde5a1b839306b551f757f to your computer and use it in GitHub Desktop.
Telethon get Telegram chat ID
# In reference to
# https://stackoverflow.com/a/60982206/823617
# You need a previous Telethon .session file (from some other bot) for this code to work.
import os
import sys
from telethon import TelegramClient
from telethon.utils import get_display_name
session_name = "session_file_name"
api_id = "id"
api_hash = "hash"
dialog_count = 10 # you may change this
client = TelegramClient(session_name, api_id, api_hash)
async def main():
await client.connect()
dialogs = await client.get_dialogs(dialog_count)
for dialog in dialogs:
print(get_display_name(dialog.entity), dialog.entity.id)
client.loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment