forked from V35HR4J/Automate-Telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
101 lines (85 loc) · 3.43 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
import logging
import json
import subprocess
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:
f=open("config.json",'r')
data = json.load(f)
token=data['TOKEN']
tg_id = data['TGID']
def start(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /start is issued."""
chat_id = update.message.chat.id
if tg_id == str(chat_id):
user = update.effective_user
update.message.reply_markdown_v2(
fr'Hi {user.mention_markdown_v2()}\!',
reply_markup=ForceReply(selective=True),
)
else:
user = update.effective_user
update.message.reply_markdown_v2(
fr'Hi {user.mention_markdown_v2()}\! You are not authorized to use this bot\! ',
reply_markup=ForceReply(selective=True),
)
def help_command(update: Update, context: CallbackContext) -> None:
"""Send a message when the command /help is issued."""
chat_id = update.message.chat.id
if tg_id == str(chat_id):
update.message.reply_text('Hi there!\nType /cmd yourcommand for executing terminal commands and /send filename to get files downloaded.')
else:
user = update.effective_user
update.message.reply_markdown_v2(
fr'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."""
chat_id = update.message.chat.id
if tg_id == str(chat_id):
comand = update.message.text.split(" ",1)[1]
output = subprocess.getoutput(comand)
if len(output) >4096:
for i in range(0, len(output), 4096):
temp=output[ i : i+4096]
update.message.reply_text(f'<code>{temp}</code>', parse_mode='HTML')
else:
update.message.reply_text(f'<code>{output}</code>', parse_mode='HTML')
print(comand)
else:
user = update.effective_user
update.message.reply_markdown_v2(
fr'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.message.chat.id
if tg_id == str(chat_id):
chat_id = update.message.chat.id
bot = context.bot
file = update.message.text.split()[1]
bot.send_document(chat_id=chat_id, document=open(file, 'rb'))
else:
user = update.effective_user
update.message.reply_markdown_v2(
fr'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("cmd", cmd))
dispatcher.add_handler(CommandHandler("send", send))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()