change ui text and edit button text

This commit is contained in:
2017-08-14 16:29:24 +08:00
parent ef8314f213
commit 1ba28a1aba
2 changed files with 73 additions and 73 deletions

18
bot.py
View File

@ -52,39 +52,39 @@ def on_chat(msg):
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] == '重新整理🔃' 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])
else: else:
user.display_questions() user.display_questions()
elif command[0] == '/help' or command[0] == '提供幫助📚': elif command[0] == '/help' or command[0] == '幫助📚':
if user.check_online() == True: if user.check_online() == True:
user.help_you() user.help_you()
elif command[0] == '/password' or command[0] == '改密碼💱': elif command[0] == '/password' or command[0] == '改密碼💱':
if user.check_online() == True: if user.check_online() == True:
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(chat_id, bot)
users[str(chat_id)] = user users[str(chat_id)] = user
user.logout_system() user.logout_system()
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] == '交作業📮':
user.upload_answer() user.upload_answer()
elif command[0] == '/result' or command[0] == '看結果☑️': elif command[0] == '/result' or command[0] == '看結果☑️':
user.list_results() user.list_results()
elif command[0] == '/passer' or command[0] == '通過名單🌐': elif command[0] == '/passer' or command[0] == '通過🌐':
user.list_passers() user.list_passers()
elif command[0] == '題目📜': elif command[0] == '回題目📜':
user.display_question(user.question) user.display_question(user.question)
else: else:
bot.sendMessage(chat_id, "快去寫扣啦!") bot.sendMessage(chat_id, "快去寫扣啦!")

View File

