update commands

This commit is contained in:
2017-08-24 17:01:08 +08:00
parent 2c541af2f9
commit 2e7b1c3d21
2 changed files with 81 additions and 66 deletions

67
bot.py
View File

@ -16,12 +16,12 @@ bot = telepot.Bot(config.TOKEN)
def on_chat(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
from_id = msg['from']['id']
pprint(msg) # for debug
print('content_type:', content_type) # for debug
print('chat_type:', chat_type) # for debug
print('chat_id:', chat_id) # for debug
print('from_id:', from_id) # for debug
print() # for debug
pprint(msg)
print('content_type:', content_type)
print('chat_type:', chat_type)
print('chat_id:', chat_id)
print('from_id:', from_id)
print()
# create a user object
user = kuser(from_id)
@ -57,18 +57,6 @@ def on_chat(msg):
else:
bot.sendMessage(chat_id, "請先私訊我登入 kCOJ", reply_to_message_id=msg['message_id'])
elif user.status == '舊的密碼' and chat_type == 'private':
if user.check_online() == True:
user.press_newpassword(msg['text'])
elif user.status == '修改密碼' and chat_type == 'private':
if user.check_online() == True:
user.change_password(msg['text'])
elif user.status == '上傳答案' and chat_type == 'private':
if user.check_online() == True:
user.send_answer(msg['text'], '')
elif command[0] == '/start' or command[0] == '首頁🏠':
if user.check_online() == True:
user.display_main(chat_id)
@ -87,31 +75,50 @@ def on_chat(msg):
time.sleep(1)
os._exit(0)
elif (command[0] == '/help' or command[0] == '幫助📚') and chat_type == 'private':
elif chat_type == 'private':
if command[0] == '/help' or command[0] == '幫助📚':
if user.check_online() == True:
user.help_you()
elif (command[0] == '/password' or command[0] == '改密碼💱') and chat_type == 'private':
elif command[0] == '/password' or command[0] == '改密碼💱':
if user.check_online() == True:
user.press_oldpassword()
elif (command[0] == '/logout' or command[0] == '登出🚪') and chat_type == 'private':
elif command[0] == '/logout' or command[0] == '登出🚪':
user = kuser(from_id)
users[str(from_id)] = user
user.logout_system()
elif user.question != '題外':
elif command[0] == '回題目📜' and user.question != '題外':
if user.check_online() == True:
if (command[0] == '/upload' or command[0] == '交作業📮') and chat_type == 'private':
user.upload_answer()
elif (command[0] == '/result' or command[0] == '看結果☑️') and chat_type == 'private':
user.list_results()
elif (command[0] == '/passer' or command[0] == '通過者🌐') and chat_type == 'private':
user.list_passers()
elif command[0] == '回題目📜':
user.display_question(chat_id ,user.question)
elif (command[0] == '/upload' or command[0] == '交作業📮') and user.question != '題外':
if user.check_online() == True:
user.upload_answer()
elif (command[0] == '/result' or command[0] == '看結果☑️') and user.question != '題外':
if user.check_online() == True:
user.list_results()
elif (command[0] == '/passer' or command[0] == '通過者🌐') and user.question != '題外':
if user.check_online() == True:
user.list_passers()
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'])
elif user.status == '上傳答案':
if user.check_online() == True:
user.send_answer(msg['text'], '')
else:
if chat_type == 'private':
if user.check_online() == True:
bot.sendMessage(chat_id, "(ˊ・ω・ˋ)")
elif content_type == 'document':

View File

@ -45,22 +45,27 @@ class kuser:
], resize_keyboard=True))
def press_newpassword(self, text):
if text != self.password:
self.status = '正常使用'
self.question = '題外'
bot.sendMessage(self.userid, "密碼錯誤!",
reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠"]
], resize_keyboard=True))
else:
self.status = '修改密碼'
self.question = '題外'
if text != self.password:
self.display_main(self.userid)
else:
bot.sendMessage(self.userid, "使用此功能請務必小心!\n"
"請輸入要設定的新密碼:", reply_markup=ReplyKeyboardRemove())
"請輸入要設定的新密碼:",
reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠"]
], resize_keyboard=True))
def change_password(self, text):
self.status = '正常使用'
self.question = '題外'
if text == "首頁🏠":
self.display_main(self.userid)
else:
self.password = text
bot.sendMessage(self.userid, "修改成功" 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=[
["首頁🏠"]
], resize_keyboard=True))
@ -79,25 +84,28 @@ class kuser:
bot.sendMessage(self.userid, "哇...登入失敗,讓我們重新開始一次", reply_markup=ReplyKeyboardRemove())
self.press_username()
def check_online(self): # edit
self.status = '正常使用'
def check_online(self):
result = self.api.check_online()
if result == None:
self.fail_connecting()
return False
else:
if result == False:
self.api.login_kcoj(self.username, self.password)
result = self.api.check_online()
if result == False:
self.fail_login()
elif result == None:
self.fail_connecting()
return result == True
def fail_connecting(self):
self.status = '正常使用'
self.question = '題外'
bot.sendMessage(self.userid, "郭老 Online Judge 離線中!",
reply_markup=ReplyKeyboardMarkup(keyboard=[
["首頁🏠"]
], resize_keyboard=True))
return False
elif result == True:
return True
else:
self.api.login_kcoj(self.username, self.password)
if self.api.check_online() == False:
self.fail_login()
return False
else:
return True
def logout_system(self):
self.status = '正常使用'