Last active
January 8, 2021 15:47
-
-
Save aahnik/135d4e039638fa8d472146098fa08c10 to your computer and use it in GitHub Desktop.
Telegram Chat info. Telethon. Python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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