@ -2,7 +2,7 @@
# necessary modules # necessary modules
import os, requests, telepot import os, requests, telepot
from telepot.namedtuple import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove from telepot.namedtuple import ReplyKeyboardMarkup, ReplyKeyboardRemove
# kCOJ API # kCOJ API
import access import access
# configurations # configurations
@ -31,14 +31,15 @@ class kuser:
self.status = '輸入密碼' self.status = '輸入密碼'
self.question = '題外' self.question = '題外'
self.username = text self.username = text
self.bot.sendMessage(self.chat_id, "輸入完可刪除訊息以策安全!\n請輸入您的密碼:", reply_markup=ReplyKeyboardRemove()) self.bot.sendMessage(self.chat_id, "輸入完可刪除訊息以策安全!\n"
"請輸入您的密碼:", reply_markup=ReplyKeyboardRemove())
def press_oldpassword(self): def press_oldpassword(self):
self.status = '舊的密碼' self.status = '舊的密碼'
self.question = '題外' self.question = '題外'
self.bot.sendMessage(self.chat_id, "請輸入要原本的舊密碼:", self.bot.sendMessage(self.chat_id, "請輸入要原本的舊密碼:",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text="主畫面🏠")] ["主畫面🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
def press_newpassword(self, text): def press_newpassword(self, text):
@ -53,13 +54,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=[
[KeyboardButton(text="主畫面🏠")] ["主畫面🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
def login_kcoj(self, text): def login_kcoj(self, text):
@ -82,7 +83,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=[
[KeyboardButton(text="主畫面🏠")] ["主畫面🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
return False return False
elif self.api.check_online() == True: elif self.api.check_online() == True:
@ -117,9 +118,9 @@ class kuser:
"你今天寫扣了嗎?", "你今天寫扣了嗎?",
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text="查看題庫📝"), KeyboardButton(text="重新整理🔃")], ["題庫📝"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], resize_keyboard=True))
def display_questions(self): def display_questions(self):
self.status = '正常使用' self.status = '正常使用'
@ -130,13 +131,15 @@ class kuser:
q_str += "📗" if q_dict[key][1] == '期限未到' else "📕" q_str += "📗" if q_dict[key][1] == '期限未到' else "📕"
q_str += "<b>" + key + "</b> (到 " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]] /question_" + key + "\n\n" q_str += "<b>" + key + "</b> (到 " + q_dict[key][0] + ")\n [[" + q_dict[key][2] + "]] /question_" + key + "\n\n"
self.bot.sendMessage(self.chat_id, "💁 <b>" + self.username + "</b> /logout\n" self.bot.sendMessage(self.chat_id, "💁 <b>" + self.username + "</b> /logout\n"
"\n📝<i>所有作業</i>\n\n" + q_str + "\n" "\n"
"📝<i>所有作業</i>\n\n" + q_str + \
"\n"
"你今天寫扣了嗎?", "你今天寫扣了嗎?",
parse_mode='HTML', parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="重新載入🔃")], ["主畫面🏠", "更新🔃"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], resize_keyboard=True))
def display_question(self, number): def display_question(self, number):
self.status = '查看題目' self.status = '查看題目'
@ -145,18 +148,13 @@ class kuser:
q = self.api.list_questions()[number] q = self.api.list_questions()[number]
q_str = "📗" if q[1] == '期限未到' else "📕" q_str = "📗" if q[1] == '期限未到' else "📕"
q_str += "**" + number + "** (到 " + q[0] + ")\n [[" + q[2] + "]]\n" q_str += "**" + number + "** (到 " + q[0] + ")\n [[" + q[2] + "]]\n"
k = [[KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="回到題庫📝")]]
k2 = []
if q[1] == '期限未到':
k2 += [KeyboardButton(text="上傳答案📮")]
if q[2] == '已繳':
k2 += [KeyboardButton(text="查看結果☑️")]
k2 += [KeyboardButton(text="通過名單🌐")]
k += [k2]
k += [[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")]]
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=k)) reply_markup=ReplyKeyboardMarkup(keyboard=[
["主畫面🏠", "回題庫📝"],
["交作業📮" if q[1] == '期限未到' else '', "看結果☑️" if q[2] == '已繳' else '', "通過者🌐"],
["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True))
def help_you(self): def help_you(self):
self.status = '正常使用' self.status = '正常使用'
@ -175,13 +173,14 @@ class kuser:
"7. 我以我的人格擔保,不會使用您提供的資訊侵害您的權利\n\n" "7. 我以我的人格擔保,不會使用您提供的資訊侵害您的權利\n\n"
"然後,附上厲害的 [郭老 Online Judge 傳送門](" + config.URL + ")", parse_mode='Markdown', "然後,附上厲害的 [郭老 Online Judge 傳送門](" + config.URL + ")", parse_mode='Markdown',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text="主畫面🏠")], ["主畫面🏠"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], resize_keyboard=True))
self.bot.sendMessage(self.chat_id, "專案授權方式採用 GPLv3\n" self.bot.sendMessage(self.chat_id, "專案授權方式採用 GPLv3\n"
"非常歡迎發 issue 送 PR owooo\n" "非常歡迎發 issue 送 PR owooo\n"
"原始碼被託管於 GitHub大大們有空的話可以按個星星支持一下> <網址如下:\n" "原始碼被託管於 GitHub大大們有空的話可以按個星星支持一下> <網址如下:\n"
"https://github.com/PinLin/kcoj_bot") "https://github.com/PinLin/kcoj_bot")
def upload_answer(self): def upload_answer(self):
self.status = '上傳答案' self.status = '上傳答案'
q = self.api.list_questions()[self.question] q = self.api.list_questions()[self.question]
@ -190,28 +189,27 @@ class kuser:
self.bot.sendMessage(self.chat_id, q_str + "\n現在請把你的程式碼讓我看看(請別超過 20 MB\n" self.bot.sendMessage(self.chat_id, q_str + "\n現在請把你的程式碼讓我看看(請別超過 20 MB\n"
"可以使用「文字訊息」或是「傳送檔案」的方式", parse_mode='HTML', "可以使用「文字訊息」或是「傳送檔案」的方式", parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text="刪除作業⚔️")], ["刪除作業⚔️"] if self.api.list_questions()[self.question][2] == '已繳' else [],
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="題目📜")]] if self.api.list_questions()[self.question][2] == '已繳' else [ ["主畫面🏠", "回題目📜"]
[KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="回到題目📜")]
], 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
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.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=[
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="題目📜")], ["主畫面🏠", "回題目📜"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], resize_keyboard=True))
return return
else: else:
f = open(self.username + self.question + '.c', 'w') f = open(self.username + self.question + '.c', 'w')
@ -223,24 +221,26 @@ 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=[
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="題目📜")], ["主畫面🏠", "回題目📜"],
[KeyboardButton(text="看結果☑️")], ["看結果☑️"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], 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=[
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="題目📜")], ["主畫面🏠", "回題目📜"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], resize_keyboard=True))
os.remove(self.username + self.question + '.c') os.remove(self.username + self.question + '.c')
def fail_send(self): def fail_send(self):
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=[
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="題目📜")] ["主畫面🏠", "回題目📜"],
])) ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True))
def list_passers(self): def list_passers(self):
self.status = '正常使用' self.status = '正常使用'
q = self.api.list_questions()[self.question] q = self.api.list_questions()[self.question]
@ -248,11 +248,12 @@ class kuser:
q_str += "<b>" + self.question + "</b> (到 " + q[0] + ")\n [[" + q[2] + "]]\n<code>" q_str += "<b>" + self.question + "</b> (到 " + q[0] + ")\n [[" + q[2] + "]]\n<code>"
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
self.bot.sendMessage(self.chat_id, q_str + "</code>", parse_mode='HTML', self.bot.sendMessage(self.chat_id, q_str + "</code>",
parse_mode='HTML',
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
[KeyboardButton(text="主畫面🏠"), KeyboardButton(text="題目📜")], ["主畫面🏠", "回題目📜"],
[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="改密碼💱"), KeyboardButton(text="提供幫助📚")] ["登出🚪", "改密碼💱", "幫助📚"]
])) ], resize_keyboard=True))
def list_results(self): def list_results(self):
self.status = '正常使用' self.status = '正常使用'
@ -263,11 +264,10 @@ 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]
k = [[KeyboardButton(text="回主畫面🏠"), KeyboardButton(text="回到題目📜")]] self.bot.sendMessage(self.chat_id, q_str,
k2 = [] parse_mode='HTML',
if q[1] == '期限未到': reply_markup=ReplyKeyboardMarkup(keyboard=[
k2 += [KeyboardButton(text="上傳答案📮")] ["主畫面🏠", "回題目📜"],
k2 += [KeyboardButton(text="通過名單🌐")] ["交作業📮" if q[1] == '期限未到' else '', "通過🌐"],
k += [k2] ["登出🚪", "改密碼💱", "幫助📚"]
k += [[KeyboardButton(text="登出帳號🚪"), KeyboardButton(text="修改密碼💱"), KeyboardButton(text="提供幫助📚")]] ], resize_keyboard=True))
self.bot.sendMessage(self.chat_id, q_str, parse_mode='HTML', reply_markup=ReplyKeyboardMarkup(keyboard=k))