change architecture and user interface

1. try to add group supportion
2. simply some buttons
3. add function of restarting
This commit is contained in:
2017-08-23 17:49:31 +08:00
parent f94a61e9a8
commit 62e0973ead
3 changed files with 53 additions and 39 deletions

55
bot.py
View File

@ -10,53 +10,62 @@ from interface import kuser
# configurations # configurations
import config import config
restart = False
users = {}
bot = telepot.Bot(config.TOKEN)
def on_chat(msg): def on_chat(msg):
content_type, chat_type, chat_id = telepot.glance(msg) content_type, chat_type, chat_id = telepot.glance(msg)
from_id = msg['from']['id']
pprint(msg) # for debug pprint(msg) # for debug
print(content_type, chat_type, chat_id) # for debug print('content_type:', content_type) # for debug
print('chat_type:', chat_type) # for debug
print('chat_id:', chat_id) # for debug
print('from_id:', from_id) # for debug
# create a user object # create a user object
user = kuser(chat_id, bot) user = kuser(from_id, bot)
if str(chat_id) in users: if str(from_id) in users:
user = users[str(chat_id)] user = users[str(from_id)]
else: else:
users[str(chat_id)] = user users[str(from_id)] = user
if content_type == 'text': if content_type == 'text':
# pre-treat the command # pre-treat the command
command = [msg['text']] command = [msg['text']]
if msg['text'][0] == '/': if msg['text'][0] == '/':
command = msg['text'].replace('_', ' ').lower().split(' ') command = msg['text'].replace('_', ' ').lower().split(' ')
command = command.replace(config.NAME, '')
# first-time user # first-time user
if user.status == '第一次用': if user.status == '第一次用' and chat_type == 'private':
user.new_user() user.new_user()
# press password # press password
elif user.status == '輸入學號': elif user.status == '輸入學號' and chat_type == 'private':
user.press_password(msg['text']) user.press_password(msg['text'])
# login # login
elif user.status == '輸入密碼': elif user.status == '輸入密碼' and chat_type == 'private':
user.login_kcoj(msg['text']) user.login_kcoj(msg['text'])
elif user.status == '舊的密碼': elif user.status == '舊的密碼' and chat_type == 'private':
if user.check_online() == True: if user.check_online() == True:
user.press_newpassword(msg['text']) user.press_newpassword(msg['text'])
elif user.status == '修改密碼': elif user.status == '修改密碼' and chat_type == 'private':
if user.check_online() == True: if user.check_online() == True:
user.change_password(msg['text']) user.change_password(msg['text'])
elif user.status == '上傳答案': elif user.status == '上傳答案' and chat_type == 'private':
if user.check_online() == True: if user.check_online() == True:
user.send_answer(msg['text'], '') user.send_answer(msg['text'], '')
elif command[0] == '/start' or command[0] == '主畫面🏠': elif command[0] == '/start' or command[0] == '首頁🏠':
if user.check_online() == True: if user.check_online() == True:
user.display_main() user.display_main()
elif command[0] == '/question' or command[0] == '題庫📝' or command[0] == '回題庫📝' or command[0] == '更新🔃': elif command[0] == '/question' or command[0] == '題庫📝' or command[0] == '更新🔃':
if user.check_online() == True: if user.check_online() == True:
if len(command) > 1: if len(command) > 1:
user.display_question(command[1]) user.display_question(command[1])
@ -72,10 +81,15 @@ def on_chat(msg):
user.press_oldpassword() user.press_oldpassword()
elif command[0] == '/logout' or command[0] == '登出🚪': elif command[0] == '/logout' or command[0] == '登出🚪':
user = kuser(chat_id, bot) user = kuser(from_id, bot)
users[str(chat_id)] = user users[str(from_id)] = user
user.logout_system() user.logout_system()
elif command[0] == '/restart':
if from_id in config.ADMIN:
bot.sendMessage(chat_id, "即將重新啟動")
restart = True
elif user.question != '題外': elif user.question != '題外':
if user.check_online() == True: if user.check_online() == True:
if command[0] == '/upload' or command[0] == '交作業📮': if command[0] == '/upload' or command[0] == '交作業📮':
@ -96,14 +110,9 @@ def on_chat(msg):
user.fail_send() user.fail_send()
else: else:
user.send_answer('', msg['document']['file_id']) user.send_answer('', msg['document']['file_id'])
else:
bot.sendMessage(chat_id, "是擅長寫扣的朋友呢!")
else:
bot.sendMessage(chat_id, "我不是來看這些的。")
users = {}
bot = telepot.Bot(config.TOKEN)
MessageLoop(bot, on_chat).run_as_thread() MessageLoop(bot, on_chat).run_as_thread()
print("Start...")
# for debug # for debug
def main(): def main():
@ -112,5 +121,7 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()
while True: while True:
time.sleep(100) if restart == True:
break
time.sleep(1)
bot.getMe() bot.getMe()

View File

@ -1,4 +1,7 @@
# kCOJ_bot
TOKEN = ""
# kCOJ # kCOJ
URL = "http://140.124.184.228:8080/Exam" # The Great kCOJ URL = "http://140.124.184.228:8080/Exam" # The Great kCOJ
# kCOJ_bot
TOKEN = ""
NAME = ""
# Admin
ADMIN = []

View File

