edit some format string 2
This commit is contained in:
120
bot.py
120
bot.py
@ -260,6 +260,7 @@ class Kuser:
|
|||||||
# 顯示點我到頂的訊息
|
# 顯示點我到頂的訊息
|
||||||
bot.sendMessage(chat_id, "點我到題庫頂", reply_to_message_id=msg['message_id'])
|
bot.sendMessage(chat_id, "點我到題庫頂", reply_to_message_id=msg['message_id'])
|
||||||
|
|
||||||
|
# 顯示題目內容
|
||||||
def show_question(self, number, chat_id):
|
def show_question(self, number, chat_id):
|
||||||
self.question = number
|
self.question = number
|
||||||
self.status = '查看題目'
|
self.status = '查看題目'
|
||||||
@ -309,6 +310,7 @@ class Kuser:
|
|||||||
bot.sendMessage(chat_id, "點我到題目頂", reply_to_message_id=msg['message_id'])
|
bot.sendMessage(chat_id, "點我到題目頂", reply_to_message_id=msg['message_id'])
|
||||||
|
|
||||||
def help(self):
|
def help(self):
|
||||||
|
# 印出幫助(?)和關於訊息
|
||||||
bot.sendMessage(self.userid,
|
bot.sendMessage(self.userid,
|
||||||
"這裡是 Kuo C Online Judge Bot!\n"
|
"這裡是 Kuo C Online Judge Bot!\n"
|
||||||
"可以簡稱 KCOJ Bot,目前定居於 [{BOT_NAME}]\n"
|
"可以簡稱 KCOJ Bot,目前定居於 [{BOT_NAME}]\n"
|
||||||
@ -331,94 +333,140 @@ class Kuser:
|
|||||||
parse_mode='Markdown'
|
parse_mode='Markdown'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 使用者選擇程式碼來上傳
|
||||||
def upload_answer(self):
|
def upload_answer(self):
|
||||||
self.status = '上傳答案'
|
self.status = '上傳答案'
|
||||||
q = self.api.list_questions()[self.question]
|
# 題目資訊字典
|
||||||
q_str = "💁 <b>" + self.username + "</b> " + NAME + "\n"
|
q_info = self.api.list_questions()[self.question]
|
||||||
q_str += "➖➖➖➖➖\n"
|
bot.sendMessage(self.userid,
|
||||||
q_str += "📗" if q[1] == '期限未到' else "📕"
|
"💁 <b>{NAME}</b> {BOT_NAME}\n"
|
||||||
q_str += "<b>" + self.question + "</b> (DL: " + q[0] + ")\n [[" + q[3] + "]] [[" + q[2] + "]]"
|
"➖➖➖➖➖\n"
|
||||||
q_str += "⚠️" if q[2] == '未繳' else "✅"
|
"{DL_ICON}<b>{NUM}</b> (DL: {DL})\n"
|
||||||
bot.sendMessage(self.userid,
|
" [[{LANG}]] [[{STATUS}]]{STAT_ICON}\n"
|
||||||
q_str + "\n\n現在請把你的程式碼讓我看看(請別超過 20 MB)\n"
|
"\n"
|
||||||
|
"現在請把你的程式碼讓我看看(請別超過 20 MB)\n"
|
||||||
"可以使用「文字訊息」或是「傳送檔案」的方式\n"
|
"可以使用「文字訊息」或是「傳送檔案」的方式\n"
|
||||||
"(注意:可在程式碼前後加上單獨成行的 ``` 避免可能的錯誤。)", parse_mode='HTML',
|
"(注意:可在程式碼前後加上單獨成行的 ``` 避免可能的錯誤。)".format(
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
NAME=self.username,
|
||||||
["刪除作業⚔️"] if self.api.list_questions()[self.question][2] == '已繳' else [],
|
BOT_NAME=NAME,
|
||||||
["首頁🏠", "回題目📜"]
|
DL_ICON=("📗" if q_info[1] == '期限未到' else "📕"),
|
||||||
], resize_keyboard=True))
|
NUM=self.question,
|
||||||
|
DL=q_info[0],
|
||||||
|
LANG=q_info[3],
|
||||||
|
STATUS=q_info[2],
|
||||||
|
STAT_ICON=("⚠️" if q_info[2] == '未繳' else "✅")
|
||||||
|
),
|
||||||
|
parse_mode='HTML',
|
||||||
|
reply_markup=
|
||||||
|
ReplyKeyboardMarkup(keyboard=[
|
||||||
|
["刪除作業⚔️"] if self.api.list_questions()[self.question][2] == '已繳' else [],
|
||||||
|
["首頁🏠", "回題目📜"]
|
||||||
|
], resize_keyboard=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 上傳程式碼
|
||||||
def send_answer(self, text, file_id):
|
def send_answer(self, text, file_id):
|
||||||
self.status = '正常使用'
|
self.status = '正常使用'
|
||||||
# define filename
|
# 定義檔名
|
||||||
filename = sys.path[0] + '/' + self.username + self.question
|
filename = sys.path[0] + '/' + self.username + self.question
|
||||||
if self.api.list_questions()[self.question][3] == 'Python':
|
if self.api.list_questions()[self.question][3] == 'Python':
|
||||||
filename += '.py'
|
filename += '.py'
|
||||||
else:
|
else:
|
||||||
filename += '.c'
|
filename += '.c'
|
||||||
|
# 判斷使用者要用什麼方式傳程式碼
|
||||||
if text != '':
|
if text != '':
|
||||||
|
# 傳送文字
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
else:
|
else:
|
||||||
|
# 傳送檔案
|
||||||
bot.download_file(file_id, filename)
|
bot.download_file(file_id, filename)
|
||||||
|
# 先把原本的答案刪掉
|
||||||
self.api.delete_answer(self.question)
|
self.api.delete_answer(self.question)
|
||||||
if self.api.upload_answer(self.question, filename) == True:
|
# 上傳並判斷是否成功
|
||||||
|
if self.api.upload_answer(self.question, filename):
|
||||||
|
# 上傳成功
|
||||||
bot.sendMessage(self.userid, "上傳成功",
|
bot.sendMessage(self.userid, "上傳成功",
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
||||||
["首頁🏠", "回題目📜"],
|
["首頁🏠", "回題目📜"],
|
||||||
["看結果☑️"],
|
["看結果☑️"],
|
||||||
["登出🚪", "改密碼💱", "幫助📚"]
|
["登出🚪", "改密碼💱", "幫助📚"]
|
||||||
], resize_keyboard=True))
|
], resize_keyboard=True)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
|
# 上傳失敗
|
||||||
bot.sendMessage(self.userid, "上傳失敗",
|
bot.sendMessage(self.userid, "上傳失敗",
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
||||||
["首頁🏠", "回題目📜"],
|
["首頁🏠", "回題目📜"],
|
||||||
["登出🚪", "改密碼💱", "幫助📚"]
|
["登出🚪", "改密碼💱", "幫助📚"]
|
||||||
], resize_keyboard=True))
|
], resize_keyboard=True)
|
||||||
|
)
|
||||||
|
# 移除上傳的檔案
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
|
||||||
|
# 刪除之前繳交的程式碼
|
||||||
def delete_answer(self):
|
def delete_answer(self):
|
||||||
bot.sendMessage(self.userid, "移除成功" if self.api.delete_answer(self.question) == True else "移除失敗",
|
bot.sendMessage(self.userid, "移除成功" if self.api.delete_answer(self.question) else "移除失敗",
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
||||||
["首頁🏠", "回題目📜"],
|
["首頁🏠", "回題目📜"],
|
||||||
["登出🚪", "改密碼💱", "幫助📚"]
|
["登出🚪", "改密碼💱", "幫助📚"]
|
||||||
], resize_keyboard=True))
|
], resize_keyboard=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 上傳失敗(預設立場是檔案太大)
|
||||||
def send_failed(self):
|
def send_failed(self):
|
||||||
self.status = '正常使用'
|
self.status = '正常使用'
|
||||||
bot.sendMessage(self.userid, "檔案不能超過 20 MB!上傳失敗",
|
bot.sendMessage(self.userid, "檔案不能超過 20 MB!上傳失敗",
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
||||||
["首頁🏠", "回題目📜"],
|
["首頁🏠", "回題目📜"],
|
||||||
["登出🚪", "改密碼💱", "幫助📚"]
|
["登出🚪", "改密碼💱", "幫助📚"]
|
||||||
], resize_keyboard=True))
|
], resize_keyboard=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
# 列出題目中已通過者的名單
|
||||||
def list_passers(self):
|
def list_passers(self):
|
||||||
self.status = '正常使用'
|
self.status = '正常使用'
|
||||||
q = self.api.list_questions()[self.question]
|
# 題目資訊字典
|
||||||
q_str = "💁 <b>" + self.username + "</b> " + NAME + "\n"
|
q_info = self.api.list_questions()[self.question]
|
||||||
q_str += "➖➖➖➖➖\n"
|
q_str = (
|
||||||
q_str += "📗" if q[1] == '期限未到' else "📕"
|
"💁 <b>{NAME}</b> {BOT_NAME}\n"
|
||||||
q_str += "<b>" + self.question + "</b> (DL: " + q[0] + ")\n [[" + q[3] + "]] [[" + q[2] + "]]"
|
"➖➖➖➖➖\n"
|
||||||
q_str += "⚠️" if q[2] == '未繳' else "✅"
|
"{DL_ICON}<b>{NUM}</b> (DL: {DL})\n"
|
||||||
q_str += "<code>\n"
|
" [[{LANG}]] [[{STATUS}]]{STAT_ICON}\n"
|
||||||
|
"\n".format(
|
||||||
|
NAME=self.username,
|
||||||
|
BOT_NAME=NAME,
|
||||||
|
DL_ICON=("📗" if q_info[1] == '期限未到' else "📕"),
|
||||||
|
NUM=self.question,
|
||||||
|
DL=q_info[0],
|
||||||
|
LANG=q_info[3],
|
||||||
|
STATUS=q_info[2],
|
||||||
|
STAT_ICON=("⚠️" if q_info[2] == '未繳' else "✅")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# 列出已通過者名單
|
||||||
|
q_str += "<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 += passer + "\n"
|
||||||
reply = bot.sendMessage(self.userid, q_str + "</code>",
|
q_str += "</code>"
|
||||||
|
# 顯示題目內容並將訊息存起來
|
||||||
|
msg = bot.sendMessage(self.userid, q_str,
|
||||||
parse_mode='HTML',
|
parse_mode='HTML',
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
||||||
["首頁🏠", "回題目📜"],
|
["首頁🏠", "回題目📜"],
|
||||||
["登出🚪", "改密碼💱", "幫助📚"]
|
["登出🚪", "改密碼💱", "幫助📚"]
|
||||||
], resize_keyboard=True))
|
], resize_keyboard=True)
|
||||||
bot.sendMessage(self.userid, "點我到名單頂", reply_to_message_id=reply['message_id'])
|
)
|
||||||
|
# 顯示點我到頂的訊息
|
||||||
|
bot.sendMessage(self.userid, "點我到名單頂", reply_to_message_id=msg['message_id'])
|
||||||
|
|
||||||
def list_results(self):
|
def list_results(self):
|
||||||
self.status = '正常使用'
|
self.status = '正常使用'
|
||||||
q = self.api.list_questions()[self.question]
|
q_info = self.api.list_questions()[self.question]
|
||||||
q_str = "💁 <b>" + self.username + "</b> " + NAME + "\n"
|
q_str = "💁 <b>" + self.username + "</b> " + NAME + "\n"
|
||||||
q_str += "➖➖➖➖➖\n"
|
q_str += "➖➖➖➖➖\n"
|
||||||
q_str += "📗" if q[1] == '期限未到' else "📕"
|
q_str += "📗" if q_info[1] == '期限未到' else "📕"
|
||||||
q_str += "<b>" + self.question + "</b> (DL: " + q[0] + ")\n"
|
q_str += "<b>" + self.question + "</b> (DL: " + q_info[0] + ")\n"
|
||||||
for result in self.api.list_results(self.question, self.username):
|
for result in self.api.list_results(self.question, self.username):
|
||||||
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 "❌ "
|
||||||
@ -427,7 +475,7 @@ class Kuser:
|
|||||||
parse_mode='HTML',
|
parse_mode='HTML',
|
||||||
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
reply_markup=ReplyKeyboardMarkup(keyboard=[
|
||||||
["首頁🏠", "回題目📜"],
|
["首頁🏠", "回題目📜"],
|
||||||
["交作業📮" if q[1] == '期限未到' else '', "通過者🌐"],
|
["交作業📮" if q_info[1] == '期限未到' else '', "通過者🌐"],
|
||||||
["登出🚪", "改密碼💱", "幫助📚"]
|
["登出🚪", "改密碼💱", "幫助📚"]
|
||||||
], resize_keyboard=True))
|
], resize_keyboard=True))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user