-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmain.py
More file actions
141 lines (120 loc) · 4.86 KB
/
main.py
File metadata and controls
141 lines (120 loc) · 4.86 KB
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
import logging
import json
import subprocess
import threading
from telegram import Update, ForceReply
from telegram.ext import (
Updater,
CommandHandler,
MessageHandler,
Filters,
CallbackContext,
)
# Enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
# Read Token:
with open("config.json", "r") as f:
data = json.load(f)
token = data["TOKEN"]
tg_id = data["TGID"]
def message_display(output, update):
try:
if len(output) > 4096:
pass
for i in range(0, len(output), 4096):
temp = output[i: i + 4096]
update.effective_message.reply_text(
f"<code>{temp}</code>", parse_mode="HTML")
else:
update.effective_message.reply_text(
f"<code>{output}</code>", parse_mode="HTML")
except:
update.effective_message.reply_text(
f"<code>Some error has occured with your command</code>", parse_mode="HTML")
def command_handler(update, command):
""" Run multiple Command without affecting the speed """
output = subprocess.getoutput(command)
message_display(output, update)
def start(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
chat_id = update.effective_message.chat.id
if str(chat_id) in tg_id:
user = update.effective_user
update.effective_message.reply_markdown_v2(
rf"Hi {user.mention_markdown_v2()}\!",
reply_markup=ForceReply(selective=True),
)
else:
user = update.effective_user
update.effective_message.reply_markdown_v2(
rf"Hi {user.mention_markdown_v2()}\! You are not authorized to use this bot\! "
)
def help_command(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /help is issued."""
chat_id = update.effective_message.chat.id
if str(chat_id) in tg_id:
update.effective_message.reply_text(
"<b> <code>/cmd [command]</code> - for executing terminal commands \n <code>/send [Filename]</code> - to get files downloaded \n <code>/download [file_from_server]</code> - to download file from server to your PC.</b>", parse_mode="HTML"
)
else:
user = update.effective_user
update.effective_message.reply_markdown_v2(
rf"Hi {user.mention_markdown_v2()}\! You are not authorized to use this bot\! ",
reply_markup=ForceReply(selective=True),
)
def cmd(update: Update, context: CallbackContext) -> None:
"""Execute Command when the command /cmd is issued."""
if str(update.effective_user.id) in tg_id:
comand = update.effective_message.text.split(" ", 1)[1]
print(comand)
t = threading.Thread(target=command_handler, args=[update, comand])
t.start()
else:
user = update.effective_user
update.effective_message.reply_markdown_v2(
rf"Hi {user.mention_markdown_v2()}\! You are not authorized to use this bot\! ",
reply_markup=ForceReply(selective=True),
)
def send(update, context):
"""Send File when the command /send is issued."""
chat_id = update.effective_message.chat.id
if str(chat_id) in tg_id:
chat_id = update.effective_message.chat.id
bot = context.bot
file = update.effective_message.text.split()[1]
bot.send_document(chat_id=chat_id, document=open(file, "rb"))
else:
user = update.effective_user
update.effective_message.reply_markdown_v2(
rf"Hi {user.mention_markdown_v2()}\! You are not authorized to use this bot\! ",
reply_markup=ForceReply(selective=True),
)
def download_files(update, context):
"""Download file to your computer"""
chat_id = update.effective_message.chat.id
if str(chat_id) in tg_id:
comand = update.effective_message.text.split(" ", 1)[1]
comand = f'curl -O {comand}'
output = subprocess.getoutput(comand)
message_display('File Downloaded, check your computer folder', update)
else:
user = update.effective_user
update.effective_message.reply_markdown_v2(
rf"Hi {user.mention_markdown_v2()}\! You are not authorized to use this bot\! ",
reply_markup=ForceReply(selective=True),
)
def main() -> None:
updater = Updater(token)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", help_command))
dispatcher.add_handler(CommandHandler("download", download_files))
dispatcher.add_handler(CommandHandler("cmd", cmd))
dispatcher.add_handler(CommandHandler("send", send))
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()