Skip to content

Instantly share code, notes, and snippets.

@aahnik
Last active January 8, 2021 15:47
Show Gist options
  • Save aahnik/135d4e039638fa8d472146098fa08c10 to your computer and use it in GitHub Desktop.
Save aahnik/135d4e039638fa8d472146098fa08c10 to your computer and use it in GitHub Desktop.
Telegram Chat info. Telethon. Python.
import asyncio
from settings import API_ID, API_HASH
from telethon import TelegramClient
from utils import _
async def get_chat_id(ref=None):
async with TelegramClient('tg_session', API_ID, API_HASH) as client:
if not ref:
ref = input('Enter link/phone/username/id to get chat info: ')
try:
entity = await client.get_entity(_(ref))
print(type(entity))
print(f'id is {entity.id}')
print(f'\nEntity object \n{entity.stringify()}')
except ValueError:
print('Could not get')
except Exception as err:
print(f'''Something went wrong. Please contact the developer Aahnik Daw.
Chat with Aahnik on Telegram, click on this 👇 link
https://telegram.me/AahnikDaw
Error details: \n {err}''')
if __name__ == "__main__":
asyncio.run(get_chat_id())
import logging
from settings import API_ID, API_HASH
from telethon import TelegramClient, client, events
from utils import _
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
SENT_VIA = f'\n__Sent via__ `{str(__file__)}`'
client = TelegramClient('tg_session', API_ID, API_HASH)
@client.on(events.NewMessage(outgoing=True, pattern=r'\.id'))
async def chat_id_handler(event):
chat_id = event.chat_id
await event.edit(str(chat_id))
@client.on(events.NewMessage(outgoing=True, pattern=r'\.info'))
async def chat_info_handler(event):
chat_info = await event.get_chat()
await event.edit(str(chat_info))
async def main():
await client.send_message('me', f'''Hi!
\n**Telegram Chat Forward** is made by @AahnikDaw.
\nPlease star 🌟 on [GitHub](https://github.com/aahnik/telegram-chat-forward).
\nYou can send `.id` to any chat/group/channel to get its chat id.
\nTo get full info, send `.info`.
\nYou may first try it here 😃
{SENT_VIA}''', link_preview=False)
if __name__ == "__main__":
client.start()
client.loop.run_until_complete(main())
print('''If this script is succesfully running,
you will see a new message in your Saved Messages.
Open your Telegram App and send .id to any chat, to get the chat id.
To get all info send .info
\nPress Ctrl + C to stop the process.''')
client.run_until_disconnected()
from dotenv import load_dotenv
import os
load_dotenv()
API_ID = os.getenv('api_id')
API_HASH = os.getenv('api_hash')
assert API_ID and API_HASH
def _(string:str):
try:
return int(string)
except:
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment