Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions bot/chains/register_bug/handlers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import json

from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.types import User as TgUser

from bot.chains.base.kb import start_kb
from bot.chains.register_bug.kb import cancel_kb, get_admin_decision_kb
from bot.chains.register_bug.kb import cancel_kb, get_admin_decision_kb, delete_markup
from bot.chains.register_bug.state import RegisterBug
from bot.config import ADMIN_CHAT_ID
from bot.core import dp, bot
Expand All @@ -25,55 +26,60 @@ async def cancel(c: types.CallbackQuery, state: FSMContext):
async def add_bug_start(msg: types.Message, state: FSMContext):
await RegisterBug.wait_photo.set()
data_state = await msg.answer('Надішліть фото багу 📸', reply_markup=cancel_kb)
await state.update_data({'message': data_state})
await state.update_data({'message_id': data_state['message_id'],
'chat_id': data_state['chat']['id']})


@dp.message_handler(state=RegisterBug.wait_photo, content_types=['photo', 'video', 'document'])
async def add_bug_photo(msg: types.Message, state: FSMContext):
data_state = await state.get_data()
await data_state.get('message').delete_reply_markup()
await delete_markup(data_state.get('chat_id'), data_state.get('message_id'))
if len(msg.photo) == 0:
await msg.answer('Упсс.. Помилка 😔\n'
'Спробуйте надіслати фото, або скасуйте додавання багу', reply_markup=cancel_kb)

await state.update_data({'photo': msg.photo[-1]})
await state.update_data({'photo': msg.photo[1]['file_id']})
await RegisterBug.wait_description.set()
answer = await msg.answer('Опишіть проблему в 1 повідомленні', reply_markup=cancel_kb)
await state.update_data({'message': answer})
await state.update_data({'message_id': answer['message_id'],
'chat_id': answer['chat']['id']})


@dp.message_handler(state=RegisterBug.wait_description)
async def add_bug_description(msg: types.Message, state: FSMContext):
data_state = await state.get_data()
await data_state.get('message').delete_reply_markup()
await delete_markup(data_state.get('chat_id'), data_state.get('message_id'))
await state.update_data({'description': msg.text})
await RegisterBug.wait_location.set()
answer = await msg.answer('Надішліть місцезнаходження (аудиторію, корпус) якомога конкретніше 🏢',
reply_markup=cancel_kb)
await state.update_data({'message': answer})
await state.update_data({'message_id': answer['message_id'],
'chat_id': answer['chat']['id']})


@dp.message_handler(state=RegisterBug.wait_location)
async def add_bug_location(msg: types.Message, state: FSMContext):
data = await state.get_data()
await data.get('message').delete_reply_markup()
data_state = await state.get_data()
await delete_markup(data_state.get('chat_id'), data_state.get('message_id'))
default_status = await BugStatus.select('id').where(BugStatus.status == 'pending').gino.scalar()

photo = data.get('photo')
photo = data_state.get('photo')

bug = await Bug.create(photo_path=photo.file_id,
description=data.get('description'),
bug = await Bug.create(photo_path=photo,
description=data_state.get('description'),
location=msg.text,
status=default_status,
user=TgUser.get_current())

await state.set_data({'bug_id': bug.id})

photo_path = os.path.join(UPLOAD_DIR, f'bugs/{bug.id}.jpg')
await photo.download(photo_path)
await bug.update(photo_path=f'bugs/{bug.id}.jpg').apply()
await bot.download_file_by_id(file_id=photo, destination=photo_path)
await bug.update(photo_path=photo_path).apply()

await bot.send_photo(ADMIN_CHAT_ID, photo.file_id, caption=f'Баг №{bug.id}\n'
await bot.send_photo(ADMIN_CHAT_ID, photo, caption=f'Баг №{bug.id}\n'
f'Місцезнаходження: <i>{msg.text}</i>\n'
f'Опис: "<i>{data.get("description")}</i>"',
f'Опис: "<i>{data_state.get("description")}</i>"',
reply_markup=get_admin_decision_kb(bug.id))

await state.finish()
Expand All @@ -87,7 +93,7 @@ async def admin_decision_(cq: types.CallbackQuery, state: FSMContext):
current_state = await state.get_state()
if current_state == 'RegisterBug:wait_admin_description':
state_data = await state.get_data()
bug_id = state_data['bug'].id
bug_id = state_data.get('bug_id')

await cq.message.answer(f'Спочатку дайте пояснення багу №{bug_id}')
return await cq.answer('Заборонено')
Expand All @@ -110,7 +116,8 @@ async def admin_decision_(cq: types.CallbackQuery, state: FSMContext):

await bot.send_message(ADMIN_CHAT_ID, 'Опишіть, чому цей баг відхилено 🤔')
await RegisterBug.wait_admin_description.set()
await state.set_data({'bug': bug})
await state.set_data({'bug_id': bug.id,
'bug_user': bug.user})
await cq.answer('Відхилено')

await bug.update(status=status).apply()
Expand All @@ -120,7 +127,7 @@ async def admin_decision_(cq: types.CallbackQuery, state: FSMContext):
async def cause_text(msg: types.Message, state: FSMContext):
data_state = await state.get_data()
await bot.send_message(ADMIN_CHAT_ID, f'Дякуємо! Причина буде передана відправнику!')
await bot.send_message(data_state.get('bug').user,
f'Баг № {data_state.get("bug").id} відхилено 😔\n\nПричина: \"{msg.text}\"')
await data_state.get('bug').update(cause=msg.text).apply()
await bot.send_message(data_state.get('bug_user'),
f'Баг № {data_state.get("bug_id")} відхилено 😔\n\nПричина: \"{msg.text}\"')
await Bug.get(int(data_state.get('bug_id'))).update(cause_text=msg.text).apply()
await state.finish()
5 changes: 5 additions & 0 deletions bot/chains/register_bug/kb.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from bot.core import dp, bot
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

cancel_kb = InlineKeyboardMarkup(
Expand All @@ -15,3 +16,7 @@ def get_admin_decision_kb(bug_id):
],
resize_keyboard=True
)


def delete_markup(chat_id, message_id):
return bot.edit_message_reply_markup(chat_id=chat_id, message_id=message_id, reply_markup='')
14 changes: 12 additions & 2 deletions bot/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.contrib.fsm_storage.redis import RedisStorage2
from aiogram.utils.executor import Executor

from bot.config import TELEGRAM_BOT_TOKEN
Expand All @@ -8,7 +10,15 @@

bot = Bot(token=TELEGRAM_BOT_TOKEN, parse_mode='HTML')

dp = Dispatcher(bot=bot, storage=MemoryStorage())
storage = RedisStorage2(
host=os.getenv('REDIS_HOST'),
port=os.getenv('REDIS_PORT'),
db=os.getenv('REDIS_DB'),
password=os.getenv('REDIS_PASSWORD'),
)


dp = Dispatcher(bot=bot, storage=storage)

dp.middleware.setup(ThrottlingMiddleware())
dp.middleware.setup(RegistrationMiddleware())
Expand Down