forked from Jipok/TelegramRetranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretranslator.py
159 lines (138 loc) · 4.87 KB
/
retranslator.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
#!/usr/bin/env python3
from telethon import TelegramClient, events, utils, sync
from pathlib import Path
import asyncio
#import socks #pip install pysocks
import traceback, sys
API_ID = YOUR_API_ID
API_HASH = 'YOUR_API_HASH'
BOT_ID = 'JipokTestBot'
###################################################################################
if len(sys.argv) < 2:
sys.argv.append("default")
id_list_file = Path(sys.argv[1] + ".id_list")
id_list_file.touch() # Create if not exists
id_list = []
with id_list_file.open('r') as file:
for line in file:
tmp = line.strip()
if len(tmp):
id_list.append(int(tmp))
if len(id_list):
TARGET = id_list[0]
else:
TARGET = None
client = TelegramClient(sys.argv[1], API_ID, API_HASH,
#proxy=(socks.SOCKS5, "localhost", int(4567)),
)
sent_messages = {}
async def send(text, index = 0):
# Edit previous messages if available
if index in sent_messages:
try:
sent_messages[index] = await client.edit_message(sent_messages[index], text)
except:
pass
# Or send new
else:
sent_messages[index] = await client.send_message(BOT_ID, text)
async def list_dialogs():
dialogs = await client.get_dialogs()
tmp = ""
i = 1
for dialog in reversed(dialogs):
eid = dialog.entity.id
name = utils.get_display_name(dialog.entity)
if TARGET and eid == TARGET:
tmp += "====== **TARGET** ======\n"
tmp += ">> /%s: **%s** \n" % (eid, name)
tmp += "========================\n"
elif eid in id_list:
tmp += ">> /%s: **%s** \n" % (eid, name)
else:
tmp += "/%s: %s \n" % (eid, name)
# Split into multiple messages if there is a lot of text
if len(tmp) > 3000:
await send(tmp, i)
i += 1
tmp = ""
if tmp != "":
await send(tmp, i)
# Send help message
if TARGET:
await send("Click on chat id above to toggle retranslation")
else:
await send("Select targert from a list")
# Handle messages sent to the BOT_ID as commands
@client.on(events.NewMessage(outgoing=True))
async def command_handler(event):
try:
global id_list
global TARGET
msg = event.message
if msg.to_id is None:
return
if not hasattr(msg.to_id, 'user_id'):
return
if msg.to_id.user_id != BOT_ID:
return
# Toggle user/chat/channel for retranslation
if (msg.message[0] == "/") and (len(msg.message) > 2):
tmp = int(msg.message[1:])
info = msg.message + " " + utils.get_display_name(await client.get_entity(tmp))
if not TARGET:
id_list.append(tmp)
TARGET = tmp
print("\x1b[32m SELF \x1b[0m: Select target %s" % tmp)
elif tmp == TARGET:
id_list.clear()
print("\x1b[31m SELF \x1b[0m: Remove target %s" % tmp)
TARGET = None
elif tmp in id_list:
id_list.remove(tmp)
print("\x1b[31m SELF \x1b[0m: Del %s" % tmp)
else:
id_list.append(tmp)
print("\x1b[32m SELF \x1b[0m: Add %s" % tmp)
# Update id_list file
with id_list_file.open('w') as file:
for item in id_list:
file.write(str(item) + "\n")
# Update list
await list_dialogs()
await msg.delete()
except Exception as e:
print('Error:\n', traceback.format_exc())
await client.delete_dialog(BOT_ID)
sent_messages.clear()
await client.send_message(BOT_ID, "https://github.com/Jipok/TelegramRetranslator by @Jipok")
await list_dialogs()
@client.on(events.NewMessage(incoming=True))
async def message_handler(event: events.NewMessage.Event):
try:
msg = event.message
if hasattr(msg.to_id, 'channel_id'):
sender_id = msg.to_id.channel_id
elif hasattr(msg.to_id, 'chat_id'):
sender_id = msg.to_id.chat_id
elif hasattr(msg, 'from_id'):
sender_id = msg.from_id
# Forward message if need
if sender_id in id_list:
sender = await client.get_entity(sender_id)
await client.forward_messages(TARGET, [msg])
except Exception as e:
print('Error:\n', traceback.format_exc())
def main():
client.get_dialogs(limit=900) # Fix
client.send_message(BOT_ID, "/start")
# Clear chat with bot
client.delete_dialog(BOT_ID)
# Send start messages
client.send_message(BOT_ID, "https://github.com/Jipok/TelegramRetranslator by @Jipok")
asyncio.ensure_future(list_dialogs())
with client:
main()
print("Successfully started")
print("Open your telegram client")
client.run_until_disconnected()