Skip to content

Instantly share code, notes, and snippets.

@casper

casper/chat_id.py Secret

Last active Feb 16, 2021
Embed
What would you like to do?
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