-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForwardGram.py
424 lines (380 loc) · 18.6 KB
/
ForwardGram.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel
import yaml
import sys
import logging
import DiscordHandler
import subprocess
from googletrans import Translator
import re
import os
import asyncio
import Util
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logging.getLogger("telethon").setLevel(level=logging.WARNING)
logger = logging.getLogger(__name__)
class Forwardgram:
forward_settings = {}
input_channels_entities = []
temp_dir = os.path.join(os.path.dirname(__file__), "temp")
group_id_messages = {}
util = Util.Util()
async def client_start(self):
await self.client.start()
def __init__(self):
self.config = self.util.get_config()
self.client = TelegramClient(
self.config["session_name"], self.config["api_id"], self.config["api_hash"]
)
loop = asyncio.get_event_loop() # Here
self.discord = DiscordHandler.DiscordHandler(self.config["discord_bot_token"])
loop.run_until_complete(self.client_start())
self.client.add_event_handler(
self.new_message_handler,
events.NewMessage(chats=self.input_channels_entities),
)
loop.run_until_complete(self.retrieve_config())
print("Forwardram started, waiting for messages........")
loop.run_until_complete(self.client.run_until_disconnected())
@staticmethod
def process_message(mess, dest_lang):
out = {"tmessage": "", "confidence": 0, "olanguage": ""}
if mess is not None:
mess_txt = '"' + mess + '"'
else:
mess_txt = "None"
if mess_txt != "None":
translator = Translator()
detection = translator.detect(mess_txt)
translation_confidence = detection.confidence
translation = translator.translate(mess_txt, dest=dest_lang)
original_language = translation.src
translated_text = translation.text
out = {
"tmessage": translated_text,
"confidence": translation_confidence,
"olanguage": original_language,
"omessage": mess_txt,
}
return out
async def retrieve_config(self):
await self.retrieve_config_settings()
async def retrieve_channels(self, id=0, name="", username=""):
entity = None
input_str = ""
if name != "":
input_str = f"name: {name}"
elif username != "":
input_str = f"username: {username}"
elif id != 0:
input_str = f"id: {id}"
async for dia in self.client.iter_dialogs():
if (
str(name).strip() != ""
and dia.name == str(name).strip()
or dia.entity.id == id
):
entity = InputChannel(dia.entity.id, dia.entity.access_hash)
print(f"[+] Channel {dia.name} found.")
break
if not entity:
try:
entity = await self.client.get_entity(id)
if entity:
print(f"[+] Channel {input_str} added.")
except:
pass
if not entity:
try:
entity = await self.client.get_entity(username)
if entity:
print(f"[+] Channel {input_str} added.")
except:
pass
return entity
@staticmethod
def align_telegram_channel_id(id, with_minus=True):
if with_minus:
if id > 0:
return int("-100" + str(id))
else:
return id
else:
return int(str(id).replace("-100", ""))
async def retrieve_config_settings(self):
for key, value in self.config.items():
if re.match(r"forward_[0-9]+", key):
current_forward = {
"telegram": [],
"discord": [],
"language_processing": "",
"ocr": False
}
input_channel_handler = ""
current_chl = None
error = False
for forwarditem in self.config[key]:
if "input" in forwarditem.keys():
if len(forwarditem["input"]) != 1:
print(
"[-] Error: Only one input channel is allowed per forward."
)
error = True
continue
elif (
len(forwarditem["input"][0].keys()) != 1
or "telegram_channel" not in forwarditem["input"][0].keys()
):
print("[-] Error: Input setting wrong.")
error = True
continue
for attr in forwarditem["input"][0]["telegram_channel"]:
if "id" in attr.keys():
current_chl = await self.retrieve_channels(
id=Forwardgram.align_telegram_channel_id(
attr["id"], False
)
)
input_channel_handler = "id: {}".format(attr["id"])
elif "name" in attr.keys():
current_chl = await self.retrieve_channels(
name=attr["name"]
)
input_channel_handler = "name: {}".format(attr["name"])
elif "username" in attr.keys():
current_chl = await self.retrieve_channels(
username=attr["username"]
)
input_channel_handler = "username: {}".format(
attr["username"]
)
elif "language" in attr.keys():
current_forward["language_processing"] = attr[
"language"
]
elif "ocr" in attr.keys():
try:
current_forward["ocr"] = bool(attr[
"ocr"
])
except:
print("[-] Error: OCR setting wrong.")
continue
if current_chl:
self.input_channels_entities.append(current_chl)
else:
continue
if "output" in forwarditem.keys():
for cckey in forwarditem["output"]:
if len(cckey.keys()) != 1:
error = True
print("[-] Error: Output setting wrong.")
continue
key_name = list(cckey.keys())[0]
forward = {"entity": None, "language": ""}
if "telegram_channel" in key_name:
for attr in cckey[key_name]:
if "id" in attr.keys():
forward[
"entity"
] = await self.retrieve_channels(
id=Forwardgram.align_telegram_channel_id(
attr["id"], True
)
)
elif "name" in attr.keys():
forward[
"entity"
] = await self.retrieve_channels(
name=attr["name"]
)
elif "username" in attr.keys():
forward[
"entity"
] = await self.retrieve_channels(
username=attr["username"]
)
elif "language" in attr.keys():
forward["language"] = attr["language"]
current_forward["telegram"].append(forward)
elif "discord_channel" in key_name:
for attr in cckey[key_name]:
if "id" in attr.keys():
forward["entity"] = attr["id"]
elif "language" in attr.keys():
forward["language"] = attr["language"]
current_forward["discord"].append(forward)
if (
not error
and current_chl
and hasattr(current_chl, "channel_id")
and current_chl.channel_id not in self.forward_settings.keys()
):
self.forward_settings[current_chl.channel_id] = current_forward
elif not current_chl:
print(f"[-] Error, {input_channel_handler}, Channel not found.")
elif current_chl.channel_id in self.forward_settings.keys():
print(
f"[-] Error: Channel {current_chl.channel_id} is already configured in another forward, check you config.yml."
)
# implement a static function for extracting a caption from a message via regex
def extract_caption(self, message):
caption = ""
if message.entities:
for entity in message.entities:
if entity.__class__.__name__ == "MessageEntityTextUrl":
caption = message.message[
entity.offset : entity.offset + entity.length
]
break
return caption
async def new_message_handler(self, event):
for channel in self.forward_settings[event.message.chat.id]["telegram"]:
if "language" in channel.keys() and channel["language"] != "":
message = self.util.process_message(
event.message.text, channel["language"]
)["tmessage"]
message = message.replace("] (", "](").replace(") ,", "),")
else:
message = event.message.text
await self.client.forward_messages(channel["entity"], message)
for channel in self.forward_settings[event.message.chat.id]["discord"]:
detected_language = ""
if "language" in channel.keys() and channel["language"] != "":
match, message = Util.Util.extract_message_url(event.message.text)
translated_message = self.util.process_message(
message, channel["language"]
)
message = Util.Util.recover_url(translated_message["tmessage"], match)
message = Util.Util.clean_message(message)
detected_language = translated_message["olanguage"]
else:
message = event.message.text
if detected_language != "":
detected_language = f"({detected_language.upper()})"
message_pieces = Util.Util.split_message(message)
"""
Handling Videos
"""
photo_sent = False
photo_list = []
photo_list_path = []
if (
hasattr(event.message, 'video') and
hasattr(event.message.video, 'mime_type') and
event.message.video.mime_type == 'video/mp4'
):
filename = str(event.message.video.id) + event.message.file.ext
await event.message.download_media(os.path.join(self.temp_dir, filename))
check_file = os.stat(os.path.join(self.temp_dir, filename)).st_size
if (check_file != 0):
photo_list_path.append(os.path.join(self.temp_dir, filename))
with open(
os.path.join(self.temp_dir, filename), "rb"
) as filehandle:
video = self.discord.generate_discord_file(filehandle)
photo_list.append(video)
if len(message_pieces["messages"]) == 0 and len(photo_list) > 0:
self.discord.send_async_message(
channel["entity"], "", files=photo_list
)
photo_sent = True
"""
Handling photos, creting a list of photos and a list of photo paths
"""
ocr_messages = None
if event.message.photo:
filename = str(event.message.photo.id) + event.message.file.ext
await event.message.download_media(
os.path.join(self.temp_dir, filename)
)
photo_list_path.append(os.path.join(self.temp_dir, filename))
if self.forward_settings[event.message.chat.id]["ocr"]:
dest_lang = "eng"
if self.forward_settings[event.message.chat.id]["language_processing"] != "":
dest_lang = dest_lang + "+" + self.forward_settings[event.message.chat.id]["language_processing"]
picture_string = Util.Util.process_ocr_image(os.path.join(self.temp_dir, filename), dest_lang).strip()
translated_ocr = self.util.process_message(picture_string, channel["language"])
ocr_strings = translated_ocr["tmessage"]
ocr_messages = Util.Util.split_message(ocr_strings)
with open(
os.path.join(self.temp_dir, filename), "rb"
) as filehandle:
picture = self.discord.generate_discord_file(filehandle)
photo_list.append(picture)
if len(message_pieces["messages"])==0 and not photo_sent:
self.discord.send_async_message(
channel["entity"], "", files=photo_list
)
photo_sent = True
"""
Ending handling photos
"""
first_message = True
for msg_piece in message_pieces["messages"]:
mmsg = msg_piece["msg"].strip()
index = msg_piece["piece"]
count_piece = ""
if len(message_pieces["messages"]) > 1:
count_piece = str(index) + "/" + str(message_pieces["len"])
if not first_message and mmsg.strip() != "":
mmsg = f"{count_piece} ...." + mmsg
elif count_piece != "":
mmsg = f"{count_piece} " + mmsg
first_message = False
if mmsg.strip() == '""':
mmsg = ""
message = ""
if mmsg.strip() != "":
if (
event.message.chat.username
and event.message.chat.username != ""
):
message = f"_[{event.message.chat.title} {detected_language}](https://t.me/{event.message.chat.username}/{event.message.id}), posted:_\n>>> {mmsg}"
else:
message = f"_[{event.message.chat.title} {detected_language}](https://t.me/c/{event.message.chat.id}/{event.message.id}), posted:_\n>>> {mmsg}"
if len(photo_list) > 0 and not photo_sent:
self.discord.send_async_message(
channel["entity"], message, files=photo_list
)
photo_sent = True
else:
if mmsg.strip() != "":
## avoid ratelimting
await asyncio.sleep(0.35)
self.discord.send_async_message(channel["entity"], message)
await asyncio.sleep(0.5)
if ocr_messages:
first_message = True
for msg_piece in ocr_messages["messages"]:
mmsg = msg_piece["msg"].strip()
index = msg_piece["piece"]
count_piece = ""
if len(message_pieces["messages"]) > 1:
count_piece = str(index) + "/" + str(message_pieces["len"])
if not first_message and mmsg.strip() != "":
mmsg = f"{count_piece} ...." + mmsg
elif count_piece != "":
mmsg = f"{count_piece} " + mmsg
first_message = False
if mmsg.strip() == '""':
mmsg = ""
if mmsg.strip() != "":
if (
event.message.chat.username
and event.message.chat.username != ""
):
message = f"_[{event.message.chat.title} {detected_language}](https://t.me/{event.message.chat.username}/{event.message.id})_, OCR TEXT:\n```{mmsg}```"
else:
message = f"_[{event.message.chat.title} {detected_language}](https://t.me/c/{event.message.chat.id}/{event.message.id})_, OCR TEXT:\n```{mmsg}```"
await asyncio.sleep(0.35)
self.discord.send_async_message(channel["entity"], message)
for photo in photo_list_path:
os.remove(photo)
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} {{CONFIG_PATH}}")
sys.exit(1)
Forwardgram()