diff --git a/bot.py b/bot.py index cbe28c3..38afbef 100644 --- a/bot.py +++ b/bot.py @@ -7,172 +7,88 @@ from telepot.namedtuple import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboar from pprint import pprint # kCOJ API import access +from interface import kuser # configurations import config -users = {} -class kuser: - def __init__(self, un='', pw='', st=0): - self.username = un - self.password = pw - self.status = st - self.api = access.kuser_api() - - def test_login(self, chat_id): - self.api.login(self.username, self.password) - if self.api.check_online() == True: - return True - else: - self.status = 1 - bot.sendMessage(chat_id, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) - time.sleep(0.6) - bot.sendMessage(chat_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) - return False - def display_main(self, chat_id): - self.status = 3 - q_list = self.api.list_questions() - q_available = "📝可繳交的作業\n\n" - q_unavailable = "📝沒有可繳交的作業哦!\n" - if q_list == {}: - q_str = q_unavailable - else: - q_str = q_available - for key in q_list.keys(): - if q_list[key][1] == '期限未到': - q_str += "📗" + key + " (到 " + q_list[key][0] + ")\n [" + q_list[key][2] + "] /question_" + key + "\n\n" - if q_str == q_available: - q_str = q_unavailable - bot.sendMessage(chat_id, "💁 " + self.username + " /logout\n" - "➖➖➖➖➖\n" + q_str + "➖➖➖➖➖\n" - "你今天寫扣了嗎?", parse_mode='HTML', - reply_markup=ReplyKeyboardMarkup(keyboard=[ - [KeyboardButton(text="查看題庫📝"), KeyboardButton(text="重新整理🔃")], - [KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")] - ])) - def display_questions(self, chat_id): - q_list = self.api.list_questions() - q_str = '' - for key in q_list.keys(): - if q_list[key][1] == '期限未到': - q_str += "📗" + key + " (到 " + q_list[key][0] + ")\n [" + q_list[key][2] + "] /question_" + key + "\n\n" - else: - q_str += "📕" + key + " (到 " + q_list[key][0] + ")\n [" + q_list[key][2] + "] /question_" + key + "\n\n" - bot.sendMessage(chat_id, "💁 " + self.username + " /logout\n" - "➖➖➖➖➖\n" - "📝所有作業\n\n" + q_str + - "➖➖➖➖➖\n" - "你今天寫扣了嗎?", parse_mode='HTML', - reply_markup=ReplyKeyboardMarkup(keyboard=[ - [KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="重新載入🔃")], - [KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")] - ])) - def display_question(self, chat_id, number): - content = self.api.show_question(number) - q = self.api.list_questions()[number] - q_str = "📗" if q[1] == '期限未到' else "📕" - q_str += "" + number + " (到 " + q[0] + ")" - bot.sendMessage(chat_id, q_str + "\n" + content + "", - parse_mode='HTML', - reply_markup=ReplyKeyboardMarkup(keyboard=[ - [KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="回到題庫📝")], - [KeyboardButton(text="上傳解答📮"), KeyboardButton(text="查看結果☑️"), KeyboardButton(text="通過名單🌐")] if q[1] == '期限未到' else - [KeyboardButton(text="查看結果☑️"), KeyboardButton(text="通過名單🌐")], - [KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")] - ])) - def help_you(self, chat_id): - bot.sendMessage(chat_id, "這裡是 kC Online Judge Bot!\n" - "可以簡稱我為 kCOJ Bot,目前定居於 `@kcoj_bot`\n" - "作用是讓大家可以方便的透過我使用郭老程設課的 OJ\n" - "操作很簡單(?)我想大家應該都不會有問題吧~\n\n" - "不過還是有些需要注意的地方:\n" - "1. 學號與密碼將以「明文」方式儲存。\n" - "2. 反正郭老的 OJ 也是「明文」存您的帳號密碼。\n" - "3. 我以我的人格擔保,不會使用您提供的資訊侵害您的權利", parse_mode="Markdown", - reply_markup=ReplyKeyboardMarkup(keyboard=[ - [KeyboardButton(text="回主畫面🏠")] - ])) - time.sleep(0.6) - bot.sendMessage(chat_id, "\n原始碼被託管於 GitHub,網址如下:\n" - "https://github.com/PinLin/kcoj_bot") - -def split_cmd(text): - if text[0] != '/': - return [text] - else: - cmd = text.replace('_', ' ').strip().lower().split(' ') - return cmd - - def on_chat(msg): - pprint(msg) content_type, chat_type, chat_id = telepot.glance(msg) - print(content_type, chat_type, chat_id) + pprint(msg) # for debug + print(content_type, chat_type, chat_id) # for debug - me = kuser() + # create a user object + user = kuser(chat_id, bot) if str(chat_id) in users: - me = users[str(chat_id)] + user = users[str(chat_id)] else: - users[str(chat_id)] = me + users[str(chat_id)] = user if content_type == 'text': - command = split_cmd(msg['text']) + # pre-treat the command + command = [msg['text']] + if msg['text'][0] == '/': + command = msg['text'].replace('_', ' ').lower().split(' ') + + # first-time user + if user.status == '第一次用': + user.new_user() + + # press password + elif user.status == '輸入學號': + user.press_password(msg['text']) + + # login + elif user.status == '輸入密碼': + user.login_kcoj(msg['text']) + + 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']) - if me.status == 1: - me.status = 2 - me.username = msg['text'] - bot.sendMessage(chat_id, "請輸入您的密碼:") - elif me.status == 2: - me.password = msg['text'] - bot.sendMessage(chat_id, "登入中...") - if me.test_login(chat_id) == True: - me.display_main(chat_id) - elif me.status == 4: - me.password = msg['text'] - bot.sendMessage(chat_id, "修改成功" if me.api.change_password(me.password) == True else "修改失敗") - if me.test_login(chat_id) == True: - me.display_main(chat_id) elif command[0] == '/start' or command[0] == '重新整理🔃' or command[0] == '回主畫面🏠': - if me.status == 0: - me.status = 1 - bot.sendMessage(chat_id, "是初次見面的朋友呢,設定一下吧!", reply_markup=ReplyKeyboardRemove()) - time.sleep(0.6) - bot.sendMessage(chat_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) - else: - if me.test_login(chat_id) == True: - me.display_main(chat_id) + if user.check_online() == True: + user.display_main() + elif command[0] == '/question' or command[0] == '查看題庫📝' or command[0] == '回到題庫📝' or command[0] == '重新載入🔃': - if me.test_login(chat_id) == True: + if user.check_online() == True: if len(command) > 1: - me.display_question(chat_id, command[1]) + user.display_question(command[1]) else: - me.display_questions(chat_id) + user.display_questions() + elif command[0] == '/help' or command[0] == '提供幫助📚': - if me.test_login(chat_id) == True: - me.help_you(chat_id) + if user.check_online() == True: + user.help_you() + elif command[0] == '/password' or command[0] == '修改密碼💱': - if me.test_login(chat_id) == True: - me.status = 4 - bot.sendMessage(chat_id, "請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove()) + if user.check_online() == True: + user.press_oldpassword() + elif command[0] == '/logout' or command[0] == '登出帳號🚪': - bot.sendMessage(chat_id, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove()) - me = kuser() - me.status = 1 - users[str(chat_id)] = me - time.sleep(0.6) - bot.sendMessage(chat_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) + user = kuser(chat_id, bot) + users[str(chat_id)] = user + user.logout_system() + + elif False: + # receive a document + pass else: bot.sendMessage(chat_id, "我不是來看這些的。") +users = {} bot = telepot.Bot(config.TOKEN) MessageLoop(bot, on_chat).run_as_thread() # for debug def main(): pass - # over 20MB will stop this bot! - # bot.download_file('BQADBQADHQADPAc5VEZQejGkM9C4Ag', '../wa.txt') -main() -while True: - time.sleep(10) \ No newline at end of file +if __name__ == '__main__': + main() + while True: + time.sleep(10) \ No newline at end of file diff --git a/interface.py b/interface.py new file mode 100644 index 0000000..b2a873d --- /dev/null +++ b/interface.py @@ -0,0 +1,148 @@ +#! /usr/bin/env python3 + +# necessary modules +import requests, telepot +from telepot.loop import MessageLoop +from telepot.namedtuple import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove +from pprint import pprint +# kCOJ API +import access +# configurations +import config + +class kuser: + def __init__(self, chat_id, bot): + self.chat_id = chat_id + self.bot = bot + self.username = '' + self.password = '' + self.status = '第一次用' + self.api = access.kuser_api() + + def new_user(self): + self.bot.sendMessage(self.chat_id, "是初次見面的朋友呢,設定一下吧!", reply_markup=ReplyKeyboardRemove()) + self.press_username() + + def press_username(self): + self.status = '輸入學號' + self.bot.sendMessage(self.chat_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) + + def press_password(self, text): + self.status = '輸入密碼' + self.username = text + self.bot.sendMessage(self.chat_id, "輸入完可刪除訊息以策安全!\n請輸入您的密碼:", reply_markup=ReplyKeyboardRemove()) + + def press_oldpassword(self): + self.status = '舊的密碼' + self.bot.sendMessage(self.chat_id, "輸入錯誤將取消操作\n" + "請輸入要原本的舊密碼:", reply_markup=ReplyKeyboardRemove()) + + def press_newpassword(self, text): + self.status = '修改密碼' + if text != self.password: + self.display_main() + else: + self.bot.sendMessage(self.chat_id, "使用此功能請務必小心!\n" + "請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove()) + + def change_password(self, text): + self.status = '正常使用' + self.password = text + self.bot.sendMessage(self.chat_id, "修改成功" if self.api.change_password(self.password) == True else "修改失敗", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + [KeyboardButton(text="回主畫面🏠")] + ])) + + def login_kcoj(self, text): + self.status = '正常使用' + self.password = text + self.bot.sendMessage(self.chat_id, "登入中...", reply_markup=ReplyKeyboardRemove()) + if self.check_online() == True: + self.display_main() + + def fail_login(self): + self.bot.sendMessage(self.chat_id, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) + self.press_username() + + def check_online(self): + self.api.login_kcoj(self.username, self.password) + if self.api.check_online() == True: + return True + else: + self.fail_login() + return False + + def logout_system(self): + self.bot.sendMessage(self.chat_id, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove()) + self.press_username() + + def display_main(self): + self.status = '正常使用' + q_dict = self.api.list_questions() + q_available = "📝可繳交的作業\n\n" + q_unavailable = "📝沒有可繳交的作業哦!\n" + if q_dict == {}: + q_str = q_unavailable + else: + q_str = q_available + for key in q_dict.keys(): + if q_dict[key][1] == '期限未到': + q_str += "📗" + key + " (到 " + q_dict[key][0] + ")\n [" + q_dict[key][2] + "] /question_" + key + "\n\n" + if q_str == q_available: + q_str = q_unavailable + self.bot.sendMessage(self.chat_id, "💁 " + self.username + " /logout\n" + "➖➖➖➖➖\n" + q_str + "➖➖➖➖➖\n" + "你今天寫扣了嗎?", + parse_mode='HTML', + reply_markup=ReplyKeyboardMarkup(keyboard=[ + [KeyboardButton(text="查看題庫📝"), KeyboardButton(text="重新整理🔃")], + [KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")] + ])) + + def display_questions(self): + self.status = '正常使用' + q_dict = self.api.list_questions() + q_str = '' + for key in q_dict.keys(): + q_str += "📗" if q_dict[key][1] == '期限未到' else "📕" + q_str += "" + key + " (到 " + q_dict[key][0] + ")\n [" + q_dict[key][2] + "] /question_" + key + "\n\n" + self.bot.sendMessage(self.chat_id, "💁 " + self.username + " /logout\n" + "➖➖➖➖➖\n📝所有作業\n\n" + q_str + "➖➖➖➖➖\n" + "你今天寫扣了嗎?", + parse_mode='HTML', + reply_markup=ReplyKeyboardMarkup(keyboard=[ + [KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="重新載入🔃")], + [KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")] + ])) + + def display_question(self, number): + self = '正常使用' + content = self.api.show_question(number) + q = self.api.list_questions()[number] + q_str = "📗" if q[1] == '期限未到' else "📕" + q_str += "" + number + " (到 " + q[0] + ")" + self.bot.sendMessage(self.chat_id, q_str + "\n" + content + "", + parse_mode='HTML', + reply_markup=ReplyKeyboardMarkup(keyboard=[ + [KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="回到題庫📝")], + [KeyboardButton(text="上傳解答📮"), KeyboardButton(text="查看結果☑️"), KeyboardButton(text="通過名單🌐")] if q[1] == '期限未到' else + [KeyboardButton(text="查看結果☑️"), KeyboardButton(text="通過名單🌐")], + [KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")] + ])) + + def help_you(self): + self.status = '正常使用' + self.bot.sendMessage(self.chat_id, "這裡是 kC Online Judge Bot!\n" + "可以簡稱我為 kCOJ Bot,目前定居於 `@kcoj_bot`\n" + "作用是讓大家可以方便的透過我使用郭老程設課的 Online Judge\n" + "操作很簡單(?)我想大家應該都不會有問題吧~\n\n" + "不過還是有些需要注意的地方:\n" + "1. 學號與密碼將以「明文」方式儲存在記憶體裡,不會儲存在我的硬碟中。\n" + "2. 反正郭老的 Online Judge 也是「明文」存您的帳號密碼。\n" + "3. 我以我的人格擔保,不會使用您提供的資訊侵害您的權利。\n\n" + "然後,附個[郭老 Online Judge 傳送門](" + config.URL + ")", parse_mode="Markdown", + reply_markup=ReplyKeyboardMarkup(keyboard=[ + [KeyboardButton(text="回主畫面🏠")] + ])) + self.bot.sendMessage(self.chat_id, "\n原始碼被託管於 GitHub,網址如下:\n" + "https://github.com/PinLin/kcoj_bot") \ No newline at end of file