Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import configparser
- import json
- import asyncio
- from datetime import date, datetime
- from telethon import TelegramClient
- from telethon.tl.types import PeerChannel
- # some functions to parse json date
- class DateTimeEncoder(json.JSONEncoder):
- def default(self, o):
- if isinstance(o, datetime):
- return o.isoformat()
- if isinstance(o, bytes):
- return list(o)
- return json.JSONEncoder.default(self, o)
- # Reading Configs
- config = configparser.ConfigParser()
- config.read("config.ini")
- # Setting configuration values
- api_id = int(config['Telegram']['api_id'])
- api_hash = str(config['Telegram']['api_hash'])
- phone = config['Telegram']['phone']
- username = config['Telegram']['username']
- # Create the client and connect
- client = TelegramClient(username, api_id, api_hash)
- async def main(phone):
- await client.start()
- me = await client.get_me()
- user_input_channel = input('enter entity(telegram URL or entity id):')
- if user_input_channel.replace('-100', '').isdigit():
- entity = PeerChannel(int(user_input_channel))
- else:
- entity = user_input_channel
- all_messages = await client.get_messages(entity, limit=None)
- with open('messages.json', 'w') as outfile:
- json.dump(all_messages, outfile, indent=4, sort_keys=True, cls=DateTimeEncoder)
- with client:
- client.loop.run_until_complete(main(phone))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement