big architecture changing

This commit is contained in:
2017-11-30 01:08:19 +08:00
parent aa12d8d224
commit 2a3ba9280a
2 changed files with 51 additions and 54 deletions

36
bot.py
View File

@ -1,19 +1,17 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# necessary modules # modules
import os import os
import time import time
import json import json
import requests from pprint import pprint
import telepot import telepot
from telepot.loop import MessageLoop from telepot.loop import MessageLoop
from pprint import pprint # config
# kCOJ API from config import NAME, TOKEN, ADMIN
import access from interface import Kuser
from interface import Kuser, bot
# configurations
import config
bot = telepot.Bot(TOKEN)
users = {} users = {}
def on_chat(msg): def on_chat(msg):
@ -42,10 +40,10 @@ def on_chat(msg):
# pre-treat the command # pre-treat the command
command = [msg['text']] command = [msg['text']]
if msg['text'].startswith('/'): if msg['text'].startswith('/'):
command = msg['text'].replace(config.NAME, '').replace('_', ' ').lower().split(' ') command = msg['text'].replace(NAME, '').replace('_', ' ').lower().split(' ')
# restart this bot # restart this bot
if command[0] == '/restart' and str(from_id) in config.ADMIN: if command[0] == '/restart' and str(from_id) in ADMIN:
bot.sendMessage(chat_id, "即將更新並重新啟動") bot.sendMessage(chat_id, "即將更新並重新啟動")
print("Restarting...") print("Restarting...")
backup_db() backup_db()
@ -61,7 +59,7 @@ def on_chat(msg):
# help message # help message
elif command[0] == '/help' or command[0] == '幫助📚': elif command[0] == '/help' or command[0] == '幫助📚':
if chat_type == 'private': if chat_type == 'private':
user.help_you() user.help()
# first-time user # first-time user
elif user._status == '第一次用': elif user._status == '第一次用':
@ -76,19 +74,19 @@ def on_chat(msg):
# login # login
elif user._status == '輸入密碼': elif user._status == '輸入密碼':
if chat_type == 'private': if chat_type == 'private':
user.login_kcoj(msg['text']) user.login(msg['text'])
# homepage # homepage
elif command[0] == '/start' or command[0] == '首頁🏠': elif command[0] == '/start' or command[0] == '首頁🏠':
if user.check_online(chat_id, msg['message_id']) == True: if user.check_online(chat_id, msg['message_id']) == True:
user.display_main(chat_id) user.show_homepage(chat_id)
elif command[0] == '/question' or command[0] == '題庫📝' or command[0] == '更新🔃': elif command[0] == '/question' or command[0] == '題庫📝' or command[0] == '更新🔃':
if user.check_online(chat_id, msg['message_id']) == True: if user.check_online(chat_id, msg['message_id']) == True:
if len(command) > 1: if len(command) > 1:
user.display_question(command[1], chat_id) user.show_question(command[1], chat_id)
else: else:
user.display_questions(chat_id) user.list_questions(chat_id)
elif chat_type == 'private': elif chat_type == 'private':
if command[0] == '/password' or command[0] == '改密碼💱': if command[0] == '/password' or command[0] == '改密碼💱':
@ -98,7 +96,7 @@ def on_chat(msg):
elif command[0] == '/logout' or command[0] == '登出🚪': elif command[0] == '/logout' or command[0] == '登出🚪':
user = Kuser(from_id) user = Kuser(from_id)
users[str(from_id)] = user users[str(from_id)] = user
user.logout_system() user.logout()
elif (command[0] == '/delete' or command[0] == '刪除作業⚔️') and user._question != '題外': elif (command[0] == '/delete' or command[0] == '刪除作業⚔️') and user._question != '題外':
if user.check_online(chat_id, msg['message_id']) == True: if user.check_online(chat_id, msg['message_id']) == True:
@ -118,7 +116,7 @@ def on_chat(msg):
elif command[0] == '回題目📜' and user._question != '題外': elif command[0] == '回題目📜' and user._question != '題外':
if user.check_online(chat_id, msg['message_id']) == True: if user.check_online(chat_id, msg['message_id']) == True:
user.display_question(user._question, chat_id) user.show_question(user._question, chat_id)
elif user._status == '舊的密碼': elif user._status == '舊的密碼':
if user.check_online(chat_id, msg['message_id']) == True: if user.check_online(chat_id, msg['message_id']) == True:
@ -140,7 +138,7 @@ def on_chat(msg):
if user._status == '上傳答案' or user._status == '查看題目': if user._status == '上傳答案' or user._status == '查看題目':
if user.check_online(chat_id, msg['message_id']) == True: if user.check_online(chat_id, msg['message_id']) == True:
if msg['document']['file_size'] > 167770000: if msg['document']['file_size'] > 167770000:
user.fail_send() user.send_failed()
else: else:
user.send_answer('', msg['document']['file_id']) user.send_answer('', msg['document']['file_id'])
@ -175,7 +173,7 @@ print("Started! Service is available.")
while True: while True:
time.sleep(60) time.sleep(60)
# keep alive # keep bot alive
bot.getMe() bot.getMe()
# backup # backup

View File

@ -1,18 +1,17 @@
#! /usr/bin/env python3 #! /usr/bin/env python3
# necessary modules # modules
import os import os
from random import choice
import telepot import telepot
from telepot.namedtuple import ReplyKeyboardMarkup, ReplyKeyboardRemove from telepot.namedtuple import ReplyKeyboardMarkup, ReplyKeyboardRemove
from random import choice # config
# kCOJ API from config import NAME, URL, TOKEN
import access from kcoj import KCOJ
# configurations
import config
import promote import promote
import external import external
bot = telepot.Bot(config.TOKEN) bot = telepot.Bot(TOKEN)
class Kuser: class Kuser:
def __init__(self, userid, username='', password='', status='第一次用', question='題外'): def __init__(self, userid, username='', password='', status='第一次用', question='題外'):
@ -21,10 +20,10 @@ class Kuser:
self._password = password self._password = password
self._status = status self._status = status
self._question = question self._question = question
self._api = access.KuserAPI() self._api = KCOJ(URL)
def new_user(self): def new_user(self):
self.help_you() self.help()
self.press_username() self.press_username()
def press_username(self): def press_username(self):
@ -73,29 +72,29 @@ class Kuser:
["首頁🏠"] ["首頁🏠"]
], resize_keyboard=True)) ], resize_keyboard=True))
def login_kcoj(self, text): def login(self, text):
self._status = '正常使用' self._status = '正常使用'
self._question = '題外' self._question = '題外'
self._password = text self._password = text
bot.sendMessage(self._userid, "登入中...", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self._userid, "登入中...", reply_markup=ReplyKeyboardRemove())
if self.check_online(self._userid) == True: if self.check_online(self._userid) == True:
self.display_main(self._userid) self.show_homepage(self._userid)
def fail_login(self, chat_id, message_id): def login_failed(self, chat_id, message_id):
self._status = '正常使用' self._status = '正常使用'
self._question = '題外' self._question = '題外'
if chat_id != self._userid: if chat_id != self._userid:
bot.sendMessage(chat_id, "登入失敗,請先私訊我重新登入 kCOJ", reply_to_message_id=message_id) bot.sendMessage(chat_id, "登入失敗,請先私訊我重新登入 KCOJ", reply_to_message_id=message_id)
bot.sendMessage(self._userid, "哇...登入失敗,讓我們重新開始", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self._userid, "哇...登入失敗,讓我們重新開始", reply_markup=ReplyKeyboardRemove())
self.press_username() self.press_username()
def fail_connecting(self, chat_id, message_id): def connect_failed(self, chat_id, message_id):
self._status = '正常使用' self._status = '正常使用'
self._question = '題外' self._question = '題外'
if chat_id != self._userid: if chat_id != self._userid:
bot.sendMessage(chat_id, "kCOJ 離線中!請稍後再試", reply_to_message_id=message_id) bot.sendMessage(chat_id, "KCOJ 離線中!請稍後再試", reply_to_message_id=message_id)
else: else:
bot.sendMessage(self._userid, "kCOJ 離線中!請稍後再試", bot.sendMessage(self._userid, "KCOJ 離線中!請稍後再試",
reply_markup=ReplyKeyboardMarkup(keyboard=[ reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠", "幫助📚"] ["首頁🏠", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
@ -103,25 +102,25 @@ class Kuser:
def check_online(self, chat_id, message_id=''): def check_online(self, chat_id, message_id=''):
result = self._api.check_online() result = self._api.check_online()
if result == None: if result == None:
self.fail_connecting(chat_id, message_id) self.connect_failed(chat_id, message_id)
return False return False
else: else:
if result == False: if result == False:
self._api.login_kcoj(self._username, self._password) self._api.login(self._username, self._password)
result = self._api.check_online() result = self._api.check_online()
if result == False: if result == False:
self.fail_login(chat_id, message_id) self.login_failed(chat_id, message_id)
elif result == None: elif result == None:
self.fail_connecting(chat_id, message_id) self.connect_failed(chat_id, message_id)
return result == True return result == True
def logout_system(self): def logout(self):
self._status = '正常使用' self._status = '正常使用'
self._question = '題外' self._question = '題外'
bot.sendMessage(self._userid, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove()) bot.sendMessage(self._userid, "您現在已經是登出的狀態。", reply_markup=ReplyKeyboardRemove())
self.press_username() self.press_username()
def display_main(self, chat_id): def show_homepage(self, chat_id):
self._status = '正常使用' self._status = '正常使用'
self._question = '題外' self._question = '題外'
q_dict = self._api.list_questions() q_dict = self._api.list_questions()
@ -131,7 +130,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"
bot.sendMessage(chat_id, "💁 <b>" + self._username + "</b> " + config.NAME + "\n" bot.sendMessage(chat_id, "💁 <b>" + self._username + "</b> " + 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,7 +141,7 @@ class Kuser:
], resize_keyboard=True) if chat_id == self._userid else ReplyKeyboardRemove(), ], resize_keyboard=True) if chat_id == self._userid else ReplyKeyboardRemove(),
disable_web_page_preview=True) disable_web_page_preview=True)
def display_questions(self, chat_id): def list_questions(self, chat_id):
self._status = '正常使用' self._status = '正常使用'
self._question = '題外' self._question = '題外'
q_dict = self._api.list_questions() q_dict = self._api.list_questions()
@ -152,7 +151,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 = bot.sendMessage(chat_id, "💁 <b>" + self._username + "</b> " + config.NAME + "\n" reply = bot.sendMessage(chat_id, "💁 <b>" + self._username + "</b> " + NAME + "\n"
"\n" "\n"
"📝<i>所有作業</i>\n\n" + q_str + \ "📝<i>所有作業</i>\n\n" + q_str + \
"\n" + choice(promote.sentences), "\n" + choice(promote.sentences),
@ -164,7 +163,7 @@ class Kuser:
disable_web_page_preview=True) disable_web_page_preview=True)
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, number, chat_id): def show_question(self, number, chat_id):
self._status = '查看題目' self._status = '查看題目'
self._question = number self._question = number
if number in external.QUESTION: if number in external.QUESTION:
@ -174,7 +173,7 @@ class Kuser:
ext_q = False ext_q = False
content = '```\n' + self._api.show_question(number) + '\n```' content = '```\n' + self._api.show_question(number) + '\n```'
q = self._api.list_questions()[number] q = self._api.list_questions()[number]
q_str = "💁 *" + self._username + "* [" + config.NAME + "]\n" q_str = "💁 *" + self._username + "* [" + NAME + "]\n"
q_str += "\n" q_str += "\n"
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] + "]]]"
@ -189,11 +188,11 @@ class Kuser:
if ext_q == False: if ext_q == False:
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(self):
bot.sendMessage(self._userid, "這裡是 kC Online Judge Bot\n" bot.sendMessage(self._userid, "這裡是 kC Online Judge Bot\n"
"可以簡稱 kCOJ Bot目前定居於 [" + config.NAME + "]\n" "可以簡稱 KCOJ Bot目前定居於 [" + NAME + "]\n"
"作用是讓大家可以方便的透過我使用郭老程設課的 Online Judge\n" "作用是讓大家可以方便的透過我使用郭老程設課的 Online Judge\n"
"➡️[傳送門](" + config.URL + ")\n" "➡️[OJ 傳送門](" + URL + ")\n"
"操作很簡單(?)\n\n" "操作很簡單(?)\n\n"
"還是稍微提幾個需要注意的地方:\n" "還是稍微提幾個需要注意的地方:\n"
"1. 📗代表還可以繳交的作業,📕代表已經不能繳交的作業\n" "1. 📗代表還可以繳交的作業,📕代表已經不能繳交的作業\n"
@ -213,7 +212,7 @@ class Kuser:
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]
q_str = "💁 <b>" + self._username + "</b> " + config.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[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] + "]]"
@ -255,7 +254,7 @@ class Kuser:
["登出🚪", "改密碼💱", "幫助📚"] ["登出🚪", "改密碼💱", "幫助📚"]
], resize_keyboard=True)) ], resize_keyboard=True))
def fail_send(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=[
@ -266,7 +265,7 @@ class Kuser:
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]
q_str = "💁 <b>" + self._username + "</b> " + config.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[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] + "]]"
@ -285,7 +284,7 @@ class Kuser:
def list_results(self): def list_results(self):
self._status = '正常使用' self._status = '正常使用'
q = self._api.list_questions()[self._question] q = self._api.list_questions()[self._question]
q_str = "💁 <b>" + self._username + "</b> " + config.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[1] == '期限未到' else "📕"
q_str += "<b>" + self._question + "</b> (DL: " + q[0] + ")\n" q_str += "<b>" + self._question + "</b> (DL: " + q[0] + ")\n"