-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.py
More file actions
64 lines (55 loc) · 2.46 KB
/
UI.py
File metadata and controls
64 lines (55 loc) · 2.46 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
import ProcessHandler as Handler
import threading
from json import load
import telegram
from telegram.ext import Updater, MessageHandler, Filters, CommandHandler
import time
config = load(open(Handler.baseDir + 'config.json'))
session = Updater(token = config['token'], use_context = True)
tasks = []
for item in config:
if (item != 'token'):
tasks.append(item)
def reloadConfig():
global config
config = load(open(Handler.baseDir + 'config.json'))
def start(update, context):
update.message.reply_text(text = "What task would you like to check?", reply_markup = telegram.ReplyKeyboardMarkup([[task] for task in tasks]))
task = { # global variable assigned to conversation func, related to the specific task required from user
'name': None, # task name
'action': None, # what to do (start/restart)
'pid': None # required to stop process fastly in restart method
}
def conversation(update, context):
time.sleep(1)
userReply = update.message.text
global task
if (userReply in config): # picking task
Handler.checkTask(userReply) # updates config file with new info 'bout processes
reloadConfig()
plainText = None
if (config[userReply]['status']):
plainText = userReply + " is already running!\nWould you like to restart this task?"
task['action'] = 'restart'
task['pid'] = config[userReply]['pid']
else:
plainText = userReply + " is not running!\nWould you like to start this task?"
task['action'] = 'start'
task['name'] = userReply
update.message.reply_text(text = plainText, reply_markup = telegram.ReplyKeyboardMarkup([["Yes"], ["No"]]))
elif (userReply in ['Yes', 'No']):
if (userReply == "No"):
update.message.reply_text(text = "Have a wonderfoul day! =)", reply_markup = telegram.ReplyKeyboardRemove())
else:
if (task['action'] == 'restart'):
Handler.stopProcess(task)
processThread = threading.Thread(target = Handler.startProcess, args = (task,))
processThread.start()
update.message.reply_text(text = "Task has been successfully " + task['action'] + "ed", reply_markup = telegram.ReplyKeyboardRemove())
def help(update, context):
update.message.reply_text("This is a telegram bot called CoffeeBreaker Web Server Helper.\nI am designed to help you to manage tasks that runs on your server.")
session.dispatcher.add_handler(CommandHandler('start', start))
session.dispatcher.add_handler(CommandHandler('help', help))
session.dispatcher.add_handler(MessageHandler(Filters.text, conversation))
session.start_polling()
session.idle()