diff --git a/bot.py b/bot.py index 193343a..0c06dab 100644 --- a/bot.py +++ b/bot.py @@ -16,12 +16,12 @@ bot = telepot.Bot(config.TOKEN) def on_chat(msg): content_type, chat_type, chat_id = telepot.glance(msg) from_id = msg['from']['id'] - pprint(msg) # 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 - print() # for debug + pprint(msg) + print('content_type:', content_type) + print('chat_type:', chat_type) + print('chat_id:', chat_id) + print('from_id:', from_id) + print() # create a user object user = kuser(from_id) @@ -57,18 +57,6 @@ def on_chat(msg): else: bot.sendMessage(chat_id, "請先私訊我登入 kCOJ", reply_to_message_id=msg['message_id']) - elif user.status == '舊的密碼' and chat_type == 'private': - if user.check_online() == True: - user.press_newpassword(msg['text']) - - elif user.status == '修改密碼' and chat_type == 'private': - if user.check_online() == True: - user.change_password(msg['text']) - - elif user.status == '上傳答案' and chat_type == 'private': - if user.check_online() == True: - user.send_answer(msg['text'], '') - elif command[0] == '/start' or command[0] == '首頁🏠': if user.check_online() == True: user.display_main(chat_id) @@ -87,32 +75,51 @@ def on_chat(msg): time.sleep(1) os._exit(0) - elif (command[0] == '/help' or command[0] == '幫助📚') and chat_type == 'private': - if user.check_online() == True: - user.help_you() + elif chat_type == 'private': + if command[0] == '/help' or command[0] == '幫助📚': + if user.check_online() == True: + user.help_you() - elif (command[0] == '/password' or command[0] == '改密碼💱') and chat_type == 'private': - if user.check_online() == True: - user.press_oldpassword() + elif command[0] == '/password' or command[0] == '改密碼💱': + if user.check_online() == True: + user.press_oldpassword() - elif (command[0] == '/logout' or command[0] == '登出🚪') and chat_type == 'private': - user = kuser(from_id) - users[str(from_id)] = user - user.logout_system() + elif command[0] == '/logout' or command[0] == '登出🚪': + user = kuser(from_id) + users[str(from_id)] = user + user.logout_system() - elif user.question != '題外': - if user.check_online() == True: - if (command[0] == '/upload' or command[0] == '交作業📮') and chat_type == 'private': - user.upload_answer() - elif (command[0] == '/result' or command[0] == '看結果☑️') and chat_type == 'private': - user.list_results() - elif (command[0] == '/passer' or command[0] == '通過者🌐') and chat_type == 'private': - user.list_passers() - elif command[0] == '回題目📜': + elif command[0] == '回題目📜' and user.question != '題外': + if user.check_online() == True: user.display_question(chat_id ,user.question) - else: - if chat_type == 'private': - bot.sendMessage(chat_id, "(ˊ・ω・ˋ)") + + elif (command[0] == '/upload' or command[0] == '交作業📮') and user.question != '題外': + if user.check_online() == True: + user.upload_answer() + + elif (command[0] == '/result' or command[0] == '看結果☑️') and user.question != '題外': + if user.check_online() == True: + user.list_results() + + elif (command[0] == '/passer' or command[0] == '通過者🌐') and user.question != '題外': + if user.check_online() == True: + user.list_passers() + + elif user.status == '舊的密碼': + if user.check_online() == True: + user.press_newpassword(msg['text']) + + elif user.status == '修改密碼': + if user.check_online() == True: + user.change_password(msg['text']) + + elif user.status == '上傳答案': + if user.check_online() == True: + user.send_answer(msg['text'], '') + + else: + if user.check_online() == True: + bot.sendMessage(chat_id, "(ˊ・ω・ˋ)") elif content_type == 'document': if user.status == '上傳答案' or user.status == '查看題目': diff --git a/interface.py b/interface.py index eabd675..afb58f4 100644 --- a/interface.py +++ b/interface.py @@ -45,25 +45,30 @@ class kuser: ], resize_keyboard=True)) def press_newpassword(self, text): - self.status = '修改密碼' - self.question = '題外' if text != self.password: - self.display_main(self.userid) + self.status = '正常使用' + self.question = '題外' + bot.sendMessage(self.userid, "密碼錯誤!", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + ["首頁🏠"] + ], resize_keyboard=True)) else: + self.status = '修改密碼' + self.question = '題外' bot.sendMessage(self.userid, "使用此功能請務必小心!\n" - "請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove()) + "請輸入要設定的新密碼:", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + ["首頁🏠"] + ], resize_keyboard=True)) def change_password(self, text): self.status = '正常使用' self.question = '題外' - if text == "首頁🏠": - self.display_main(self.userid) - else: - self.password = text - bot.sendMessage(self.userid, "修改成功" if self.api.change_password(self.password) == True else "修改失敗", - reply_markup=ReplyKeyboardMarkup(keyboard=[ - ["首頁🏠"] - ], resize_keyboard=True)) + self.password = text + bot.sendMessage(self.userid, "修改成功!" if self.api.change_password(self.password) == True else "修改失敗。", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + ["首頁🏠"] + ], resize_keyboard=True)) def login_kcoj(self, text): self.status = '正常使用' @@ -79,25 +84,28 @@ class kuser: bot.sendMessage(self.userid, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) self.press_username() - def check_online(self): # edit - self.status = '正常使用' + def check_online(self): result = self.api.check_online() if result == None: - self.question = '題外' - bot.sendMessage(self.userid, "郭老 Online Judge 離線中!", - reply_markup=ReplyKeyboardMarkup(keyboard=[ - ["首頁🏠"] - ], resize_keyboard=True)) + self.fail_connecting() return False - elif result == True: - return True else: - self.api.login_kcoj(self.username, self.password) - if self.api.check_online() == False: + if result == False: + self.api.login_kcoj(self.username, self.password) + result = self.api.check_online() + if result == False: self.fail_login() - return False - else: - return True + elif result == None: + self.fail_connecting() + return result == True + + def fail_connecting(self): + self.status = '正常使用' + self.question = '題外' + bot.sendMessage(self.userid, "郭老 Online Judge 離線中!", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + ["首頁🏠"] + ], resize_keyboard=True)) def logout_system(self): self.status = '正常使用'