Skip to content

Commit

Permalink
Add: --chat option now accepts invitation link
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostya K committed May 7, 2018
1 parent 44b7156 commit 96c9260
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ Binaries for Linux, Windows and MacOS are available in [Releases](https://github
## Usage

Mandatory parameters are <chat_name> e.g. @Python, @CSharp or a title of a dialogue, as seen in the UI, and <phone_num> - 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 <chat_name> -p <phone_num> [-l <count>] [-o <file>] [-cl]
Expand Down
10 changes: 7 additions & 3 deletions telegram_messages_dump/chat_dump_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
20 changes: 19 additions & 1 deletion telegram_messages_dump/telegram_dumper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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().')
Expand Down
3 changes: 3 additions & 0 deletions telegram_messages_dump/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/'

0 comments on commit 96c9260

Please sign in to comment.