forked from salmanfarisvp/TelegramBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTeleBashBot.py
56 lines (45 loc) · 1.29 KB
/
TeleBashBot.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
import sys
import time
import telepot
import subprocess
def teleBash(chat_id, command):
try:
completed = subprocess.run(
command,
check=True,
shell=True,
stdout=subprocess.PIPE,
)
except subprocess.CalledProcessError as err:
print("Error:", err)
if len(err) > 0:
bot.sendMessage(chat_id, err)
else:
returnMessage = 'returncode:', completed.returncode
stdoutMessage = 'Have {} bytes in {} stdout:\n{}'.format(
len(completed.stdout),
'yer Ma',
completed.stdout.decode('utf-8')
)
print("return Message", returnMessage)
print("stdout message", stdoutMessage)
bot.sendMessage(chat_id, completed.stdout.decode('utf-8'))
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('runing command: %s' % command)
bot.sendMessage(chat_id, teleBash(chat_id, command))
# get key
key = open('/opt/teleBot')
bot = telepot.Bot(key.read().strip('\n'))
key.close()
bot.message_loop(handle)
print('I am listening...')
while 1:
try:
time.sleep(10)
except KeyboardInterrupt:
print('\n Program interrupted')
exit()
except:
print('Other error or exception occured!')