main
Сергей Ванюшкин 2024-02-23 18:04:30 +03:00
parent 49c74ea2b6
commit c11f87b500
5 changed files with 80 additions and 7 deletions

View File

@ -11,6 +11,7 @@ from aiogram.utils.markdown import hbold
from tg_bot.handlers import ( from tg_bot.handlers import (
games, games,
help,
lesson_five, lesson_five,
lesson_four, lesson_four,
lesson_one, lesson_one,
@ -25,6 +26,7 @@ TOKEN: str = getenv('BOT_TOKEN') or 'Your TG_BOT token'
dp = Dispatcher() dp = Dispatcher()
dp.include_routers( dp.include_routers(
help.router,
quest.router, quest.router,
tic_tac_toy.router, tic_tac_toy.router,
quiz.router, quiz.router,

View File

@ -15,4 +15,5 @@ markup_kb = ReplyKeyboardMarkup(keyboard=kb, resize_keyboard=True)
@router.message(Command('games')) @router.message(Command('games'))
async def games(message: Message): async def games(message: Message):
"""Хэндлер выбора игры"""
await message.answer('Select Game:', reply_markup=markup_kb) await message.answer('Select Game:', reply_markup=markup_kb)

47
tg_bot/handlers/help.py Normal file
View File

@ -0,0 +1,47 @@
from aiogram import Router
from aiogram.filters import Command
from aiogram.types import Message
from aiogram.utils.formatting import (
Bold,
HashTag,
as_key_value,
as_list,
as_marked_section,
)
router = Router()
@router.message(Command('help'))
async def games(message: Message):
"""Хэндлер выбора игры"""
content = as_list(
as_marked_section(
Bold('Реализованный функционал'),
'Эхо_бот',
'Генератор случайных чисел',
'Разворот строки',
'Обработчик приветствия',
'Эхо видеозаметкой',
'API Запросы к deeppavlov.ai',
'Получения адреса и погоды по геометке',
'3 игры',
marker='',
),
as_marked_section(
Bold('Команды:'),
as_key_value('...', 'Бот вернет написанный текст'),
as_key_value('/rand', 'Вернет число от 0 до 100'),
as_key_value('/reverse', 'Запросит строку и развернет ее'),
as_key_value('...', 'Приветствует user, если написать волшебное слово'),
as_key_value('...', 'Вернет video_note, если отправить видео'),
as_key_value('/ask', 'Отправит запрос к апи и вернет ответ'),
as_key_value('...', 'Вернет адрес и погоду, если отправить свои гео'),
as_key_value('/games', 'Выведет кнопки для запуска игр'),
as_key_value('/help', 'Вывод этой справки'),
marker=' ',
),
HashTag('#shitbot'),
sep='\n\n',
)
await message.answer(**content.as_kwargs())

View File

@ -11,10 +11,12 @@ from aiogram.types import (
ReplyKeyboardMarkup, ReplyKeyboardMarkup,
ReplyKeyboardRemove, ReplyKeyboardRemove,
) )
from aiogram.utils.formatting import Bold, as_list, as_marked_section from aiogram.utils.formatting import Bold, Text, as_list, as_marked_section
# Создаем граф
G = nx.Graph() G = nx.Graph()
# Создаем точки графа, точка соответствует игровой локации
G.add_node('start') G.add_node('start')
G.add_node('A') G.add_node('A')
G.add_node('B') G.add_node('B')
@ -22,6 +24,7 @@ G.add_node('C')
G.add_node('D') G.add_node('D')
G.add_node('end') G.add_node('end')
# Связываем точки ребрами создавая связи между локациями
G.add_edge('start', 'A') G.add_edge('start', 'A')
G.add_edge('start', 'B') G.add_edge('start', 'B')
G.add_edge('A', 'C') G.add_edge('A', 'C')
@ -37,11 +40,13 @@ class GameState(StatesGroup):
@router.message(F.text.lower() == 'exit game') @router.message(F.text.lower() == 'exit game')
async def exit_games(message: Message, state: FSMContext): async def exit_games(message: Message, state: FSMContext):
"""Обработчик завершения игры"""
await state.clear() await state.clear()
await message.reply('Game is finished', reply_markup=ReplyKeyboardRemove()) await message.reply('Game is finished', reply_markup=ReplyKeyboardRemove())
async def get_content(place, backward, forward): async def get_content(place: str, backward: str, forward: list[str]) -> Text:
"""Возвращает форматированный текс ответа бота"""
if forward != ['None']: if forward != ['None']:
f_text = 'Можете продолжить путь' f_text = 'Можете продолжить путь'
else: else:
@ -69,10 +74,12 @@ async def get_content(place, backward, forward):
), ),
sep='\n\n', sep='\n\n',
) )
print(type(content))
return content return content
async def get_keyboard(backward, forward): async def get_keyboard(backward: list[str], forward: str) -> InlineKeyboardMarkup:
"""Возвращает инлайн клавиатуру в зависимости от локации"""
buttons = [] buttons = []
if backward is not None: if backward is not None:
@ -100,6 +107,7 @@ async def get_keyboard(backward, forward):
@router.message(F.text.lower() == 'quest') @router.message(F.text.lower() == 'quest')
async def start_quest(message: Message, state: FSMContext): async def start_quest(message: Message, state: FSMContext):
"""Обработчик старта игры"""
quest_kb = [ quest_kb = [
[KeyboardButton(text='Exit Game')], [KeyboardButton(text='Exit Game')],
] ]
@ -120,6 +128,7 @@ async def start_quest(message: Message, state: FSMContext):
@router.message(GameState.quest, F.text) @router.message(GameState.quest, F.text)
async def make_move(message: Message, state: FSMContext): async def make_move(message: Message, state: FSMContext):
"""Обработчик смены локации в игре"""
data = await state.get_data() data = await state.get_data()
keyboard = await get_keyboard(data['backward'], data['forward']) keyboard = await get_keyboard(data['backward'], data['forward'])
@ -130,6 +139,7 @@ async def make_move(message: Message, state: FSMContext):
@router.callback_query(GameState.quest) @router.callback_query(GameState.quest)
async def check_answer(callback: CallbackQuery, state: FSMContext, bot: Bot): async def check_answer(callback: CallbackQuery, state: FSMContext, bot: Bot):
"""Обработчик колбэков на локациях"""
data = await state.get_data() data = await state.get_data()
data['backward'] = data['place'] data['backward'] = data['place']
data['place'] = (callback.data.split('_'))[1] data['place'] = (callback.data.split('_'))[1]

