diff --git a/README.md b/README.md index ad4e4d4..4cdbe0c 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,10 @@ Binaries for Linux, Windows and MacOS are available in [Releases](https://github ## Usage Mandatory parameters are e.g. @Python, @CSharp or a title of a dialogue, as seen in the UI, and - a telephone number. A phone number is needed for authentication and will not be stored anywhere. After the first successful authorization it will create telegram_chat_dump.session file containing auth token. The information from this file is being reused in next runs. If this is not a desirable behaviour, use -cl flag to delete session file on exit. ->Note: You can use telegram dialogue multi-word title like so: `--chat="Telegram Geeks"` with double quotes. However, when using multi-word title (rather than @channel_name), you need to join the channel first. Only then you will be able to dump it. This way you can dump __private__ dialogues which doesn't have @channel_name. +>Note1: You can use telegram dialogue multi-word title like so: `--chat="Telegram Geeks"` with double quotes. However, when using multi-word title (rather than @channel_name), you need to join the channel first. Only then you will be able to dump it. This way you can dump __private__ dialogues which doesn't have @channel_name. + +>Note2: For private channels you can also pass an invitation link as chat name. E.g. `--chat="https://t.me/joinchat/XXXXXYYYYZZZZZ"`. +__IMPORTANT__: It only works when you (the logged-in user) has already joined the private chat that the invitation link corresponds to. ``` telegram-messages-dump -c -p [-l ] [-o ] [-cl] diff --git a/telegram_messages_dump/chat_dump_settings.py b/telegram_messages_dump/chat_dump_settings.py index c9dc68f..f1c28ee 100644 --- a/telegram_messages_dump/chat_dump_settings.py +++ b/telegram_messages_dump/chat_dump_settings.py @@ -3,7 +3,7 @@ """ This Module contains classes related to CLI interactions""" import argparse -# import dateutil.parser +from telegram_messages_dump.utils import JOIN_CHAT_PREFIX_URL class ChatDumpSettings: @@ -72,9 +72,13 @@ def __init__(self, usage): parser.error('Exporter name is invalid.') # Default output file if not specified by user - out_file = 'telegram_{}.log'.format(args.chat) - if args.out: + OUTPUT_FILE_TEMPLATE = 'telegram_{}.log' + if args.out != '': out_file = args.out + elif args.chat.startswith(JOIN_CHAT_PREFIX_URL): + out_file = OUTPUT_FILE_TEMPLATE.format(args.chat.rsplit('/', 1)[-1]) + else: + out_file = OUTPUT_FILE_TEMPLATE.format(args.chat) self.chat_name = args.chat self.phone_num = args.phone diff --git a/telegram_messages_dump/telegram_dumper.py b/telegram_messages_dump/telegram_dumper.py index 8cc2aea..1d25119 100644 --- a/telegram_messages_dump/telegram_dumper.py +++ b/telegram_messages_dump/telegram_dumper.py @@ -20,6 +20,7 @@ from telethon.errors.rpc_error_list import UsernameInvalidError from telethon.tl.functions.contacts import ResolveUsernameRequest from telegram_messages_dump.utils import sprint +from telegram_messages_dump.utils import JOIN_CHAT_PREFIX_URL from telegram_messages_dump.exceptions import DumpingError from telegram_messages_dump.exceptions import MetadataError from telegram_messages_dump.exporter_context import ExporterContext @@ -79,7 +80,7 @@ def run(self): except ValueError as ex: ret_code = 1 self.logger.error('%s', ex, - exc_info=self.logger.level > logging.INFO) + exc_info=self.logger.level > logging.INFO) return # Fetch history in chunks and save it into a resulting file self._do_dump(chatObj) @@ -144,6 +145,23 @@ def _getChannel(self): """ name = self.settings.chat_name + # For private channуls try to resolve channel peer object from its invitation link + # Note: it will only work if the login user has already joined the private channel. + # Otherwise, get_entity will throw ValueError + if name.startswith(JOIN_CHAT_PREFIX_URL): + self.logger.debug('Trying to resolve as invite url.') + try: + peer = self.get_entity(name) + if peer: + sprint('Invitation link "{}" resolved into channel id={}'.format( + name, peer.id)) + return peer + except ValueError as ex: + self.logger.debug('Failed to resolve "%s" as an invitation link. %s', + self.settings.chat_name, + ex, + exc_info=self.logger.level > logging.INFO) + if name.startswith('@'): name = name[1:] self.logger.debug('Trying ResolveUsernameRequest().') diff --git a/telegram_messages_dump/utils.py b/telegram_messages_dump/utils.py index 042f8ec..3cd39e8 100644 --- a/telegram_messages_dump/utils.py +++ b/telegram_messages_dump/utils.py @@ -10,3 +10,6 @@ def sprint(string, *args, **kwargs): string = string.encode('utf-8', errors='ignore') \ .decode('ascii', errors='ignore') print(string, *args, **kwargs) + + +JOIN_CHAT_PREFIX_URL = 'https://t.me/joinchat/'