-
Notifications
You must be signed in to change notification settings - Fork 1
/
telegram_message_forwarder.py
219 lines (160 loc) · 6.83 KB
/
telegram_message_forwarder.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import consts
import logger_config
from functools import partial
from telethon import TelegramClient, events
from telethon import utils as telethon_utils
logger = logger_config.get_logger()
def create_client(phone_number, api_id, api_hash):
session_file_path = consts.SESSION_FILE_PATH.format(phone_number)
logger.debug("TelegramClient path: {}".format(session_file_path))
client = TelegramClient(
session_file_path,
api_id,
api_hash,
)
return client
async def is_authorized(client:TelegramClient, phone_number):
is_authorized = await client.is_user_authorized()
if not is_authorized:
logger.warning(f"Unauthorized account: {phone_number}")
while True:
logger.info("Sending a message to your Telegram account...")
await client.send_code_request(phone_number)
auth_code = input("Insert the received code: ")
try:
await client.sign_in(phone_number, auth_code)
except Exception as e:
logger.error(f"The account cannot be authorized, error: {e}")
else:
is_authorized = await client.is_user_authorized()
if is_authorized:
logger.info(f"Account authorized: {phone_number}")
break
return is_authorized
async def acquire_targets(client: TelegramClient):
dialogs = await client.get_dialogs()
dialogs_data = {
index: {
'id': dialogs[index].entity.id,
'title': dialogs[index].name,
} for index in range(len(dialogs))
}
for index in dialogs_data:
logger.info('index: {} - id: {:>14} title: {}'.format(
str(index).ljust(len(str(max(dialogs_data)))),
dialogs_data[index]['id'],
dialogs_data[index]['title']
))
destination_entity = None
while True:
index = input("Insert the index of the destination chat: ").replace(" ", "")
if index.isdigit():
index = int(index)
destination_entity = dialogs_data.get(index, None)
if destination_entity:
break
logger.warning("You entered an invalid index, please try again.")
target_entities = {}
while True:
index = input("Insert the index of the target chat: ").replace(" ", "")
if index.isdigit():
index = int(index)
target_entity = dialogs_data.get(index, None)
if target_entity:
target_entities[len(target_entities)] = target_entity
choice = input("Do you want to add another target(Y/n)? ").replace(" ", "").lower()
if choice == 'n':
break
continue
logger.warning("You entered an invalid index, please try again.")
return destination_entity, target_entities
async def handler(update:events.NewMessage, client:TelegramClient, destination_id):
message_id = telethon_utils.get_message_id(update.message)
source = update.message.peer_id
logger.info(f"New message with id: {message_id} received from chat '{source}'")
try:
message = await client.send_message(destination_id, update.message)
except Exception as e:
logger.error("The message cannot be sent, error: {}".format(e))
else:
logger.info(f"Sent message with id: {message.id} to the chat '{message.peer_id}'")
async def initialize_handler(client: TelegramClient, destination, targets):
logger.info('The intercepted messages will be sent to chat with id: {:>14} - title: "{}"'.format(
destination["id"],
destination["title"]
))
destination_id = destination["id"]
logger.info("The following entities will be intercepted:")
for index in targets:
logger.info('index: {} - id: {:>14} title: "{}"'.format(
str(index).ljust(len(str(max(targets)))),
targets[index]['id'],
targets[index]['title']
))
target_ids = [
targets[key]["id"] for key in targets
]
func = partial(handler, client=client, destination_id=destination_id)
client.add_event_handler(
func,
events.NewMessage(
chats=target_ids,
incoming=True
))
logger.info("Handler activated, intercepting messages.")
logger.info("Press CTRL + C to terminate the execution")
await client.run_until_disconnected()
def start(user_data):
client = create_client(
user_data["phone_number"],
user_data["api_id"],
user_data["api_hash"]
)
with client:
try:
client.loop.run_until_complete(
initialize_handler(
client,
user_data["destination"],
user_data["targets"]
))
except KeyboardInterrupt:
pass
logger.info("Telegram client stopped and terminating correctly.")
async def initialize(client: TelegramClient, phone_number):
entities = None
await client.connect()
authorized = await is_authorized(client, phone_number)
if authorized:
destination, targets = await acquire_targets(client)
entities = destination, targets
await client.disconnect()
return entities
def initialize_user_data():
user_data = None
logger.info("To retrieve your Telegram `api_id` and `api_hash` visit the https://my.telegram.org and log in with your Telegram account.")
phone_number = input("Insert Telegram account phone number (with country prefix, e.g., +1 for USA): ").replace(" ", "")
logger.info(f"User entered phone number: {phone_number}")
api_id = input("Insert api_id: ").replace(" ", "")
logger.info("User entered api_id: {}".format(api_id))
api_hash = input("Insert api_hash: ").replace(" ", "")
logger.info("User entered api_hash: {}".format(api_hash))
client = None
try:
client = create_client(phone_number, api_id, api_hash)
except Exception as e:
logger.error('Cannot be created a Telegram Client, error: {}'.format(e))
if client:
try:
destination, targets = client.loop.run_until_complete(initialize(client, phone_number))
except Exception as e:
logger.error('The account cannot be initialized, error: {}'.format(e))
else:
user_data = {
'phone_number': phone_number,
'api_id': api_id,
'api_hash': api_hash,
'destination': destination,
'targets': targets
}
return user_data