@ -40,7 +40,7 @@ class kuser:
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.chat_id, "請輸入要原本的舊密碼:", self.bot.sendMessage(self.chat_id, "請輸入要原本的舊密碼:",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
def press_newpassword(self, text): def press_newpassword(self, text):
@ -55,13 +55,13 @@ class kuser:
def change_password(self, text): def change_password(self, text):
self.status = '正常使用' self.status = '正常使用'
self.question = '題外' self.question = '題外'
if text == "主畫面🏠": if text == "首頁🏠":
self.display_main() self.display_main()
else: else:
self.password = text self.password = text
self.bot.sendMessage(self.chat_id, "修改成功" if self.api.change_password(self.password) == True else "修改失敗", self.bot.sendMessage(self.chat_id, "修改成功" if self.api.change_password(self.password) == True else "修改失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
def login_kcoj(self, text): def login_kcoj(self, text):
@ -85,7 +85,7 @@ class kuser:
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.chat_id, "郭老 Online Judge 離線中!", self.bot.sendMessage(self.chat_id, "郭老 Online Judge 離線中!",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
return False return False
elif result == True: elif result == True:
@ -150,7 +150,7 @@ class kuser:
"你今天寫扣了嗎?", # todo "你今天寫扣了嗎?", # todo
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "更新🔃"], ["首頁🏠", "更新🔃"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -164,7 +164,7 @@ class kuser:
self.bot.sendMessage(self.chat_id, q_str + "\n```\n" + content + "\n```", self.bot.sendMessage(self.chat_id, q_str + "\n```\n" + content + "\n```",
parse_mode='Markdown', parse_mode='Markdown',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "題庫📝"], ["首頁🏠", "題庫📝"],
["交作業📮" if q[1] == '期限未到' else '', "看結果☑️" if q[2] == '已繳' else '', "通過者🌐"], ["交作業📮" if q[1] == '期限未到' else '', "看結果☑️" if q[2] == '已繳' else '', "通過者🌐"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -184,7 +184,7 @@ class kuser:
"6. 我以我的人格擔保,不會使用您提供的資訊侵害您的權利\n\n" "6. 我以我的人格擔保,不會使用您提供的資訊侵害您的權利\n\n"
"然後,附上厲害的 [郭老 Online Judge 傳送門](" + config.URL + ")", parse_mode='Markdown', "然後,附上厲害的 [郭老 Online Judge 傳送門](" + config.URL + ")", parse_mode='Markdown',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠"], ["首頁🏠"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True) if self.status == '正常使用' else ReplyKeyboardRemove()) ], resize_keyboard=True) if self.status == '正常使用' else ReplyKeyboardRemove())
self.bot.sendMessage(self.chat_id, "專案授權方式採用 GPLv3\n" self.bot.sendMessage(self.chat_id, "專案授權方式採用 GPLv3\n"
@ -201,13 +201,13 @@ class kuser:
"可以使用「文字訊息」或是「傳送檔案」的方式", parse_mode='HTML', "可以使用「文字訊息」或是「傳送檔案」的方式", parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["刪除作業⚔️"] if self.api.list_questions()[self.question][2] == '已繳' else [], ["刪除作業⚔️"] if self.api.list_questions()[self.question][2] == '已繳' else [],
["主畫面🏠", "回題目📜"] ["首頁🏠", "回題目📜"]
], resize_keyboard=True)) ], resize_keyboard=True))
def send_answer(self, text, file_id): def send_answer(self, text, file_id):
self.status = '正常使用' self.status = '正常使用'
if text != '': if text != '':
if text == '主畫面🏠': if text == '首頁🏠':
if self.check_online() == True: if self.check_online() == True:
self.display_main() self.display_main()
return return
@ -218,7 +218,7 @@ class kuser:
elif text == '刪除作業⚔️': elif text == '刪除作業⚔️':
self.bot.sendMessage(self.chat_id, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗", self.bot.sendMessage(self.chat_id, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
return return
@ -232,14 +232,14 @@ class kuser:
if self.api.upload_answer(self.question, self.username + self.question + '.c') == True: if self.api.upload_answer(self.question, self.username + self.question + '.c') == True:
self.bot.sendMessage(self.chat_id, "上傳成功", self.bot.sendMessage(self.chat_id, "上傳成功",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["看結果☑️"], ["看結果☑️"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
else: else:
self.bot.sendMessage(self.chat_id, "上傳失敗", self.bot.sendMessage(self.chat_id, "上傳失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
os.remove(self.username + self.question + '.c') os.remove(self.username + self.question + '.c')
@ -248,7 +248,7 @@ class kuser:
self.status = '正常使用' self.status = '正常使用'
self.bot.sendMessage(self.chat_id, "檔案不能超過 20 MB上傳失敗", self.bot.sendMessage(self.chat_id, "檔案不能超過 20 MB上傳失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -262,7 +262,7 @@ class kuser:
self.bot.sendMessage(self.chat_id, q_str + "</code>", self.bot.sendMessage(self.chat_id, q_str + "</code>",
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -278,7 +278,7 @@ class kuser:
self.bot.sendMessage(self.chat_id, q_str, self.bot.sendMessage(self.chat_id, q_str,
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["交作業📮" if q[1] == '期限未到' else '', "通過者🌐"], ["交作業📮" if q[1] == '期限未到' else '', "通過者🌐"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))