forked from salmanfarisvp/TelegramBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegrambot.py
More file actions
81 lines (70 loc) · 2.04 KB
/
telegrambot.py
File metadata and controls
81 lines (70 loc) · 2.04 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
#coder :- Salman Faris
import sys
import os
import time
import telepot
import datetime
from subprocess import PIPE, Popen
DOWNLOADS_PATH = '/home/users/pi/Downloads'
def cmdline(command):
process = Popen(
args=command,
stdout=PIPE,
shell=True
)
return process.communicate()[0]
def gitCommandHandler(command):
params = command.split(' ')[1:]
if params[0] == 'update':
path = os.path.dirname(os.path.realpath(__file__))
os.system('cd %s; git pull; sudo reboot;' % path)
return True
return False
def torrentCommandHandler(command):
params = command.split(' ')[1:]
if params[0] == 'start':
os.system('deluged')
return True
elif params[0] == 'add':
return cmdline('deluge-console "add {}; exit;"'.format(params[1]))
elif params[0] == 'show':
files = [f for f in os.listdir(DOWNLOADS_PATH) if os.path.isfile(os.path.join(DOWNLOADS_PATH, f))]
return files
elif params[0] == 'current':
return cmdline('deluge-console "info; exit;"')
return False
def handle(msg):
chat_id = msg['chat']['id']
command = msg['text']
print('Got command: %s' % command)
bot.sendMessage(chat_id, "Acknowledged %s" %command)
result = False
if command.startswith('/'):
if command.startswith('/git'):
result = gitCommandHandler(command)
elif command.startswith('/torrent'):
result = torrentCommandHandler(command)
if result:
bot.sendMessage(chat_id, result)
else:
bot.sendMessage(chat_id, "Not a valid command")
token = os.getenv("TELEGRAM_TOKEN")
if token:
try:
bot = telepot.Bot(token)
bot.message_loop(handle)
print('Bot initiated at ', datetime.datetime.now())
except:
exit()
else:
print("Set TELEGRAM_TOKEN in env")
exit()
while 1:
try:
time.sleep(10)
except KeyboardInterrupt:
print('\n Program interrupted')
exit()
except:
print('Other error or exception occured!')
exit()