Advertisement
disk6969

bin

Aug 17th, 2023 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. import configparser
  2. import json
  3. import asyncio
  4. from datetime import date, datetime
  5.  
  6. from telethon import TelegramClient
  7. from telethon.tl.types import PeerChannel
  8.  
  9. # some functions to parse json date
  10. class DateTimeEncoder(json.JSONEncoder):
  11.     def default(self, o):
  12.         if isinstance(o, datetime):
  13.             return o.isoformat()
  14.         if isinstance(o, bytes):
  15.             return list(o)
  16.         return json.JSONEncoder.default(self, o)
  17.  
  18. # Reading Configs
  19. config = configparser.ConfigParser()
  20. config.read("config.ini")
  21.  
  22. # Setting configuration values
  23. api_id = int(config['Telegram']['api_id'])
  24. api_hash = str(config['Telegram']['api_hash'])
  25.  
  26. phone = config['Telegram']['phone']
  27. username = config['Telegram']['username']
  28.  
  29. # Create the client and connect
  30. client = TelegramClient(username, api_id, api_hash)
  31.  
  32. async def main(phone):
  33.     await client.start()
  34.     me = await client.get_me()
  35.     user_input_channel = input('enter entity(telegram URL or entity id):')
  36.  
  37.     if user_input_channel.replace('-100', '').isdigit():
  38.         entity = PeerChannel(int(user_input_channel))
  39.     else:
  40.         entity = user_input_channel
  41.  
  42.     all_messages = await client.get_messages(entity, limit=None)
  43.  
  44.     with open('messages.json', 'w') as outfile:
  45.         json.dump(all_messages, outfile, indent=4, sort_keys=True, cls=DateTimeEncoder)
  46.  
  47. with client:
  48.     client.loop.run_until_complete(main(phone))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement