-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward.py
More file actions
61 lines (53 loc) · 2.19 KB
/
forward.py
File metadata and controls
61 lines (53 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from argparse import ArgumentParser, RawTextHelpFormatter
from dataclasses import dataclass
from typing import Union
import re
import sys
from telethon.sync import TelegramClient
from parsers import add_api_arg, add_session_arg
from tg_utils import pack_peer, get_entity
from print import printerr
@dataclass
class Message:
id: int
peer: Union['PeerUser', 'PeerChat', 'PeerChannel']
@staticmethod
def parse(line: str) -> Union['Message', None]:
data = line.split('\t', 3)
if len(data) < 3:
printerr('Invalid data length: `', repr(data), '`')
return None
try:
msg_id, peer_type_s, peer_id = int(data[0]), data[1], int(data[2])
except ValueError as e:
printerr('Error while parsing: ', repr(e))
return None
peer = pack_peer(peer_type_s, peer_id)
if not peer:
return None
return Message(msg_id, peer)
parser = ArgumentParser(description='Forward messages to a Telegram channel, chat or user',
epilog='Program reads tab-separated lists to identify the peer and message to forward\n' \
' - msg_id: int - id of message to forward\n' \
' - peer_type: (user|chat|channel) - type of a peer\n' \
' - peer_id: int - id of a peer\n' \
'Further fields are ignored.',
formatter_class=RawTextHelpFormatter)
parser.add_argument('target',
help='Telegram channel, char or user to forward messages to.' \
'Can be usernames, phone numbers, chat names, etc.')
add_api_arg(parser)
add_session_arg(parser)
args = parser.parse_args()
with TelegramClient(args.session, args.api.id, args.api.hash) as client:
try:
target_entity = get_entity(client, args.target)
except Exception as e:
printerr('Failed to find entity for `', target, '`')
sys.exit(1)
# only parse valid lines
try:
for msg in filter(None, map(Message.parse, sys.stdin)):
client.forward_messages(target_entity, msg.id, msg.peer)
except KeyboardInterrupt:
pass