View File

@ -1,4 +1,5 @@
from os import getenv from os import getenv
from typing import Any
from aiogram import Bot, F, Router, html from aiogram import Bot, F, Router, html
from aiogram.fsm.context import FSMContext from aiogram.fsm.context import FSMContext
@ -12,11 +13,18 @@ from aiogram.types import (
ReplyKeyboardMarkup, ReplyKeyboardMarkup,
ReplyKeyboardRemove, ReplyKeyboardRemove,
) )
from aiogram.utils.formatting import Bold, as_key_value, as_list, as_marked_section from aiogram.utils.formatting import (
Bold,
Text,
as_key_value,
as_list,
as_marked_section,
)
from pyquizAPI import QuizClient from pyquizAPI import QuizClient
async def get_question(): async def get_question() -> list[dict[str, Any]]:
"""Получение вопроса с QuizAPI"""
api = getenv('QUIZ_API') api = getenv('QUIZ_API')
client = QuizClient(api) client = QuizClient(api)
client.make_config(limit=1, difficulty='Easy') client.make_config(limit=1, difficulty='Easy')
@ -34,13 +42,15 @@ class GameState(StatesGroup):
@router.message(F.text.lower() == 'exit game') @router.message(F.text.lower() == 'exit game')
async def exit_games(message: Message, state: FSMContext): async def exit_games(message: Message, state: FSMContext):
"""Обработка закрытия игры"""
await state.clear() await state.clear()
await message.reply('Game is finished', reply_markup=ReplyKeyboardRemove()) await message.reply('Game is finished', reply_markup=ReplyKeyboardRemove())
async def get_formated_question( async def get_formated_question(
qnt: int, correct: int, question: str, answers: list[str] qnt: int, correct: int, question: str, answers: list[str]
): ) -> Text:
"""Возвращает форматированный текст для отправки ботом"""
content = as_list( content = as_list(
as_marked_section( as_marked_section(
Bold('Game summary:'), Bold('Game summary:'),
@ -66,6 +76,7 @@ async def get_formated_question(
@router.message(F.text.lower() == 'quiz') @router.message(F.text.lower() == 'quiz')
async def start_quiz(message: Message, state: FSMContext): async def start_quiz(message: Message, state: FSMContext):
"""Обработка запуска игры"""
quiz_kb = [ quiz_kb = [
[KeyboardButton(text='Next question')], [KeyboardButton(text='Next question')],
[KeyboardButton(text='Exit Game')], [KeyboardButton(text='Exit Game')],
@ -82,9 +93,10 @@ async def start_quiz(message: Message, state: FSMContext):
@router.message(GameState.quiz, F.text) @router.message(GameState.quiz, F.text)
async def question(message: Message, state: FSMContext): async def question(message: Message, state: FSMContext):
"""Отправка пользователю шага игры с клавиатурой для выбора"""
data = await state.get_data() data = await state.get_data()
question_data = await get_question() question_data = await get_question()
question = question_data[0].get('question') question = str(question_data[0].get('question'))
answers = { answers = {
question_data[0]['answers'][k]: question_data[0]['correct_answers'][ question_data[0]['answers'][k]: question_data[0]['correct_answers'][
k + '_correct' k + '_correct'
@ -116,6 +128,7 @@ async def question(message: Message, state: FSMContext):
@router.callback_query(GameState.quiz) @router.callback_query(GameState.quiz)
async def check_answer(callback: CallbackQuery, state: FSMContext, bot: Bot): async def check_answer(callback: CallbackQuery, state: FSMContext, bot: Bot):
"""Обработчик нажатий на кнопки в процессе игры"""
data = await state.get_data() data = await state.get_data()
answer = data['answer'] answer = data['answer']
score = data.get('score', 0) score = data.get('score', 0)