forked from bipinkrish/Save-Restricted-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
282 lines (212 loc) · 8.63 KB
/
main.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
import pyrogram
from pyrogram import Client, filters
from pyrogram.errors import UserAlreadyParticipant, InviteHashExpired, UsernameNotOccupied
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
import time
import os
import threading
import json
with open('config.json', 'r') as f: DATA = json.load(f)
def getenv(var): return os.environ.get(var) or DATA.get(var, None)
bot_token = getenv("TOKEN")
api_hash = getenv("HASH")
api_id = getenv("ID")
bot = Client("mybot", api_id=api_id, api_hash=api_hash, bot_token=bot_token)
ss = getenv("STRING")
if ss is not None:
acc = Client("myacc" ,api_id=api_id, api_hash=api_hash, session_string=ss)
acc.start()
else: acc = None
# download status
def downstatus(statusfile,message):
while True:
if os.path.exists(statusfile):
break
time.sleep(3)
while os.path.exists(statusfile):
with open(statusfile,"r") as downread:
txt = downread.read()
try:
bot.edit_message_text(message.chat.id, message.id, f"__Downloaded__ : **{txt}**")
time.sleep(10)
except:
time.sleep(5)
# upload status
def upstatus(statusfile,message):
while True:
if os.path.exists(statusfile):
break
time.sleep(3)
while os.path.exists(statusfile):
with open(statusfile,"r") as upread:
txt = upread.read()
try:
bot.edit_message_text(message.chat.id, message.id, f"__Uploaded__ : **{txt}**")
time.sleep(10)
except:
time.sleep(5)
# progress writter
def progress(current, total, message, type):
with open(f'{message.id}{type}status.txt',"w") as fileup:
fileup.write(f"{current * 100 / total:.1f}%")
# start command
@bot.on_message(filters.command(["start"]))
def send_start(client: pyrogram.client.Client, message: pyrogram.types.messages_and_media.message.Message):
bot.send_message(message.chat.id, f"__👋 Hi **{message.from_user.mention}**, I am Save Restricted Bot, I can send you restricted content by it's post link__\n\n{USAGE}",
reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("🌐 Source Code", url="https://github.com/bipinkrish/Save-Restricted-Bot")]]), reply_to_message_id=message.id)
@bot.on_message(filters.text)
def save(client: pyrogram.client.Client, message: pyrogram.types.messages_and_media.message.Message):
print(message.text)
# joining chats
if "https://t.me/+" in message.text or "https://t.me/joinchat/" in message.text:
if acc is None:
bot.send_message(message.chat.id,f"**String Session is not Set**", reply_to_message_id=message.id)
return
try:
try: acc.join_chat(message.text)
except Exception as e:
bot.send_message(message.chat.id,f"**Error** : __{e}__", reply_to_message_id=message.id)
return
bot.send_message(message.chat.id,"**Chat Joined**", reply_to_message_id=message.id)
except UserAlreadyParticipant:
bot.send_message(message.chat.id,"**Chat alredy Joined**", reply_to_message_id=message.id)
except InviteHashExpired:
bot.send_message(message.chat.id,"**Invalid Link**", reply_to_message_id=message.id)
# getting message
elif "https://t.me/" in message.text:
datas = message.text.split("/")
temp = datas[-1].replace("?single","").split("-")
fromID = int(temp[0].strip())
try: toID = int(temp[1].strip())
except: toID = fromID
for msgid in range(fromID, toID+1):
# private
if "https://t.me/c/" in message.text:
chatid = int("-100" + datas[4])
if acc is None:
bot.send_message(message.chat.id,f"**String Session is not Set**", reply_to_message_id=message.id)
return
handle_private(message,chatid,msgid)
# try: handle_private(message,chatid,msgid)
# except Exception as e: bot.send_message(message.chat.id,f"**Error** : __{e}__", reply_to_message_id=message.id)
# bot
elif "https://t.me/b/" in message.text:
username = datas[4]
if acc is None:
bot.send_message(message.chat.id,f"**String Session is not Set**", reply_to_message_id=message.id)
return
try: handle_private(message,username,msgid)
except Exception as e: bot.send_message(message.chat.id,f"**Error** : __{e}__", reply_to_message_id=message.id)
# public
else:
username = datas[3]
try: msg = bot.get_messages(username,msgid)
except UsernameNotOccupied:
bot.send_message(message.chat.id,f"**The username is not occupied by anyone**", reply_to_message_id=message.id)
return
try: bot.copy_message(message.chat.id, msg.chat.id, msg.id,reply_to_message_id=message.id)
except:
if acc is None:
bot.send_message(message.chat.id,f"**String Session is not Set**", reply_to_message_id=message.id)
return
try: handle_private(message,username,msgid)
except Exception as e: bot.send_message(message.chat.id,f"**Error** : __{e}__", reply_to_message_id=message.id)
# wait time
time.sleep(3)
# handle private
def handle_private(message: pyrogram.types.messages_and_media.message.Message, chatid: int, msgid: int):
msg: pyrogram.types.messages_and_media.message.Message = acc.get_messages(chatid,msgid)
msg_type = get_message_type(msg)
if "Text" == msg_type:
bot.send_message(message.chat.id, msg.text, entities=msg.entities, reply_to_message_id=message.id)
return
smsg = bot.send_message(message.chat.id, '__Downloading__', reply_to_message_id=message.id)
dosta = threading.Thread(target=lambda:downstatus(f'{message.id}downstatus.txt',smsg),daemon=True)
dosta.start()
file = acc.download_media(msg, progress=progress, progress_args=[message,"down"])
os.remove(f'{message.id}downstatus.txt')
upsta = threading.Thread(target=lambda:upstatus(f'{message.id}upstatus.txt',smsg),daemon=True)
upsta.start()
if "Document" == msg_type:
try:
thumb = acc.download_media(msg.document.thumbs[0].file_id)
except: thumb = None
bot.send_document(message.chat.id, file, thumb=thumb, caption=msg.caption, caption_entities=msg.caption_entities, reply_to_message_id=message.id, progress=progress, progress_args=[message,"up"])
if thumb != None: os.remove(thumb)
elif "Video" == msg_type:
try:
thumb = acc.download_media(msg.video.thumbs[0].file_id)
except: thumb = None
bot.send_video(message.chat.id, file, duration=msg.video.duration, width=msg.video.width, height=msg.video.height, thumb=thumb, caption=msg.caption, caption_entities=msg.caption_entities, reply_to_message_id=message.id, progress=progress, progress_args=[message,"up"])
if thumb != None: os.remove(thumb)
elif "Animation" == msg_type:
bot.send_animation(message.chat.id, file, reply_to_message_id=message.id)
elif "Sticker" == msg_type:
bot.send_sticker(message.chat.id, file, reply_to_message_id=message.id)
elif "Voice" == msg_type:
bot.send_voice(message.chat.id, file, caption=msg.caption, thumb=thumb, caption_entities=msg.caption_entities, reply_to_message_id=message.id, progress=progress, progress_args=[message,"up"])
elif "Audio" == msg_type:
try:
thumb = acc.download_media(msg.audio.thumbs[0].file_id)
except: thumb = None
bot.send_audio(message.chat.id, file, caption=msg.caption, caption_entities=msg.caption_entities, reply_to_message_id=message.id, progress=progress, progress_args=[message,"up"])
if thumb != None: os.remove(thumb)
elif "Photo" == msg_type:
bot.send_photo(message.chat.id, file, caption=msg.caption, caption_entities=msg.caption_entities, reply_to_message_id=message.id)
os.remove(file)
if os.path.exists(f'{message.id}upstatus.txt'): os.remove(f'{message.id}upstatus.txt')
bot.delete_messages(message.chat.id,[smsg.id])
# get the type of message
def get_message_type(msg: pyrogram.types.messages_and_media.message.Message):
try:
msg.document.file_id
return "Document"
except: pass
try:
msg.video.file_id
return "Video"
except: pass
try:
msg.animation.file_id
return "Animation"
except: pass
try:
msg.sticker.file_id
return "Sticker"
except: pass
try:
msg.voice.file_id
return "Voice"
except: pass
try:
msg.audio.file_id
return "Audio"
except: pass
try:
msg.photo.file_id
return "Photo"
except: pass
try:
msg.text
return "Text"
except: pass
USAGE = """**FOR PUBLIC CHATS**
__just send post/s link__
**FOR PRIVATE CHATS**
__first send invite link of the chat (unnecessary if the account of string session already member of the chat)
then send post/s link__
**FOR BOT CHATS**
__send link with '/b/', bot's username and message id, you might want to install some unofficial client to get the id like below__
```
https://t.me/b/botusername/4321
```
**MULTI POSTS**
__send public/private posts link as explained above with formate "from - to" to send multiple messages like below__
```
https://t.me/xxxx/1001-1010
https://t.me/c/xxxx/101 - 120
```
__note that space in between doesn't matter__
"""
# infinty polling
bot.run()