From d4620c80b2179513f11cc7a1fd737b1e45b2072b Mon Sep 17 00:00:00 2001 From: PinLin Date: Thu, 24 Aug 2017 02:08:29 +0800 Subject: [PATCH] add group supportion --- bot.py | 66 +++++++++++++++++++++++++++---------------------- interface.py | 70 ++++++++++++++++++++++++++-------------------------- 2 files changed, 72 insertions(+), 64 deletions(-) diff --git a/bot.py b/bot.py index 8fd4597..aad1d5f 100644 --- a/bot.py +++ b/bot.py @@ -34,20 +34,28 @@ def on_chat(msg): # pre-treat the command command = [msg['text']] if msg['text'][0] == '/': - command = msg['text'].replace('_', ' ').lower().split(' ') - command[0] = command[0].replace(config.NAME, '') + command = msg['text'].replace(config.NAME, '').replace('_', ' ').lower().split(' ') # first-time user - if user.status == '第一次用' and chat_type == 'private': - user.new_user() + if user.status == '第一次用': + if chat_type == 'private': + user.new_user() + else: + bot.sendMessage(chat_id, "請先私訊我登入 kCOJ", reply_to_message_id=msg['message_id']) # press password - elif user.status == '輸入學號' and chat_type == 'private': - user.press_password(msg['text']) + elif user.status == '輸入學號': + if chat_type == 'private': + user.press_password(msg['text']) + else: + bot.sendMessage(chat_id, "請先私訊我登入 kCOJ", reply_to_message_id=msg['message_id']) # login - elif user.status == '輸入密碼' and chat_type == 'private': - user.login_kcoj(msg['text']) + elif user.status == '輸入密碼': + if chat_type == 'private': + user.login_kcoj(msg['text']) + 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: @@ -60,30 +68,17 @@ def on_chat(msg): 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() + user.display_main(chat_id) elif command[0] == '/question' or command[0] == '題庫📝' or command[0] == '更新🔃': if user.check_online() == True: if len(command) > 1: - user.display_question(command[1]) + user.display_question(chat_id, command[1]) else: - user.display_questions() - - elif command[0] == '/help' or command[0] == '幫助📚': - if user.check_online() == True: - user.help_you() - - elif command[0] == '/password' or command[0] == '改密碼💱': - if user.check_online() == True: - user.press_oldpassword() - - elif command[0] == '/logout' or command[0] == '登出🚪': - user = kuser(from_id, bot) - users[str(from_id)] = user - user.logout_system() + user.display_questions(chat_id) elif command[0] == '/restart': if str(from_id) in config.ADMIN: @@ -92,16 +87,29 @@ 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 (command[0] == '/password' or command[0] == '改密碼💱') and chat_type == 'private': + if user.check_online() == True: + user.press_oldpassword() + + elif (command[0] == '/logout' or command[0] == '登出🚪') and chat_type == 'private': + user = kuser(from_id, bot) + users[str(from_id)] = user + user.logout_system() + elif user.question != '題外': if user.check_online() == True: - if command[0] == '/upload' or command[0] == '交作業📮': + if (command[0] == '/upload' or command[0] == '交作業📮') and chat_type == 'private': user.upload_answer() - elif command[0] == '/result' or command[0] == '看結果☑️': + elif (command[0] == '/result' or command[0] == '看結果☑️') and chat_type == 'private': user.list_results() - elif command[0] == '/passer' or command[0] == '通過者🌐': + elif (command[0] == '/passer' or command[0] == '通過者🌐') and chat_type == 'private': user.list_passers() elif command[0] == '回題目📜': - user.display_question(user.question) + user.display_question(chat_id ,user.question) elif content_type == 'document': if user.status == '上傳答案' or user.status == '查看題目': diff --git a/interface.py b/interface.py index 316f604..41cbf8f 100644 --- a/interface.py +++ b/interface.py @@ -10,8 +10,8 @@ import access import config, promote class kuser: - def __init__(self, chat_id, bot): - self.chat_id = chat_id + def __init__(self, from_id, bot): + self.from_id = from_id self.bot = bot self.username = '' self.password = '' @@ -26,19 +26,19 @@ class kuser: def press_username(self): self.status = '輸入學號' self.question = '題外' - self.bot.sendMessage(self.chat_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) + self.bot.sendMessage(self.from_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) def press_password(self, text): self.status = '輸入密碼' self.question = '題外' self.username = text - self.bot.sendMessage(self.chat_id, "輸入完可刪除訊息以策安全!\n" + self.bot.sendMessage(self.from_id, "輸入完可刪除訊息以策安全!\n" "請輸入您的密碼:", reply_markup=ReplyKeyboardRemove()) def press_oldpassword(self): self.status = '舊的密碼' self.question = '題外' - self.bot.sendMessage(self.chat_id, "請輸入要原本的舊密碼:", + self.bot.sendMessage(self.from_id, "請輸入要原本的舊密碼:", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠"] ], resize_keyboard=True)) @@ -47,19 +47,19 @@ class kuser: self.status = '修改密碼' self.question = '題外' if text != self.password: - self.display_main() + self.display_main(self.from_id) else: - self.bot.sendMessage(self.chat_id, "使用此功能請務必小心!\n" + self.bot.sendMessage(self.from_id, "使用此功能請務必小心!\n" "請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove()) def change_password(self, text): self.status = '正常使用' self.question = '題外' if text == "首頁🏠": - self.display_main() + self.display_main(self.from_id) else: self.password = text - self.bot.sendMessage(self.chat_id, "修改成功" if self.api.change_password(self.password) == True else "修改失敗", + self.bot.sendMessage(self.from_id, "修改成功" if self.api.change_password(self.password) == True else "修改失敗", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠"] ], resize_keyboard=True)) @@ -68,14 +68,14 @@ class kuser: self.status = '正常使用' self.question = '題外' self.password = text - self.bot.sendMessage(self.chat_id, "登入中...", reply_markup=ReplyKeyboardRemove()) + self.bot.sendMessage(self.from_id, "登入中...", reply_markup=ReplyKeyboardRemove()) if self.check_online() == True: - self.display_main() + self.display_main(self.from_id) def fail_login(self): self.status = '正常使用' self.question = '題外' - self.bot.sendMessage(self.chat_id, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) + self.bot.sendMessage(self.from_id, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) self.press_username() def check_online(self): # edit @@ -83,7 +83,7 @@ class kuser: result = self.api.check_online() if result == None: self.question = '題外' - self.bot.sendMessage(self.chat_id, "郭老 Online Judge 離線中!", + self.bot.sendMessage(self.from_id, "郭老 Online Judge 離線中!", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠"] ], resize_keyboard=True)) @@ -101,10 +101,10 @@ class kuser: def logout_system(self): self.status = '正常使用' self.question = '題外' - self.bot.sendMessage(self.chat_id, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove()) + self.bot.sendMessage(self.from_id, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove()) self.press_username() - def display_main(self): + def display_main(self, chat_id): self.status = '正常使用' self.question = '題外' q_dict = self.api.list_questions() @@ -114,7 +114,7 @@ class kuser: q_str += "📗" + key + " (DL: " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]]" q_str += "⚠️" if q_dict[key][2] == '未繳' else "✅" q_str += " /question_" + key + "\n\n" - self.bot.sendMessage(self.chat_id, "💁 " + self.username + " " + config.NAME + "\n" + self.bot.sendMessage(chat_id, "💁 " + self.username + " " + config.NAME + "\n" "➖➖➖➖➖\n" "📝可繳交的作業\n\n" + q_str + \ "➖➖➖➖➖\n" + choice(promote.sentences), @@ -122,9 +122,9 @@ class kuser: reply_markup=ReplyKeyboardMarkup(keyboard=[ ["題庫📝"], ["登出🚪", "改密碼💱", "幫助📚"] - ], resize_keyboard=True)) + ], resize_keyboard=True) if chat_id == self.from_id else ReplyKeyboardRemove()) - def display_questions(self): + def display_questions(self, chat_id): self.status = '正常使用' self.question = '題外' q_dict = self.api.list_questions() @@ -134,7 +134,7 @@ class kuser: q_str += "" + key + " (DL: " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]]" q_str += "⚠️" if q_dict[key][2] == '未繳' else "✅" q_str += " /question_" + key + "\n\n" - reply = self.bot.sendMessage(self.chat_id, "💁 " + self.username + " " + config.NAME + "\n" + reply = self.bot.sendMessage(chat_id, "💁 " + self.username + " " + config.NAME + "\n" "➖➖➖➖➖\n" "📝所有作業\n\n" + q_str + \ "➖➖➖➖➖\n" + choice(promote.sentences), @@ -142,10 +142,10 @@ class kuser: reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "更新🔃"], ["登出🚪", "改密碼💱", "幫助📚"] - ], resize_keyboard=True)) - self.bot.sendMessage(self.chat_id, "點我到題庫頂", reply_to_message_id=reply['message_id']) + ], resize_keyboard=True) if chat_id == self.from_id else ReplyKeyboardRemove()) + self.bot.sendMessage(self.from_id, "點我到題庫頂", reply_to_message_id=reply['message_id']) - def display_question(self, number): + def display_question(self, chat_id, number): self.status = '查看題目' self.question = number content = self.api.show_question(number) @@ -155,18 +155,18 @@ class kuser: q_str += "📗" if q[1] == '期限未到' else "📕" q_str += "*" + number + "* (DL: " + q[0] + ")\n [[[" + q[2] + "]]]" q_str += "⚠️" if q[2] == '未繳' else "✅" - reply = self.bot.sendMessage(self.chat_id, q_str + "\n\n```\n" + content + "\n```", + reply = self.bot.sendMessage(chat_id, q_str + "\n\n```\n" + content + "\n```", parse_mode='Markdown', reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "題庫📝"], ["交作業📮" if q[1] == '期限未到' else '', "看結果☑️" if q[2] == '已繳' else '', "通過者🌐"], ["登出🚪", "改密碼💱", "幫助📚"] - ], resize_keyboard=True)) - self.bot.sendMessage(self.chat_id, "點我到題目頂", reply_to_message_id=reply['message_id']) + ], resize_keyboard=True) if chat_id == self.from_id else ReplyKeyboardRemove()) + self.bot.sendMessage(chat_id, "點我到題目頂", reply_to_message_id=reply['message_id']) def help_you(self): self.question = '題外' - self.bot.sendMessage(self.chat_id, "這裡是 kC Online Judge Bot!\n" + self.bot.sendMessage(self.from_id, "這裡是 kC Online Judge Bot!\n" "可以簡稱 kCOJ Bot,目前定居於 [" + config.NAME + "]\n" "作用是讓大家可以方便的透過我使用郭老程設課的 Online Judge\n" "➡️[傳送門](" + config.URL + ")\n" @@ -198,7 +198,7 @@ class kuser: q_str += "📗" if q[1] == '期限未到' else "📕" q_str += "" + self.question + " (DL: " + q[0] + ")\n [[" + q[2] + "]]" q_str += "⚠️" if q[2] == '未繳' else "✅" - self.bot.sendMessage(self.chat_id, q_str + "\n\n現在請把你的程式碼讓我看看(請別超過 20 MB)\n" + self.bot.sendMessage(self.from_id, q_str + "\n\n現在請把你的程式碼讓我看看(請別超過 20 MB)\n" "可以使用「文字訊息」或是「傳送檔案」的方式", parse_mode='HTML', reply_markup=ReplyKeyboardMarkup(keyboard=[ ["刪除作業⚔️"] if self.api.list_questions()[self.question][2] == '已繳' else [], @@ -210,14 +210,14 @@ class kuser: if text != '': if text == '首頁🏠': if self.check_online() == True: - self.display_main() + self.display_main(self.from_id) return elif text == '回題目📜': if self.check_online() == True: self.display_question(self.question) return elif text == '刪除作業⚔️': - self.bot.sendMessage(self.chat_id, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗", + self.bot.sendMessage(self.from_id, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "回題目📜"], ["登出🚪", "改密碼💱", "幫助📚"] @@ -231,14 +231,14 @@ class kuser: self.bot.download_file(file_id, self.username + self.question + '.c') self.api.delete_answer(self.question) if self.api.upload_answer(self.question, self.username + self.question + '.c') == True: - self.bot.sendMessage(self.chat_id, "上傳成功", + self.bot.sendMessage(self.from_id, "上傳成功", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "回題目📜"], ["看結果☑️"], ["登出🚪", "改密碼💱", "幫助📚"] ], resize_keyboard=True)) else: - self.bot.sendMessage(self.chat_id, "上傳失敗", + self.bot.sendMessage(self.from_id, "上傳失敗", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "回題目📜"], ["登出🚪", "改密碼💱", "幫助📚"] @@ -247,7 +247,7 @@ class kuser: def fail_send(self): self.status = '正常使用' - self.bot.sendMessage(self.chat_id, "檔案不能超過 20 MB!上傳失敗", + self.bot.sendMessage(self.from_id, "檔案不能超過 20 MB!上傳失敗", reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "回題目📜"], ["登出🚪", "改密碼💱", "幫助📚"] @@ -264,13 +264,13 @@ class kuser: q_str += "\n" for passer in self.api.list_passers(self.question): q_str += "\n" + passer - reply = self.bot.sendMessage(self.chat_id, q_str + "", + reply = self.bot.sendMessage(self.from_id, q_str + "", parse_mode='HTML', reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "回題目📜"], ["登出🚪", "改密碼💱", "幫助📚"] ], resize_keyboard=True)) - self.bot.sendMessage(self.chat_id, "點我到名單頂", reply_to_message_id=reply['message_id']) + self.bot.sendMessage(self.from_id, "點我到名單頂", reply_to_message_id=reply['message_id']) def list_results(self): self.status = '正常使用' @@ -283,7 +283,7 @@ class kuser: q_str += "\n測試編號 " + result[0] + ":" q_str += "✔️ " if result[1] == '通過測試' else "❌ " q_str += result[1] - self.bot.sendMessage(self.chat_id, q_str, + self.bot.sendMessage(self.from_id, q_str, parse_mode='HTML', reply_markup=ReplyKeyboardMarkup(keyboard=[ ["首頁🏠", "回題目📜"],