forked from ryanv404/telegram_translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.py
345 lines (299 loc) · 12.9 KB
/
listener.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
from telethon import TelegramClient, events
from telethon.tl.types import InputChannel
from googletrans import Translator
import logging
import yaml
import html
import re
# Logging as per docs
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING)
logging.getLogger('telethon').setLevel(level=logging.WARNING)
logger = logging.getLogger(__name__)
# Load credentials from config.yml
with open('config.yml', 'rb') as f:
config = yaml.safe_load(f)
# Load channel ID's from channels.yml
with open('channels.yml', 'rb') as f:
channels = yaml.safe_load(f)
# Create the client
client = TelegramClient(config["session_name"], config["api_id"], config["api_hash"])
# Connect the client
client.start()
print('[Telethon] Client is listening...')
# Create a translator instance
translator = Translator()
# Get input and output Channel entities from the channel ID's
rus_channels_entities = []
ukr_channels_entities = []
preferred_channels_entities = []
output_channels_entities = []
for d in client.iter_dialogs():
if d.entity.id in channels["rus_channel_ids"]:
rus_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if d.entity.id in channels["ukr_channel_ids"]:
ukr_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if d.entity.id in channels["preferred_channel_ids"]:
preferred_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if d.entity.id in channels["output_channel_ids"]:
output_channels_entities.append(InputChannel(d.entity.id, d.entity.access_hash))
if not output_channels_entities:
logger.error(f"[Telethon] Could not find any output channels in the user's dialogs")
if not rus_channels_entities and not ukr_channels_entities and not preferred_channels_entities:
logger.error(f"[Telethon] Could not find any input channels in the user's dialogs")
# Log total number of input and output channels
num_input_channels = len(list(set(channels["ukr_channel_ids"] + channels["rus_channel_ids"] + channels["preferred_channel_ids"])))
num_output_channels = len(output_channels_entities)
print(f"[Telethon] Listening to {num_input_channels} {'channel' if num_input_channels == 1 else 'channels'}.")
print(f"[Telethon] Forwarding messages to {num_output_channels} {'channel' if num_output_channels == 1 else 'channels'}.")
# Output channels
war_news_channel = channels['output_channel_ids'][0]
rus_videos_channel = channels['output_channel_ids'][1]
rus_photos_channel = channels['output_channel_ids'][2]
ukr_videos_channel = channels['output_channel_ids'][3]
ukr_photos_channel = channels['output_channel_ids'][4]
# Get the title or username of the input channel
def get_channel_name(chat):
if hasattr(chat, 'title'):
return chat.title
else:
return chat.username
# Listen for new messages from my preferred channels
@client.on(events.NewMessage(chats=preferred_channels_entities))
async def handler(e):
# Translate with Google Translator (source language is auto-detected; output language is English)
content = translator.translate(e.message.message)
if content.text:
translation = content.text
else:
translation = ''
chat = await e.get_chat()
chat_name = get_channel_name(chat)
if chat.username:
link = f't.me/{chat.username}'
else:
link = f't.me/c/{chat.id}'
# Translator mistranslates 'Тривога!' as 'Anxiety' (in this context); change to 'Alert!'
translated_msg = translation.replace('Anxiety!', 'Alert!')
# Escape input text since using html parsing
message_id = e.id
border = '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~'
if translation:
message = (
f'<p><p>{border}\n'
f'<b>{html.escape(chat_name)}</b>\n'
f'{border}\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'{html.escape(translated_msg)}\n\n</p>'
f'<p>{border}\n'
f'{link}/{message_id} ↩</p></p>')
else:
message = (
f'<p>{border}\n'
f'<b>{html.escape(chat_name)}</b>\n'
f'{border}\n\n'
f'{link}/{message_id} ↩</p>')
# Message length limit appears to be around 3980 characters; must trim longer messages or they cannot be sent
if len(message) >= 3980:
formatting_chars_len = len(
f'<p><p>{border}\n' +
f'<b>{html.escape(chat_name)}</b>\n' +
f'{border}\n\n</p>' +
f'<p>[TRANSLATED MESSAGE]\n' +
f'\n\n</p>' +
f'<p>{border}\n' +
f'{link}/{message_id} ↩</p></p>')
# Subtract 3 for ellipsis
desired_msg_len = 3980 - formatting_chars_len - 3
translated_msg = f'{translated_msg[0:desired_msg_len]}...'
message = (
f'<p><p>{border}\n'
f'<b>{html.escape(chat_name)}</b>\n'
f'{border}\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'{html.escape(translated_msg)}\n\n</p>'
f'<p>{border}\n'
f'{link}/{message_id} ↩</p></p>')
if chat.username not in ['photo_posts', 'shadedPineapple', 'my_search_results', 'video_posts', 'ukr_rus_war_news', 'cyberbenb', 'Telegram']:
try:
await client.send_message(war_news_channel, message, link_preview=False, parse_mode='html')
except Exception as exc:
print('[Telethon] Error while sending message!')
print(exc)
# Listen for new Russian video messages
@client.on(events.NewMessage(chats=rus_channels_entities, func=lambda e: hasattr(e.media, 'document')))
async def handler(e):
video = e.message.media.document
if hasattr(video, 'mime_type') and bool(re.search('video', video.mime_type)):
content = translator.translate(e.message.message)
if content.text:
translation = content.text
else:
translation = ''
chat = await e.get_chat()
chat_name = get_channel_name(chat)
if chat.username:
link = f't.me/{chat.username}'
else:
link = f't.me/c/{chat.id}'
message_id = e.id
# Escape input text since using html parsing
untranslated_msg = e.message.message
border = '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~'
if translation:
message = (
f'<p><p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<p><b>{html.escape(chat_name)}</b>\n</p>'
f'{border}\n\n</p>'
f'<p>[ORIGINAL MESSAGE]\n'
f'{html.escape(untranslated_msg)}\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'{html.escape(translation)}</p></p>')
else:
message = (
f'<p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<b>{html.escape(chat_name)}</b>\n'
f'{border}</p>')
# Video message length limit appears to be around 1024 characters; must trim longer messages or they cannot be sent
if len(message) >= 1024:
formatting_chars_len = len(
f'<p><p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<p><b>{html.escape(chat_name)}</b>\n</p>'
f'{border}\n\n</p>'
f'<p>[ORIGINAL MESSAGE]\n'
f'\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'</p></p>')
# Subtract 6 for ellipses;
desired_msg_len = (1024 - formatting_chars_len - 6) // 2
translated_msg = f'{translation[0:desired_msg_len]}...'
untranslated_msg = f'{untranslated_msg[0:desired_msg_len]}...'
message = (
f'<p><p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<p><b>{html.escape(chat_name)}</b>\n</p>'
f'{border}\n\n</p>'
f'<p>[ORIGINAL MESSAGE]\n'
f'{html.escape(untranslated_msg)}\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'{html.escape(translated_msg)}</p></p>')
try:
await client.send_message(rus_videos_channel, message, parse_mode='html', file=e.media, link_preview=False)
except Exception as exc:
print('[Telethon] Error while forwarding video message!')
print(exc)
print(e.message)
# Listen for new Russian photo messages
@client.on(events.NewMessage(chats=rus_channels_entities, func=lambda e: hasattr(e.media, 'photo')))
async def handler(e):
chat = await e.get_chat()
chat_name = get_channel_name(chat)
if chat.username:
link = f't.me/{chat.username}'
else:
link = f't.me/c/{chat.id}'
message_id = e.id
border = '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~'
message = (
f'<p>{link}/{message_id} ↩\n\n'
f'{border}\n</p>'
f'<b>{chat_name}</b>\n'
f'{border}</p>')
try:
await client.send_message(rus_photos_channel, message, parse_mode='html', file=e.media, link_preview=False)
except Exception as exc:
print('[Telethon] Error while forwarding video message!')
print(exc)
print(e.message)
# Listen for new Ukrainian video messages
@client.on(events.NewMessage(chats=ukr_channels_entities, func=lambda e: hasattr(e.media, 'document')))
async def handler(e):
video = e.message.media.document
if hasattr(video, 'mime_type') and bool(re.search('video', video.mime_type)):
content = translator.translate(e.message.message)
if content.text:
translation = content.text
else:
translation = ''
chat = await e.get_chat()
chat_name = get_channel_name(chat)
if chat.username:
link = f't.me/{chat.username}'
else:
link = f't.me/c/{chat.id}'
message_id = e.id
# Escape input text since using html parsing
untranslated_msg = e.message.message
border = '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~'
if translation:
message = (
f'<p><p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<p><b>{html.escape(chat_name)}</b>\n</p>'
f'{border}\n\n</p>'
f'<p>[ORIGINAL MESSAGE]\n'
f'{html.escape(untranslated_msg)}\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'{html.escape(translation)}</p></p>')
else:
message = (
f'<p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<b>{html.escape(chat_name)}</b>\n'
f'{border}</p>')
# Video message length limit appears to be around 1024 characters; must trim longer messages or they cannot be sent
if len(message) >= 1024:
formatting_chars_len = len(
f'<p><p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<p><b>{html.escape(chat_name)}</b>\n</p>'
f'{border}\n\n</p>'
f'<p>[ORIGINAL MESSAGE]\n'
f'\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'</p></p>')
# Subtract 6 for ellipses;
desired_msg_len = (1024 - formatting_chars_len - 6) // 2
translated_msg = f'{translation[0:desired_msg_len]}...'
untranslated_msg = f'{untranslated_msg[0:desired_msg_len]}...'
message = (
f'<p><p>{link}/{message_id} ↩\n\n'
f'{border}\n'
f'<p><b>{html.escape(chat_name)}</b>\n</p>'
f'{border}\n\n</p>'
f'<p>[ORIGINAL MESSAGE]\n'
f'{html.escape(untranslated_msg)}\n\n</p>'
f'<p>[TRANSLATED MESSAGE]\n'
f'{html.escape(translated_msg)}</p></p>')
try:
await client.send_message(ukr_videos_channel, message, parse_mode='html', file=e.media, link_preview=False)
except Exception as exc:
print('[Telethon] Error while forwarding video message!')
print(exc)
print(e.message)
# Listen for new Ukrainian photo messages
@client.on(events.NewMessage(chats=ukr_channels_entities, func=lambda e: hasattr(e.media, 'photo')))
async def handler(e):
chat = await e.get_chat()
chat_name = get_channel_name(chat)
if chat.username:
link = f't.me/{chat.username}'
else:
link = f't.me/c/{chat.id}'
message_id = e.id
border = '~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~'
message = (
f'<p>{link}/{message_id} ↩\n\n'
f'{border}\n</p>'
f'<b>{chat_name}</b>\n'
f'{border}</p>')
try:
await client.send_message(ukr_photos_channel, message, parse_mode='html', file=e.media, link_preview=False)
except Exception as exc:
print('[Telethon] Error while forwarding video message!')
print(exc)
print(e.message)
# Run client until a keyboard interrupt (ctrl+C)
client.run_until_disconnected()