change kuser's architecture

This commit is contained in:
2017-08-24 16:11:51 +08:00
parent b7ef3201d4
commit 2c541af2f9
2 changed files with 84 additions and 83 deletions

4
bot.py
View File

@ -24,7 +24,7 @@ def on_chat(msg):
print() # for debug print() # for debug
# create a user object # create a user object
user = kuser(from_id, bot) user = kuser(from_id)
if str(from_id) in users: if str(from_id) in users:
user = users[str(from_id)] user = users[str(from_id)]
else: else:
@ -96,7 +96,7 @@ def on_chat(msg):
user.press_oldpassword() user.press_oldpassword()
elif (command[0] == '/logout' or command[0] == '登出🚪') and chat_type == 'private': elif (command[0] == '/logout' or command[0] == '登出🚪') and chat_type == 'private':
user = kuser(from_id, bot) user = kuser(from_id)
users[str(from_id)] = user users[str(from_id)] = user
user.logout_system() user.logout_system()

View File

@ -9,14 +9,15 @@ import access
# configurations # configurations
import config, promote import config, promote
bot = telepot.Bot(config.TOKEN)
class kuser: class kuser:
def __init__(self, from_id, bot): def __init__(self, uid, un='', pw='', st='第一次用', qu='題外'):
self.from_id = from_id self.userid = uid
self.bot = bot self.username = un
self.username = '' self.password = pw
self.password = '' self.status = st
self.status = '第一次用' self.question = qu
self.question = '題外'
self.api = access.kuser_api() self.api = access.kuser_api()
def new_user(self): def new_user(self):
@ -26,19 +27,19 @@ class kuser:
def press_username(self): def press_username(self):
self.status = '輸入學號' self.status = '輸入學號'
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.from_id, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self.userid, "請輸入您的學號:", reply_markup=ReplyKeyboardRemove())
def press_password(self, text): def press_password(self, text):
self.status = '輸入密碼' self.status = '輸入密碼'
self.question = '題外' self.question = '題外'
self.username = text self.username = text
self.bot.sendMessage(self.from_id, "輸入完可刪除訊息以策安全!\n" bot.sendMessage(self.userid, "輸入完可刪除訊息以策安全!\n"
"請輸入您的密碼:", reply_markup=ReplyKeyboardRemove()) "請輸入您的密碼:", reply_markup=ReplyKeyboardRemove())
def press_oldpassword(self): def press_oldpassword(self):
self.status = '舊的密碼' self.status = '舊的密碼'
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.from_id, "請輸入要原本的舊密碼:", bot.sendMessage(self.userid, "請輸入要原本的舊密碼:",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -47,19 +48,19 @@ class kuser:
self.status = '修改密碼' self.status = '修改密碼'
self.question = '題外' self.question = '題外'
if text != self.password: if text != self.password:
self.display_main(self.from_id) self.display_main(self.userid)
else: else:
self.bot.sendMessage(self.from_id, "使用此功能請務必小心!\n" bot.sendMessage(self.userid, "使用此功能請務必小心!\n"
"請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove()) "請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove())
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.from_id) self.display_main(self.userid)
else: else:
self.password = text self.password = text
self.bot.sendMessage(self.from_id, "修改成功" if self.api.change_password(self.password) == True else "修改失敗", bot.sendMessage(self.userid, "修改成功" if self.api.change_password(self.password) == True else "修改失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -68,14 +69,14 @@ class kuser:
self.status = '正常使用' self.status = '正常使用'
self.question = '題外' self.question = '題外'
self.password = text self.password = text
self.bot.sendMessage(self.from_id, "登入中...", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self.userid, "登入中...", reply_markup=ReplyKeyboardRemove())
if self.check_online() == True: if self.check_online() == True:
self.display_main(self.from_id) self.display_main(self.userid)
def fail_login(self): def fail_login(self):
self.status = '正常使用' self.status = '正常使用'
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.from_id, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self.userid, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove())
self.press_username() self.press_username()
def check_online(self): # edit def check_online(self): # edit
@ -83,7 +84,7 @@ class kuser:
result = self.api.check_online() result = self.api.check_online()
if result == None: if result == None:
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.from_id, "郭老 Online Judge 離線中!", bot.sendMessage(self.userid, "郭老 Online Judge 離線中!",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -101,7 +102,7 @@ class kuser:
def logout_system(self): def logout_system(self):
self.status = '正常使用' self.status = '正常使用'
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.from_id, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self.userid, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove())
self.press_username() self.press_username()
def display_main(self, chat_id): def display_main(self, chat_id):
@ -114,7 +115,7 @@ class kuser:
q_str += "📗<b>" + key + "</b> (DL: " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]]" q_str += "📗<b>" + key + "</b> (DL: " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]]"
q_str += "⚠️" if q_dict[key][2] == '未繳' else "" q_str += "⚠️" if q_dict[key][2] == '未繳' else ""
q_str += " /question_" + key + "\n\n" q_str += " /question_" + key + "\n\n"
self.bot.sendMessage(chat_id, "💁 <b>" + self.username + "</b> " + config.NAME + "\n" bot.sendMessage(chat_id, "💁 <b>" + self.username + "</b> " + config.NAME + "\n"
"\n" "\n"
"📝<i>可繳交的作業</i>\n\n" + q_str + \ "📝<i>可繳交的作業</i>\n\n" + q_str + \
"\n" + choice(promote.sentences), "\n" + choice(promote.sentences),
@ -122,7 +123,7 @@ class kuser:
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["題庫📝"], ["題庫📝"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True) if chat_id == self.from_id else ReplyKeyboardRemove()) ], resize_keyboard=True) if chat_id == self.userid else ReplyKeyboardRemove())
def display_questions(self, chat_id): def display_questions(self, chat_id):
self.status = '正常使用' self.status = '正常使用'
@ -134,7 +135,7 @@ class kuser:
q_str += "<b>" + key + "</b> (DL: " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]]" q_str += "<b>" + key + "</b> (DL: " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]]"
q_str += "⚠️" if q_dict[key][2] == '未繳' else "" q_str += "⚠️" if q_dict[key][2] == '未繳' else ""
q_str += " /question_" + key + "\n\n" q_str += " /question_" + key + "\n\n"
reply = self.bot.sendMessage(chat_id, "💁 <b>" + self.username + "</b> " + config.NAME + "\n" reply = bot.sendMessage(chat_id, "💁 <b>" + self.username + "</b> " + config.NAME + "\n"
"\n" "\n"
"📝<i>所有作業</i>\n\n" + q_str + \ "📝<i>所有作業</i>\n\n" + q_str + \
"\n" + choice(promote.sentences), "\n" + choice(promote.sentences),
@ -142,8 +143,8 @@ class kuser:
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "更新🔃"], ["首頁🏠", "更新🔃"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True) if chat_id == self.from_id else ReplyKeyboardRemove()) ], resize_keyboard=True) if chat_id == self.userid else ReplyKeyboardRemove())
self.bot.sendMessage(chat_id, "點我到題庫頂", reply_to_message_id=reply['message_id']) bot.sendMessage(chat_id, "點我到題庫頂", reply_to_message_id=reply['message_id'])
def display_question(self, chat_id, number): def display_question(self, chat_id, number):
self.status = '查看題目' self.status = '查看題目'
@ -155,18 +156,18 @@ class kuser:
q_str += "📗" if q[1] == '期限未到' else "📕" q_str += "📗" if q[1] == '期限未到' else "📕"
q_str += "*" + number + "* (DL: " + q[0] + ")\n [[[" + q[2] + "]]]" q_str += "*" + number + "* (DL: " + q[0] + ")\n [[[" + q[2] + "]]]"
q_str += "⚠️" if q[2] == '未繳' else "" q_str += "⚠️" if q[2] == '未繳' else ""
reply = self.bot.sendMessage(chat_id, q_str + "\n\n```\n" + content + "\n```", reply = bot.sendMessage(chat_id, q_str + "\n\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) if chat_id == self.from_id else ReplyKeyboardRemove()) ], resize_keyboard=True) if chat_id == self.userid else ReplyKeyboardRemove())
self.bot.sendMessage(chat_id, "點我到題目頂", reply_to_message_id=reply['message_id']) bot.sendMessage(chat_id, "點我到題目頂", reply_to_message_id=reply['message_id'])
def help_you(self): def help_you(self):
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.from_id, "這裡是 kC Online Judge Bot\n" bot.sendMessage(self.userid, "這裡是 kC Online Judge Bot\n"
"可以簡稱 kCOJ Bot目前定居於 [" + config.NAME + "]\n" "可以簡稱 kCOJ Bot目前定居於 [" + config.NAME + "]\n"
"作用是讓大家可以方便的透過我使用郭老程設課的 Online Judge\n" "作用是讓大家可以方便的透過我使用郭老程設課的 Online Judge\n"
"➡️[傳送門](" + config.URL + ")\n" "➡️[傳送門](" + config.URL + ")\n"
@ -198,7 +199,7 @@ class kuser:
q_str += "📗" if q[1] == '期限未到' else "📕" q_str += "📗" if q[1] == '期限未到' else "📕"
q_str += "<b>" + self.question + "</b> (DL: " + q[0] + ")\n [[" + q[2] + "]]" q_str += "<b>" + self.question + "</b> (DL: " + q[0] + ")\n [[" + q[2] + "]]"
q_str += "⚠️" if q[2] == '未繳' else "" q_str += "⚠️" if q[2] == '未繳' else ""
self.bot.sendMessage(self.from_id, q_str + "\n\n現在請把你的程式碼讓我看看(請別超過 20 MB\n" bot.sendMessage(self.userid, q_str + "\n\n現在請把你的程式碼讓我看看(請別超過 20 MB\n"
"可以使用「文字訊息」或是「傳送檔案」的方式", 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 [],
@ -210,14 +211,14 @@ class kuser:
if text != '': if text != '':
if text == '首頁🏠': if text == '首頁🏠':
if self.check_online() == True: if self.check_online() == True:
self.display_main(self.from_id) self.display_main(self.userid)
return return
elif text == '回題目📜': elif text == '回題目📜':
if self.check_online() == True: if self.check_online() == True:
self.display_question(self.question) self.display_question(self.question)
return return
elif text == '刪除作業⚔️': elif text == '刪除作業⚔️':
self.bot.sendMessage(self.from_id, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗", bot.sendMessage(self.userid, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
@ -228,17 +229,17 @@ class kuser:
f.write(text) f.write(text)
f.close() f.close()
else: else:
self.bot.download_file(file_id, self.username + self.question + '.c') bot.download_file(file_id, self.username + self.question + '.c')
self.api.delete_answer(self.question) self.api.delete_answer(self.question)
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.from_id, "上傳成功", bot.sendMessage(self.userid, "上傳成功",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["看結果☑️"], ["看結果☑️"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
else: else:
self.bot.sendMessage(self.from_id, "上傳失敗", bot.sendMessage(self.userid, "上傳失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
@ -247,7 +248,7 @@ class kuser:
def fail_send(self): def fail_send(self):
self.status = '正常使用' self.status = '正常使用'
self.bot.sendMessage(self.from_id, "檔案不能超過 20 MB上傳失敗", bot.sendMessage(self.userid, "檔案不能超過 20 MB上傳失敗",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
@ -264,13 +265,13 @@ class kuser:
q_str += "<code>\n" q_str += "<code>\n"
for passer in self.api.list_passers(self.question): for passer in self.api.list_passers(self.question):
q_str += "\n" + passer q_str += "\n" + passer
reply = self.bot.sendMessage(self.from_id, q_str + "</code>", reply = bot.sendMessage(self.userid, q_str + "</code>",
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "回題目📜"], ["首頁🏠", "回題目📜"],
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
self.bot.sendMessage(self.from_id, "點我到名單頂", reply_to_message_id=reply['message_id']) bot.sendMessage(self.userid, "點我到名單頂", reply_to_message_id=reply['message_id'])
def list_results(self): def list_results(self):
self.status = '正常使用' self.status = '正常使用'
@ -283,7 +284,7 @@ class kuser:
q_str += "\n測試編號 <code>" + result[0] + "</code>" q_str += "\n測試編號 <code>" + result[0] + "</code>"
q_str += "✔️ " if result[1] == '通過測試' else "" q_str += "✔️ " if result[1] == '通過測試' else ""
q_str += result[1] q_str += result[1]
self.bot.sendMessage(self.from_id, q_str, bot.sendMessage(self.userid, q_str,
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "回題目📜"], ["首頁🏠", "回題目📜"],