Skip to content

Commit 96c9260

Browse files
author
Kostya K
committed
Add: --chat option now accepts invitation link
1 parent 44b7156 commit 96c9260

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ Binaries for Linux, Windows and MacOS are available in [Releases](https://github
2626
## Usage
2727

2828
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.
29-
>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.
29+
>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.
30+
31+
>Note2: For private channels you can also pass an invitation link as chat name. E.g. `--chat="https://t.me/joinchat/XXXXXYYYYZZZZZ"`.
32+
__IMPORTANT__: It only works when you (the logged-in user) has already joined the private chat that the invitation link corresponds to.
3033

3134
```
3235
telegram-messages-dump -c <chat_name> -p <phone_num> [-l <count>] [-o <file>] [-cl]

telegram_messages_dump/chat_dump_settings.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
""" This Module contains classes related to CLI interactions"""
44

55
import argparse
6-
# import dateutil.parser
6+
from telegram_messages_dump.utils import JOIN_CHAT_PREFIX_URL
77

88

99
class ChatDumpSettings:
@@ -72,9 +72,13 @@ def __init__(self, usage):
7272
parser.error('Exporter name is invalid.')
7373

7474
# Default output file if not specified by user
75-
out_file = 'telegram_{}.log'.format(args.chat)
76-
if args.out:
75+
OUTPUT_FILE_TEMPLATE = 'telegram_{}.log'
76+
if args.out != '':
7777
out_file = args.out
78+
elif args.chat.startswith(JOIN_CHAT_PREFIX_URL):
79+
out_file = OUTPUT_FILE_TEMPLATE.format(args.chat.rsplit('/', 1)[-1])
80+
else:
81+
out_file = OUTPUT_FILE_TEMPLATE.format(args.chat)
7882

7983
self.chat_name = args.chat
8084
self.phone_num = args.phone

telegram_messages_dump/telegram_dumper.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from telethon.errors.rpc_error_list import UsernameInvalidError
2121
from telethon.tl.functions.contacts import ResolveUsernameRequest
2222
from telegram_messages_dump.utils import sprint
23+
from telegram_messages_dump.utils import JOIN_CHAT_PREFIX_URL
2324
from telegram_messages_dump.exceptions import DumpingError
2425
from telegram_messages_dump.exceptions import MetadataError
2526
from telegram_messages_dump.exporter_context import ExporterContext
@@ -79,7 +80,7 @@ def run(self):
7980
except ValueError as ex:
8081
ret_code = 1
8182
self.logger.error('%s', ex,
82-
exc_info=self.logger.level > logging.INFO)
83+
exc_info=self.logger.level > logging.INFO)
8384
return
8485
# Fetch history in chunks and save it into a resulting file
8586
self._do_dump(chatObj)
@@ -144,6 +145,23 @@ def _getChannel(self):
144145
"""
145146
name = self.settings.chat_name
146147

148+
# For private channуls try to resolve channel peer object from its invitation link
149+
# Note: it will only work if the login user has already joined the private channel.
150+
# Otherwise, get_entity will throw ValueError
151+
if name.startswith(JOIN_CHAT_PREFIX_URL):
152+
self.logger.debug('Trying to resolve as invite url.')
153+
try:
154+
peer = self.get_entity(name)
155+
if peer:
156+
sprint('Invitation link "{}" resolved into channel id={}'.format(
157+
name, peer.id))
158+
return peer
159+
except ValueError as ex:
160+
self.logger.debug('Failed to resolve "%s" as an invitation link. %s',
161+
self.settings.chat_name,
162+
ex,
163+
exc_info=self.logger.level > logging.INFO)
164+
147165
if name.startswith('@'):
148166
name = name[1:]
149167
self.logger.debug('Trying ResolveUsernameRequest().')

telegram_messages_dump/utils.py

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ def sprint(string, *args, **kwargs):
1010
string = string.encode('utf-8', errors='ignore') \
1111
.decode('ascii', errors='ignore')
1212
print(string, *args, **kwargs)
13+
14+
15+
JOIN_CHAT_PREFIX_URL = 'https://t.me/joinchat/'

0 commit comments

Comments
 (0)