2022-04-14 07:18:45 +00:00
|
|
|
|
import random
|
|
|
|
|
import re
|
|
|
|
|
import time
|
2022-05-19 09:33:35 +00:00
|
|
|
|
from typing import List, Optional
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
from numpy.random import MT19937, Generator
|
2022-04-20 12:31:15 +00:00
|
|
|
|
from redis import DataError, ResponseError
|
2022-05-19 09:33:35 +00:00
|
|
|
|
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup, Poll, \
|
|
|
|
|
ReplyKeyboardRemove, Message
|
2022-05-30 08:08:56 +00:00
|
|
|
|
from telegram.constants import ChatAction
|
2022-06-09 06:00:46 +00:00
|
|
|
|
from telegram.ext import CallbackContext, ConversationHandler, CommandHandler, MessageHandler, filters
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from telegram.helpers import escape_markdown
|
|
|
|
|
|
2022-04-20 12:31:15 +00:00
|
|
|
|
from logger import Log
|
2022-05-20 10:23:15 +00:00
|
|
|
|
from plugins.base import BasePlugins, RestrictsCalls
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from service import BaseService
|
|
|
|
|
from service.base import QuestionData, AnswerData
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class QuizCommandData:
|
|
|
|
|
question_id: int = -1
|
|
|
|
|
new_question: str = ""
|
|
|
|
|
new_correct_answer: str = ""
|
|
|
|
|
new_wrong_answer: List[str] = []
|
|
|
|
|
status: int = 0
|
|
|
|
|
|
|
|
|
|
|
2022-05-17 12:54:45 +00:00
|
|
|
|
class Quiz(BasePlugins):
|
2022-06-09 12:52:59 +00:00
|
|
|
|
"""
|
|
|
|
|
派蒙的十万个为什么
|
|
|
|
|
合并了香港问题修改/添加/删除
|
|
|
|
|
"""
|
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
CHECK_COMMAND, VIEW_COMMAND, CHECK_QUESTION, \
|
|
|
|
|
GET_NEW_QUESTION, GET_NEW_CORRECT_ANSWER, GET_NEW_WRONG_ANSWER, \
|
|
|
|
|
QUESTION_EDIT, SAVE_QUESTION = range(10300, 10308)
|
|
|
|
|
|
|
|
|
|
def __init__(self, service: BaseService):
|
2022-05-17 12:54:45 +00:00
|
|
|
|
super().__init__(service)
|
2022-05-20 06:33:35 +00:00
|
|
|
|
self.user_time = {}
|
2022-04-14 07:18:45 +00:00
|
|
|
|
self.send_time = time.time()
|
|
|
|
|
self.generator = Generator(MT19937(int(self.send_time)))
|
|
|
|
|
self.service = service
|
|
|
|
|
self.time_out = 120
|
2022-06-09 07:12:06 +00:00
|
|
|
|
|
2022-06-09 05:26:08 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def create_conversation_handler(service: BaseService):
|
|
|
|
|
quiz = Quiz(service)
|
|
|
|
|
quiz_handler = ConversationHandler(
|
|
|
|
|
entry_points=[CommandHandler('quiz', quiz.command_start, block=True)],
|
|
|
|
|
states={
|
|
|
|
|
quiz.CHECK_COMMAND: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
|
|
|
|
quiz.check_command, block=True)],
|
|
|
|
|
quiz.CHECK_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
2022-06-09 07:12:06 +00:00
|
|
|
|
quiz.check_question, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
quiz.GET_NEW_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
2022-06-09 07:12:06 +00:00
|
|
|
|
quiz.get_new_question, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
quiz.GET_NEW_CORRECT_ANSWER: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
2022-06-09 07:12:06 +00:00
|
|
|
|
quiz.get_new_correct_answer, block=True)],
|
2022-06-09 05:26:08 +00:00
|
|
|
|
quiz.GET_NEW_WRONG_ANSWER: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
2022-06-09 07:12:06 +00:00
|
|
|
|
quiz.get_new_wrong_answer, block=True),
|
2022-06-09 05:26:08 +00:00
|
|
|
|
CommandHandler("finish", quiz.finish_edit)],
|
|
|
|
|
quiz.SAVE_QUESTION: [MessageHandler(filters.TEXT & ~filters.COMMAND,
|
|
|
|
|
quiz.save_question, block=True)],
|
|
|
|
|
},
|
|
|
|
|
fallbacks=[CommandHandler('cancel', quiz.cancel, block=True)]
|
|
|
|
|
)
|
|
|
|
|
return quiz_handler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
def random(self, low: int, high: int) -> int:
|
|
|
|
|
if self.send_time + 24 * 60 * 60 >= time.time():
|
|
|
|
|
self.send_time = time.time()
|
|
|
|
|
self.generator = Generator(MT19937(int(self.send_time)))
|
|
|
|
|
return int(self.generator.uniform(low, high))
|
|
|
|
|
|
2022-05-19 09:33:35 +00:00
|
|
|
|
async def send_poll(self, update: Update) -> Optional[Message]:
|
|
|
|
|
chat = update.message.chat
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
question_id_list = await self.service.quiz_service.get_question_id_list()
|
|
|
|
|
if filters.ChatType.GROUPS.filter(update.message):
|
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] 在群 {chat.title}[{chat.id}] 发送挑战问题命令请求")
|
|
|
|
|
if len(question_id_list) == 0:
|
2022-05-28 07:27:22 +00:00
|
|
|
|
await update.message.reply_text("旅行者!!!派蒙的问题清单你还没给我!!快去私聊我给我问题!")
|
2022-05-19 09:33:35 +00:00
|
|
|
|
if len(question_id_list) == 0:
|
|
|
|
|
return None
|
|
|
|
|
index = self.random(0, len(question_id_list))
|
|
|
|
|
question = await self.service.quiz_service.get_question(question_id_list[index])
|
尝试修复奇怪的错误
很奇怪,先会这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 874, in __create_task_callback
raise exception
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 846, in __create_task_callback
return await coroutine
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_handler.py", line 137, in handle_update
return await self.callback(update, context)
File "/root/Projects/TGPaimonBot/plugins/base.py", line 66, in decorator
return await func(*args, **kwargs)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 95, in command_start
poll_message = await self.send_poll(update)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 66, in send_poll
index = options.index(correct_option)
ValueError: '' is not in list`
然后就会一直这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 929, in process_update
check = handler.check_update(update) # Should the handler handle this update?
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_conversationhandler.py", line 741, in check_update
for candidate in self.states.get(state, []):
TypeError: unhashable type: 'PendingState'`
但是对于上个错误一直触发我也没办法
2022-05-25 06:24:13 +00:00
|
|
|
|
_options = []
|
|
|
|
|
correct_option = None
|
2022-05-19 09:33:35 +00:00
|
|
|
|
for answer_id in question["answer_id"]:
|
|
|
|
|
answer = await self.service.quiz_service.get_answer(answer_id)
|
尝试修复奇怪的错误
很奇怪,先会这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 874, in __create_task_callback
raise exception
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 846, in __create_task_callback
return await coroutine
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_handler.py", line 137, in handle_update
return await self.callback(update, context)
File "/root/Projects/TGPaimonBot/plugins/base.py", line 66, in decorator
return await func(*args, **kwargs)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 95, in command_start
poll_message = await self.send_poll(update)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 66, in send_poll
index = options.index(correct_option)
ValueError: '' is not in list`
然后就会一直这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 929, in process_update
check = handler.check_update(update) # Should the handler handle this update?
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_conversationhandler.py", line 741, in check_update
for candidate in self.states.get(state, []):
TypeError: unhashable type: 'PendingState'`
但是对于上个错误一直触发我也没办法
2022-05-25 06:24:13 +00:00
|
|
|
|
_options.append(answer["answer"])
|
2022-05-19 09:33:35 +00:00
|
|
|
|
if answer["is_correct"] == 1:
|
|
|
|
|
correct_option = answer["answer"]
|
尝试修复奇怪的错误
很奇怪,先会这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 874, in __create_task_callback
raise exception
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 846, in __create_task_callback
return await coroutine
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_handler.py", line 137, in handle_update
return await self.callback(update, context)
File "/root/Projects/TGPaimonBot/plugins/base.py", line 66, in decorator
return await func(*args, **kwargs)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 95, in command_start
poll_message = await self.send_poll(update)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 66, in send_poll
index = options.index(correct_option)
ValueError: '' is not in list`
然后就会一直这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 929, in process_update
check = handler.check_update(update) # Should the handler handle this update?
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_conversationhandler.py", line 741, in check_update
for candidate in self.states.get(state, []):
TypeError: unhashable type: 'PendingState'`
但是对于上个错误一直触发我也没办法
2022-05-25 06:24:13 +00:00
|
|
|
|
if correct_option is None:
|
2022-05-25 09:04:14 +00:00
|
|
|
|
question_id = question["question_id"]
|
|
|
|
|
Log.warning(f"Quiz模块 correct_option 异常 question_id[{question_id}] ")
|
尝试修复奇怪的错误
很奇怪,先会这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 874, in __create_task_callback
raise exception
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 846, in __create_task_callback
return await coroutine
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_handler.py", line 137, in handle_update
return await self.callback(update, context)
File "/root/Projects/TGPaimonBot/plugins/base.py", line 66, in decorator
return await func(*args, **kwargs)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 95, in command_start
poll_message = await self.send_poll(update)
File "/root/Projects/TGPaimonBot/plugins/quiz.py", line 66, in send_poll
index = options.index(correct_option)
ValueError: '' is not in list`
然后就会一直这样
`Traceback (most recent call last):
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_application.py", line 929, in process_update
check = handler.check_update(update) # Should the handler handle this update?
File "/home/luoshuijs/anaconda3/envs/TGPaimonBot/lib/python3.9/site-packages/python_telegram_bot-20.0a0-py3.9.egg/telegram/ext/_conversationhandler.py", line 741, in check_update
for candidate in self.states.get(state, []):
TypeError: unhashable type: 'PendingState'`
但是对于上个错误一直触发我也没办法
2022-05-25 06:24:13 +00:00
|
|
|
|
return None
|
|
|
|
|
random.shuffle(_options)
|
|
|
|
|
index = _options.index(correct_option)
|
|
|
|
|
return await update.effective_message.reply_poll(question["question"], _options,
|
2022-05-19 09:33:35 +00:00
|
|
|
|
correct_option_id=index, is_anonymous=False,
|
|
|
|
|
open_period=self.time_out, type=Poll.QUIZ)
|
|
|
|
|
|
2022-05-20 10:23:15 +00:00
|
|
|
|
@RestrictsCalls(return_data=ConversationHandler.END, try_delete_message=True)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
user = update.effective_user
|
2022-05-20 06:33:35 +00:00
|
|
|
|
message = update.message
|
|
|
|
|
if filters.ChatType.PRIVATE.filter(message):
|
2022-05-17 12:58:44 +00:00
|
|
|
|
Log.info(f"用户 {user.full_name}[{user.id}] quiz命令请求")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
admin_list = await self.service.admin.get_admin_list()
|
|
|
|
|
if user.id in admin_list:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
if quiz_command_data is None:
|
|
|
|
|
quiz_command_data = QuizCommandData()
|
|
|
|
|
context.chat_data["quiz_command_data"] = quiz_command_data
|
2022-05-20 06:33:35 +00:00
|
|
|
|
text = f'你好 {user.mention_markdown_v2()} {escape_markdown("!请选择你的操作!")}'
|
2022-04-14 07:18:45 +00:00
|
|
|
|
reply_keyboard = [
|
|
|
|
|
["查看问题", "添加问题"],
|
|
|
|
|
["重载问题"],
|
|
|
|
|
["退出"]
|
|
|
|
|
]
|
2022-05-20 06:33:35 +00:00
|
|
|
|
await message.reply_markdown_v2(text, reply_markup=ReplyKeyboardMarkup(reply_keyboard,
|
|
|
|
|
one_time_keyboard=True))
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return self.CHECK_COMMAND
|
2022-05-19 09:33:35 +00:00
|
|
|
|
else:
|
|
|
|
|
await self.send_poll(update)
|
|
|
|
|
elif filters.ChatType.GROUPS.filter(update.message):
|
2022-05-30 08:08:56 +00:00
|
|
|
|
await update.message.reply_chat_action(ChatAction.TYPING)
|
2022-05-19 09:33:35 +00:00
|
|
|
|
poll_message = await self.send_poll(update)
|
|
|
|
|
if poll_message is None:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-05-17 12:55:40 +00:00
|
|
|
|
self._add_delete_message_job(context, update.message.chat_id, update.message.message_id, 300)
|
|
|
|
|
self._add_delete_message_job(context, poll_message.chat_id, poll_message.message_id, 300)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
2022-06-09 12:35:07 +00:00
|
|
|
|
async def view_command(self, update: Update, _: CallbackContext) -> int:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
keyboard = [
|
|
|
|
|
[
|
2022-05-20 06:33:35 +00:00
|
|
|
|
InlineKeyboardButton(text="选择问题", switch_inline_query_current_chat="查看问题 ")
|
2022-04-14 07:18:45 +00:00
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
await update.message.reply_text("请回复你要查看的问题",
|
|
|
|
|
reply_markup=InlineKeyboardMarkup(keyboard))
|
|
|
|
|
return self.CHECK_COMMAND
|
|
|
|
|
|
2022-06-09 12:35:07 +00:00
|
|
|
|
async def check_question(self, update: Update, _: CallbackContext) -> int:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
reply_keyboard = [
|
|
|
|
|
["删除问题"],
|
|
|
|
|
["退出"]
|
|
|
|
|
]
|
|
|
|
|
await update.message.reply_text("请选择你的操作", reply_markup=ReplyKeyboardMarkup(reply_keyboard))
|
|
|
|
|
return self.CHECK_COMMAND
|
|
|
|
|
|
|
|
|
|
async def check_command(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
if update.message.text == "退出":
|
|
|
|
|
await update.message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
elif update.message.text == "查看问题":
|
|
|
|
|
return await self.view_command(update, context)
|
|
|
|
|
elif update.message.text == "添加问题":
|
|
|
|
|
return await self.add_question(update, context)
|
|
|
|
|
elif update.message.text == "删除问题":
|
|
|
|
|
return await self.delete_question(update, context)
|
|
|
|
|
# elif update.message.text == "修改问题":
|
|
|
|
|
# return await self.edit_question(update, context)
|
|
|
|
|
elif update.message.text == "重载问题":
|
|
|
|
|
return await self.refresh_question(update, context)
|
|
|
|
|
else:
|
|
|
|
|
result = re.findall(r"问题ID (\d+)", update.message.text)
|
|
|
|
|
if len(result) == 1:
|
|
|
|
|
try:
|
|
|
|
|
question_id = int(result[0])
|
|
|
|
|
except ValueError:
|
|
|
|
|
await update.message.reply_text("获取问题ID失败")
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
quiz_command_data.question_id = question_id
|
|
|
|
|
await update.message.reply_text("获取问题ID成功")
|
|
|
|
|
return await self.check_question(update, context)
|
|
|
|
|
await update.message.reply_text("命令错误", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
2022-06-09 12:35:07 +00:00
|
|
|
|
async def refresh_question(self, update: Update, _: CallbackContext) -> int:
|
2022-04-14 07:18:45 +00:00
|
|
|
|
try:
|
|
|
|
|
await self.service.quiz_service.refresh_quiz()
|
|
|
|
|
except DataError:
|
|
|
|
|
await update.message.reply_text("Redis数据错误,重载失败", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-04-30 06:51:24 +00:00
|
|
|
|
except ResponseError as error:
|
|
|
|
|
Log.error("重载问题失败 /n", error)
|
|
|
|
|
await update.message.reply_text("重载问题失败,异常抛出Redis请求错误异常,详情错误请看日记",
|
|
|
|
|
reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_text("重载成功", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
|
async def add_question(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
quiz_command_data.new_wrong_answer = []
|
|
|
|
|
quiz_command_data.new_question = ""
|
|
|
|
|
quiz_command_data.new_correct_answer = ""
|
|
|
|
|
quiz_command_data.status = 1
|
2022-04-20 12:31:15 +00:00
|
|
|
|
await update.message.reply_text("请回复你要添加的问题,或发送 /cancel 取消操作", reply_markup=ReplyKeyboardRemove())
|
2022-04-14 07:18:45 +00:00
|
|
|
|
return self.GET_NEW_QUESTION
|
|
|
|
|
|
|
|
|
|
async def get_new_question(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
reply_text = f"问题:`{escape_markdown(update.message.text, version=2)}`\n" \
|
|
|
|
|
f"请填写正确答案:"
|
|
|
|
|
quiz_command_data.new_question = update.message.text
|
|
|
|
|
await update.message.reply_markdown_v2(reply_text)
|
|
|
|
|
return self.GET_NEW_CORRECT_ANSWER
|
|
|
|
|
|
|
|
|
|
async def get_new_correct_answer(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
reply_text = f"正确答案:`{escape_markdown(update.message.text, version=2)}`\n" \
|
|
|
|
|
f"请填写错误答案:"
|
|
|
|
|
await update.message.reply_markdown_v2(reply_text)
|
|
|
|
|
quiz_command_data.new_correct_answer = update.message.text
|
|
|
|
|
return self.GET_NEW_WRONG_ANSWER
|
|
|
|
|
|
|
|
|
|
async def get_new_wrong_answer(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
reply_text = f"错误答案:`{escape_markdown(update.message.text, version=2)}`\n" \
|
|
|
|
|
f"可继续填写,并使用 {escape_markdown('/finish', version=2)} 结束。"
|
|
|
|
|
await update.message.reply_markdown_v2(reply_text)
|
|
|
|
|
quiz_command_data.new_wrong_answer.append(update.message.text)
|
|
|
|
|
return self.GET_NEW_WRONG_ANSWER
|
|
|
|
|
|
|
|
|
|
async def finish_edit(self, update: Update, context: CallbackContext):
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
reply_text = f"问题:`{escape_markdown(quiz_command_data.new_question, version=2)}`\n" \
|
|
|
|
|
f"正确答案:`{escape_markdown(quiz_command_data.new_correct_answer, version=2)}`\n" \
|
|
|
|
|
f"错误答案:`{escape_markdown(' '.join(quiz_command_data.new_wrong_answer), version=2)}`"
|
|
|
|
|
await update.message.reply_markdown_v2(reply_text)
|
2022-04-20 12:38:34 +00:00
|
|
|
|
reply_keyboard = [["保存并重载配置", "抛弃修改并退出"]]
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_text("请核对问题,并选择下一步操作。", reply_markup=ReplyKeyboardMarkup(reply_keyboard))
|
|
|
|
|
return self.SAVE_QUESTION
|
|
|
|
|
|
|
|
|
|
async def save_question(self, update: Update, context: CallbackContext):
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
2022-04-20 07:52:15 +00:00
|
|
|
|
if update.message.text == "抛弃修改并退出":
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_text("退出任务", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
elif update.message.text == "保存并重载配置":
|
|
|
|
|
if quiz_command_data.status == 1:
|
|
|
|
|
answer = [
|
|
|
|
|
AnswerData(answer=wrong_answer, is_correct=False) for wrong_answer in
|
|
|
|
|
quiz_command_data.new_wrong_answer
|
|
|
|
|
]
|
|
|
|
|
answer.append(AnswerData(answer=quiz_command_data.new_correct_answer, is_correct=True))
|
|
|
|
|
await self.service.quiz_service.save_quiz(
|
|
|
|
|
QuestionData(question=quiz_command_data.new_question, answer=answer))
|
|
|
|
|
await update.message.reply_text("保存成功", reply_markup=ReplyKeyboardRemove())
|
2022-04-20 12:31:15 +00:00
|
|
|
|
try:
|
|
|
|
|
await self.service.quiz_service.refresh_quiz()
|
|
|
|
|
except ResponseError as error:
|
|
|
|
|
Log.error("重载问题失败 /n", error)
|
|
|
|
|
await update.message.reply_text("重载问题失败,异常抛出Redis请求错误异常,详情错误请看日记",
|
|
|
|
|
reply_markup=ReplyKeyboardRemove())
|
2022-04-20 12:38:34 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_text("重载配置成功", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
else:
|
|
|
|
|
await update.message.reply_text("回复错误,请重新选择")
|
|
|
|
|
return self.SAVE_QUESTION
|
|
|
|
|
|
|
|
|
|
async def edit_question(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
quiz_command_data.new_wrong_answer = []
|
|
|
|
|
quiz_command_data.new_question = ""
|
|
|
|
|
quiz_command_data.new_correct_answer = ""
|
|
|
|
|
quiz_command_data.status = 2
|
|
|
|
|
await update.message.reply_text("请回复你要修改的问题", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return self.GET_NEW_QUESTION
|
|
|
|
|
|
|
|
|
|
async def delete_question(self, update: Update, context: CallbackContext) -> int:
|
|
|
|
|
quiz_command_data: QuizCommandData = context.chat_data.get("quiz_command_data")
|
|
|
|
|
# 再问题重载Redis 以免redis数据为空时出现奔溃
|
2022-04-20 12:38:34 +00:00
|
|
|
|
try:
|
|
|
|
|
await self.service.quiz_service.refresh_quiz()
|
|
|
|
|
question = await self.service.quiz_service.get_question(quiz_command_data.question_id)
|
|
|
|
|
# 因为外键的存在,先删除答案
|
|
|
|
|
for answer_id in question["answer_id"]:
|
|
|
|
|
await self.service.repository.delete_answer(answer_id)
|
|
|
|
|
await self.service.repository.delete_question(question["question_id"])
|
|
|
|
|
await update.message.reply_text("删除问题成功", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
await self.service.quiz_service.refresh_quiz()
|
|
|
|
|
except ResponseError as error:
|
|
|
|
|
Log.error("重载问题失败 /n", error)
|
|
|
|
|
await update.message.reply_text("重载问题失败,异常抛出Redis请求错误异常,详情错误请看日记",
|
|
|
|
|
reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|
2022-04-14 07:18:45 +00:00
|
|
|
|
await update.message.reply_text("重载配置成功", reply_markup=ReplyKeyboardRemove())
|
|
|
|
|
return ConversationHandler.END
|