ДЗ quiz

main
Сергей Ванюшкин 2024-02-21 01:45:59 +03:00
parent 3f78a33b94
commit f33802a740
4 changed files with 137 additions and 1 deletions

16
poetry.lock generated
View File

@ -804,6 +804,20 @@ files = [
[package.dependencies] [package.dependencies]
typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0" typing-extensions = ">=4.6.0,<4.7.0 || >4.7.0"
[[package]]
name = "pyquizapi"
version = "0.0.14"
description = "API Client Wrapper for QuizAPI.io"
optional = false
python-versions = ">=3.8"
files = [
{file = "pyquizAPI-0.0.14-py3-none-any.whl", hash = "sha256:5d6530dd6df57dfd8f1d2b44f46989b34b39e5cbd210910d70ed2e697458e08d"},
{file = "pyquizAPI-0.0.14.tar.gz", hash = "sha256:d552a56d8d8cc1dd57257e4c75361d2f6ac2aa928f5f47f327cd4c6597bececb"},
]
[package.dependencies]
requests = "*"
[[package]] [[package]]
name = "pyyaml" name = "pyyaml"
version = "6.0.1" version = "6.0.1"
@ -1079,4 +1093,4 @@ multidict = ">=4.0"
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.10" python-versions = "^3.10"
content-hash = "b821fb28f2b00460dcf46192e544e0040c2833c82a9e5029ed131c2389894446" content-hash = "74b5d10c672b860dada4f42ba97679cbab3b3e9b29d92a9ff7fbed7f61bfd5c5"

View File

@ -12,6 +12,7 @@ aiogram = "^3.3.0"
requests = "^2.31.0" requests = "^2.31.0"
types-requests = "^2.31.0.20240125" types-requests = "^2.31.0.20240125"
mypy = "^1.8.0" mypy = "^1.8.0"
pyquizapi = "^0.0.14"
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]

View File

@ -10,6 +10,7 @@ from aiogram.types import Message
from aiogram.utils.markdown import hbold from aiogram.utils.markdown import hbold
from tg_bot.handlers import ( from tg_bot.handlers import (
games,
lesson_five, lesson_five,
lesson_four, lesson_four,
lesson_one, lesson_one,
@ -21,6 +22,7 @@ TOKEN: str = getenv('BOT_TOKEN') or 'Your TG_BOT token'
dp = Dispatcher() dp = Dispatcher()
dp.include_routers( dp.include_routers(
games.router,
lesson_five.router, lesson_five.router,
lesson_four.router, lesson_four.router,
lesson_tree.router, lesson_tree.router,

119
tg_bot/handlers/games.py Normal file
View File

@ -0,0 +1,119 @@
from os import getenv
from aiogram import Bot, F, Router
from aiogram.filters import Command
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import State, StatesGroup
from aiogram.types import (
CallbackQuery,
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
Message,
ReplyKeyboardMarkup,
ReplyKeyboardRemove,
)
from pyquizAPI import QuizClient
async def get_question():
api = getenv('QUIZ_API')
client = QuizClient(api)
client.make_config(limit=1, difficulty='Easy')
questions = client.get_questions(use_config=True)
return questions
router = Router()
kb = [
[KeyboardButton(text='Quiz')],
[KeyboardButton(text='Tic_tac_toy')],
[KeyboardButton(text='Quest')],
]
class GameState(StatesGroup):
game = State()
quiz = State()
tic_tac_toy = State()
quest = State()
markup_kb = ReplyKeyboardMarkup(keyboard=kb, resize_keyboard=True)
@router.message(Command('games'))
async def games(message: Message):
await message.answer('Select Game:', reply_markup=markup_kb)
@router.message(F.text.lower() == 'exit game')
async def exit_games(message: Message, state: FSMContext):
await state.clear()
await message.reply('Game is finished', reply_markup=ReplyKeyboardRemove())
@router.message(F.text.lower() == 'quiz')
async def with_puree(message: Message, state: FSMContext):
quiz_kb = [
[KeyboardButton(text='Next question')],
[KeyboardButton(text='Exit Game')],
]
await state.set_state(GameState.quiz)
await message.reply('Start Quiz', reply_markup=ReplyKeyboardRemove())
await message.reply(
'First question:',
reply_markup=ReplyKeyboardMarkup(keyboard=quiz_kb, resize_keyboard=True),
)
await question(message, state)
@router.message(GameState.quiz, F.text)
async def question(message: Message, state: FSMContext):
data = await state.get_data()
question_data = await get_question()
question = question_data[0].get('question')
answers = {
question_data[0]['answers'][k]: question_data[0]['correct_answers'][
k + '_correct'
]
for k in question_data[0]['answers'].keys()
if question_data[0]['answers'][k] is not None
}
buttons = []
idx = 1
text = f'Question:\n{question}\n\nanswers:\n'
for k, v in answers.items():
buttons.append([InlineKeyboardButton(text=f'choice {idx}', callback_data=k)])
if v == 'true':
data['answer'] = k
text = text + f'{idx} - {k}\n'
idx += 1
data['question'] = question
await state.update_data(data)
await message.answer(
text=text,
reply_markup=InlineKeyboardMarkup(inline_keyboard=buttons),
)
@router.callback_query(GameState.quiz)
async def check_answer(callback: CallbackQuery, state: FSMContext, bot: Bot):
answer = (await state.get_data())['answer']
await callback.answer()
if answer == callback.data:
await callback.message.answer(text='Correct')
else:
await callback.message.answer(text='Opps. You miss')
await bot.edit_message_text(
chat_id=callback.from_user.id,
message_id=callback.message.message_id,
text=callback.message.text,
)