forked from bipinkrish/Save-Restricted-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
176 lines (137 loc) · 6.36 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
import pyrogram
from pyrogram import Client, filters
from pyrogram.errors import UserAlreadyParticipant, InviteHashExpired
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
import time
import os
import threading
bot_token = os.environ.get("TOKEN", None)
api_hash = os.environ.get("HASH", None)
api_id = os.environ.get("ID", None)
bot = Client("mybot", api_id=api_id, api_hash=api_hash, bot_token=bot_token)
ss = os.environ.get("STRING", None)
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__",
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):
# 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("/")
msgid = int(datas[-1].split("?")[0])
# private
if "https://t.me/c/" in message.text:
chatid = int("-100" + datas[-2])
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,chatid,msgid)
except Exception as e: bot.send_message(message.chat.id,f"**Error** : __{e}__", reply_to_message_id=message.id)
# public
else:
username = datas[-2]
msg = bot.get_messages(username,msgid)
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)
# handle private
def handle_private(message,chatid,msgid):
msg = acc.get_messages(chatid,msgid)
if "text" in str(msg):
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" in str(msg):
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" in str(msg):
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" in str(msg):
bot.send_animation(message.chat.id, file, reply_to_message_id=message.id)
elif "Sticker" in str(msg):
bot.send_sticker(message.chat.id, file, reply_to_message_id=message.id)
elif "Voice" in str(msg):
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" in str(msg):
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" in str(msg):
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])
# infinty polling
bot.run()